• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can someone please help me out.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 -

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic