File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Create a Die Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Create a Die Class" Watch "Create a Die Class" New topic
Author

Create a Die Class

lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I am very very very new to Java and have been struggling for days and just don't seem to understand.

I need to:

1. Create a class named Die to store data about each die. This class should contain these constructors and methods:

public Die() //default to 6 sided die
public Die(int sides) //allow a variable number of sides
public void roll()
public int getValue()

2. Create a class named PairOfDice to stre two dice. This class should contain two instance variables of the Die type, an instance variable that holds the sum of the two dice, and these constructors and methods:

public PairOfDice() //default to six sided dice
public PairOfDice (int sides) //allow a variable number of sides
public void roll()
public int getValue1() // get value of die1
public int getValue2() // get value of die2
public int getSum() // get the sum of both dice

Use the ramdon method of the Math calss to generate a random number from 1 to the number of sides on a die like this:


int value = (int) (Math.randon() * sides);

3. Create a class named PairOfDice class to roll the dice. This class should display special messages for craps, snake eyes and box cars. Assume that two six sided dice are used.

If anyone could help me understand this concept and how to get started I would appreciate it! It's due sunday night.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this site will help you where you're stuck, so let us know exactly the part you have trouble with.

> It's due sunday night.

if you have nothing done, return to previous activities, and don't worry about it.
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I think the problem is understanding Java lol.

I did this and I'm sure it's not even close for starting to set it up.

package chapt06;
import java.util.Scanner;

public class CH06PR63App {
public static void main(String[] args) {

}
}

class Die {
public int getValue(){
return 0;
}
}
public Die (int sides);
public void roll ();
public int getValue();
}

class PairOfDice {



}
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I guess what I need is someone to help walk me through this. It's really that I don't understand how to get started and what needs to go where and why.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Start by writing all of your posted java
code onto paper.

Scibble into each class the fields it is
likely to have e.g. the Die class might
have a field
int valueShowing;

Scribble into each method what the
method is to do, and what has to be done
to achieve that e.g. generate a random number.

Basically, you write the program on paper
and when you have all the requirements
covered, you convert your notes/scribble
to java.
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
I worked on this all night lol.

Here's what I've got. There's still a problem as it doesn't run and I'm not sure how to get it to say Roll 1: then change to Roll 2: Roll 3: etc.


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> There's still a problem as it doesn't run

it does run, but you have a huge early problem that causes it to do nothing except print the welcome message

debugging 101:
first line of main() add this
System.out.println("OK");

so you'll have this


recompile/run the program, and if 'OK' prints out, move it to after the next line,
so you'll have this


repeat until 'OK' is not printed (won't take long), then you'll have your offending line.
work out exactly what the line is doing, and correct it.

post again if you have another problem, but use code tags - you may notice the code is better to read
lizz Palmer
Greenhorn

Joined: Feb 10, 2012
Posts: 22
Ok I have worked on the code and think it's pretty close. I now need to pull the information from the other classes to display the results to the console. If anyone could show me how this works or what I would need to do, that would be great!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36464
    
  15
Why are your die and pair of dice classes not top-level public classes? They are not that closely associated with a craps game that you should have them as inner classes. You use dice in pairs for other games, eg Monopoly,
You have got too many fields. Why have you got value fields in your pair class? The pair does not have values, it has two dice. That is it: two dice. Nothing else. You can work out the values from the individual dice. It would be better to create a MultipleDice class, with an array of dice, which can be used for any game, irrespective of number of dice used. (It will even work for bridge, which uses cards only, with a 0-length array.)
Your die class ought not to have public fields; they should all be private and accessed via accessor methods.
A method should do one thing and one thing only; the getValue() method should simply return its current value. You should have a separate roll() method for the die.

I presume you got the Die class working before you tried the pair class? You should always code that way, from periphery to centre. [You usually design the other way, from centre to periphery.] You should also code in small bits; compile and ru nyuour code every five (or fewer) lines. Similarly. get the pair class working before you try the game class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create a Die Class
 
Similar Threads
What is wrong with this code?
Need Help With returning some int numbers.
Can someone please help me out.
Can someone help me out with this code
Simple Dice Rolling Project