• Post Reply Bookmark Topic Watch Topic
  • New Topic

First Java Class -- Dice/Statistics  RSS feed

 
D Doemer
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.
 
Mike. J. Thompson
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.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • 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
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • 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: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not replying earlier: am away, but “you're welcome”
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!