Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone please help me out.

 
James harry smith
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic