wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Can someone please help me out. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone please help me out." Watch "Can someone please help me out." New topic
Author

Can someone please help me out.

James harry smith
Greenhorn

Joined: Oct 14, 2011
Posts: 1
I've been asked to do the following question for my computer science lab today and I don't even know where to start... I would really appreciate it if someone could give me a few pointers on where to start please.. Any help is appreciated

Write a program that creates a Dice object.
Your program will involve two separate classes in different files:
One class for the main method
Another class describing the Dice object
The Dice class should have fived methods (and a single class variable for storing the number on the dice):
A constructor method with the same name as the class
A roll( ) method for rolling the dice
a getValue( ) method for returning the value showing on the dice
An isEven( ) method which checks if the value is even or odd An isHigh( ) method which checks if the value is high or low
The main application should do the following:
1. Create a Dice object
2. Roll the dice every time the user hits enter
3. Print out the new value showing on the dice
4. Print out whether the value is even or odd, high or low
The interaction with the program might look something like this:
Dice roll: 5
Roll is ODD
Roll is HIGH
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You can start by designing the Dice class first. First think a real dice. What are its behavior. A dice will have numbers from 1 - 6. And whenever you roll it, you get different (or random) occurrences between 1 and 6.

You shall have a instance variable named number to store the number got by rolling the dice.

1. The roll() method in the Dice class should roll the dice and give an output between 1 and 6.

2. The getValue() method should return the number returned by rolling the dice.

3. The isEven() method should check if the rolled number is even or odd.

4. isHigh() method - You have to think what it should do. Do you consider a roll above 3 high? or above 4 high?

So the Dice class may look some thing like below...



Now comes the main class... Let's name it DiceRoller. The things that this class must do are -

1. On every user entering return key, a new dice has to be rolled

2. Roll the newly created dice, print its value, check if it's even and print if it's high...

Below shall be structure of the DiceRoller class. Note that since this is the main class that starts the roll, it contains the main() method in it.



I will leave you with code hints..

Below code shall be used to create random numbers from 1 - 6 on every different method call.


Below shall be used to read return key input from user to use in the while loop -

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

take it one step at at time. write AT THE MOST 2-3 lines before you compile. Don't be afraid to write lots of code you know you will throw away (I probably write at least one "System.out.println()" statement that I eventually delete for every line I keep. Follow the directions, on step at a time:

1) One class for the main method

So this tells me the first thing I should do is something like this:


After I wrote that much, I'd compile and run it. THEN I'd start step two:
2) Another class describing the Dice object

Again, I would write EXACTLY THAT and then make sure it compiles. THEN I'd start adding the parts, one at a time. First, the variable to hold the value:

Then compile that. Then add the constructor. Once you have that, you can try and make one in your DiceTester class. When you first make the constructor, you can have it do NOTHING but print "I'm constructing a die!!!" Once THAT works, you can maybe have it do something...



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Welcome to the Ranch

Don’t do what I did once: create a throw() method in a Die class. I couldn’t understand why it wouldn’t compile!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

Campbell Ritchie wrote:
Don’t do what I did once: create a throw() method in a Die class. I couldn’t understand why it wouldn’t compile!

HA HA HA!!!

That's awesome...and sounds like something I would do, and beat my head against a wall for a few hours...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
I had only been learning for a few weeks, and did beat my head against the wall for ages. Eventually I thought it would work if I changed the method name to . . .




























































throws()

I did realise eventually what I had done wrong. Sent my teacher an e-mail saying I hope she wouldn’t throw me out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone please help me out.
 
Similar Threads
Exception in thread "main" java.lang.NoClassDefFoundError: dice (wrong name: Dice)
Dice game issue
So far...
Dice Program Desparate Help!!!
help on Yahtzee