This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

First Java Class -- Dice/Statistics

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I am taking my first Java class and I'm starting to get behind because I don't understand it. Our assignment has several steps, and I'm on #5:

Create a class called DiceStatistics to represent a pair of dice and the totals of their rolls. It should have an array of two Dice as a private data member (these are the Dice from Problem 1). It should also have, as a data member, an array of integers to represent the possible totals of rolling two dice.
The class should also have the following methods:
initStats() to initialize all the totals to zero.
rollOnce() to roll each die once, and add one to the correct totals element
printStatistics() to print the number of times each total has come up over the number of runs.



Here is my code:






And here is the error:


run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Dice.roll
at DiceStatistics.rollOnce(DiceStatistics.java:27)
at DiceStatistics.main(DiceStatistics.java:55)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)




I know it has something to do with the fact that I need to somehow import the information from my first program "Dice" into this in order to actually get the dice statistics, but how do I do that?

Thanks.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Diana Oswald wrote:Hi everyone,

I am taking my first Java class and I'm starting to get behind because I don't understand it. Our assignment has several steps, and I'm on #5:

Create a class called DiceStatistics to represent a pair of dice and the totals of their rolls. It should have an array of two Dice as a private data member (these are the Dice from Problem 1). It should also have, as a data member, an array of integers to represent the possible totals of rolling two dice.
The class should also have the following methods:
initStats() to initialize all the totals to zero.
rollOnce() to roll each die once, and add one to the correct totals element
printStatistics() to print the number of times each total has come up over the number of runs.



Here is my code:


public class DiceStatistics {
private final int NUMBER_OF_DICE = 2;

private Dice[] dice = new Dice[NUMBER_OF_DICE];
private int[] totals;
private int numberOfRolls;

public DiceStatistics() {
dice[0] = new Dice();
dice[1] = new Dice();

int sides = 6*dice.length;

totals = new int[sides + 1];
}

public void initStats() {
for ( int i=0; i<totals.length; i++)
totals[i] = 0;
numberOfRolls = 0;
}

public int rollOnce() {
int total = 0;
for ( Dice d: dice ) {
total += d.roll();
}
totals[total]++;
numberOfRolls++;
return total;
}

public int[] getStatistics() {
return totals.clone();
}

public void printStatistics() {
System.out.println("Counts for totals:");
for ( int i = NUMBER_OF_DICE*1; i < totals.length; i++)
System.out.printf(" %2d\t%5d\n", i, totals[i]);
}

public void printPercentages() {
System.out.println("Statistics:");
for ( int i = NUMBER_OF_DICE*1; i < totals.length; i++)
System.out.printf(" %2d\t%5.2f%%\n", i, 100.0*totals[i]/numberOfRolls);
}

public static void main(String[] args) {
DiceStatistics diceStatistics = new DiceStatistics();

int howManyRolls = 100000;
for ( int i=0; i<howManyRolls; i++)
diceStatistics.rollOnce();
diceStatistics.printStatistics();
diceStatistics.printPercentages();
}
}




And here is the error:


run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Dice.roll
at DiceStatistics.rollOnce(DiceStatistics.java:27)
at DiceStatistics.main(DiceStatistics.java:55)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)




I know it has something to do with the fact that I need to somehow import the information from my first program "Dice" into this in order to actually get the dice statistics, but how do I do that?

Thanks.



When you're posting code you should use the code tags so that it formats it nicely for others to read. There's a button above the text editor labled 'Code' that will insert the code tags for you.

As for your problem, your code is not compiling correctly but your IDE has allowed you to run it anyway and it failed at runtime. You haven't posted all of the code, but is there a compilation error in the Dice class?

Try compiling it outside of your IDE and see where it fails.
 
Bartender
Posts: 4007
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Diana Oswald wrote:
I know it has something to do with the fact that I need to somehow import the information from my first program "Dice" into this in order to actually get the dice statistics, but how do I do that?

Thanks.



hi Diana,

if you are using an IDE, then at the left of some line you see a red bullet (well, in NetBeans anyway). Hovering the mouse
over it pops up a message indicating what could possibly be wrong.
It looks like it doesn't reckognize 'd.roll()' somehow. Can you show us the code of the 'Dice' class?

You do not need to 'import' your 'Dice' class. It suffices when that class is in the same map as the DiceStatistics class.

Greetings,
Piet
 
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote: . . . you see a red bullet (well, in NetBeans anyway). Hovering the mouse
over it pops up a message indicating what could possibly be wrong. . . .

Eclipse shows a red triangle like a road sign. You can tell it has to do with roll() not Dice otherwise you would have got the error for other uses of Dice. Never try to get the IDE to run code with possible syntax errors like that.
You can read about code tags here; I added them because you are new and you can see the improvement

And welcome to the Ranch
 
D Doemer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not replying earlier: am away, but “you're welcome”
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic