• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dice  RSS feed

 
D Doemer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again everyone. I am going back to some of my beginning assignments to try to understand better. My teacher has been helping me along but I hate to keep bothering him.

Need help writing main program for this assignment. The instructions are:

Create a class called Dice to represent a single cube. The class diagram for class is shown. It should have a method called roll() that randomly selects a number from 1 to 6 for the value of the dice.

This is a main program, not JUnit, and it is java.util.Random, not math.Random.

This is what I have so far:





It's the "public static void main() part that I need help with. Right now it's showing up as a bunch of 10000's, but I need it to show the random numbers that are rolled on a dice. Please, please keep it very simple, and thank you so much in advance.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your explanation at the top didn't say what it is you are meant to be doing. It mentions creating the Dice class and gives some details about what it should do, but what is the ultimate aim of the exercise? Are you meant to roll the dice and display the results?


Currently your main method loops 10000 times, and each time prints out the number 10000. You aren't using your instance of Dice at all.
 
D Doemer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is supposed to roll the dice randomly and display the results. I gave it 10,000 rolls. I'm not sure how to write a main program, so I don't know how to make it roll anything other than 10,000.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are getting such a result because you are printing the local variable numberShowing in the for loop which was initialized to 10000.

What you should be doing instead is within the println() method place new Dice() to get the string representation of that object.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diana Oswald wrote:Yes, it is supposed to roll the dice randomly and display the results. I gave it 10,000 rolls. I'm not sure how to write a main program, so I don't know how to make it roll anything other than 10,000.


It didn't roll 10000, it can't roll anything outside of the range 1-6. You're getting 10000 because as Ricoo said, that's the value you set into the variable you're printing out. You wrote the roll() method, so you need to call it to get the dice value.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:You wrote the roll() method, so you need to call it to get the dice value.


This is correct... I was getting ahead of myself with putting new Dice() inside the println() method....

What you need to do is inside the for loop call the roll() method on the dice reference and pass the dice reference as an argument to the println() method to get the value.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or in other words:

Maybe part of the confusion is that you have two variables 'numberShowing': one in your Dice class, and
one in your main method. These two variables have nothing to do with each other.
 
D Doemer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all so much. I knew it was something simple but I just don't have enough experience yet. Really appreciate your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!