# Pair of Die Class question or guide

Page 1 of 1

## 2 Replies - 6828 Views - Last Post: 23 March 2008 - 12:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=46844&amp;s=631da2bd60a59a75bcd2d55bbe432a31&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dhallrec87

Reputation: 0
• Posts: 1
• Joined: 20-March 08

# Pair of Die Class question or guide

Posted 20 March 2008 - 06:59 PM

I am creating a pair of die class and I need way to find the doubles rolled and and get the total of doubles rolled. I am stuck! I keep on trying to do things but I keep getting errors. This is what I have so far where there are no errors. Any help would be awesome

public class PairOfDice {
private static final int MAX_STEP = 6;
private Die die1;
private Die die2;
private int DieSides = (MAX_STEP);
private int Double = 0;
/**
* Constructs a PairOfDice object.
*/
public PairOfDice() {
this.die1 = new Die(MAX_STEP);
this.die2 = new Die(MAX_STEP);
}
/**
* Conducts a roll of the dice pair.
* @return a String representing the rolled dice, e.g., "3-4"
*/
public String roll() {
return this.die1.roll() + "-" + this.die2.roll();

}
/**
* Accessor Method that returns the number of sides for each die.
* In this case it is the MAX_STEP, which is 6.
*
*/
public int getDieSides() {
return this.DieSides;

}

}

Is This A Good Question/Topic? 0

## Replies To: Pair of Die Class question or guide

### #2 letthecolorsrumble

• Student of The Sun

Reputation: 27
• Posts: 555
• Joined: 07-November 07

## Re: Pair of Die Class question or guide

Posted 20 March 2008 - 08:57 PM

First, Welcome to </dream.in.code> Have a great time here.

Second, as you are a beginner I'd suggest you start with the basics of Java, and if you are already through, with the basics, you will need more practice to work on the higher level topics in Java.

Third, I have modified your program and came up with a sample solution of how it could be, you can surely find other ways of doing it. But try to learn by understanding what is happening in the following code.

```public class PairOfDice {

private static final int MAX_STEP = 6;
private int die1;
private int die2;
private int DieSides;
private int doubles;

/**
* Constructs a PairOfDice object.
*/
public PairOfDice() {

die1 		= 0;
die2 		= 0;
doubles 	= 0;
DieSides 	= MAX_STEP;

}

/**
* Conducts a roll of the dice pair.
* @return a String representing the rolled dice, e.g., "3-4"
*/
public String roll() {

//generating random numbers for each die
die1 = (int)(1 + Math.random() * (MAX_STEP - 1 + 1));
die2 = (int)(1 + Math.random() * (MAX_STEP - 1 + 1));
checkDoubles(die1,die2); //calling the checkDoubles method
return die1 + "-" + die2;
}

public void checkDoubles(int die1, int die2) {
if(die1 == die2) {
doubles = doubles + 1;
}
}

/**
* Accessor Method that returns the number of sides for each die.
* In this case it is the MAX_STEP, which is 6.
*
*/

public static int getMAX_STEP() {
return MAX_STEP;
}

public int getDie1() {
return die1;
}

public void setDie1(int die1) {
this.die1 = die1;
}

public int getDie2() {
return die2;
}

public void setDie2(int die2) {
this.die2 = die2;
}

public int getDieSides() {
return DieSides;
}

public void setDieSides(int dieSides) {
DieSides = dieSides;
}

public int getDoubles() {
return doubles;
}

public void setDoubles(int doubles) {
this.doubles = doubles;
}

//Sample
public static void main(String[] args) {
PairOfDice pod = new PairOfDice();

for(int i=0; i < 10; i++) {
System.out.println(pod.roll());
}

System.out.println("Number of sides of the dice used: "+pod.getDieSides());
System.out.println("Number of doubles during rolls: "+ pod.getDoubles());
}

}

```

Hope this helps.

### #3 FlawlessAxis

Reputation: 0
• Posts: 9
• Joined: 21-March 08

## Re: Pair of Die Class question or guide

Posted 23 March 2008 - 12:21 AM

you should never name a variable that begins with an UPPERCASE. This is used to declare classes.

A better way of naming variables is referred to as camelCase, or a lowercase beginning letter and for each other word in the variableName, begin with an UPPERCASE letter.

Also, if you had used camelCase to name your variables, you would've avoided your first problem I spotted in the program, which was naming a variable with an already existent class(Double)