I am trying to create a program that rolls 2 dice, gives the amount of die1
and the amount of die2
then adds them together. I am getting the random numbers for the dice, but when they are being added together (sum) the totals are incorrect.
I have tried to change faceValue
, numValue
, I have changed the sum to die1.getFaceValue + die2.getFaceValue
, but the sum always comes out wrong. If someone can look over the code and see if I have everything else in the correct place. Thanks in advance.
package assignment3;
import java.util.*;
public class Die {
private static final int DEFAULT_NUM_FACES =6;
private static final int value = 1;
private static int faceValue;
private static int numFaces;
//die(int, int) method- constructor that initialized the instance variables to the parameter values
public Die(int die1, int die2) {
}
//Die(int) method- constructor that initialized the instance variables faceValue to the parameter value and numFaces to
//DEFAULT_NUM_FACES
public Die(int value) {
faceValue = value;
numFaces = DEFAULT_NUM_FACES;
}
//die() default constructor that initialized both instance variables to DEFAULT_NUM_FACES
public Die() {
this.faceValue=DEFAULT_NUM_FACES;
this.numFaces=DEFAULT_NUM_FACES;
}
//die(die) copy constructor
public Die(Die otherDie)
{
new Die();
}
// getFaceValue() - returns the faceValue of the instance
public int getFaceValue() {
return faceValue;
}
// getNumFaces - return the numFaces of the Die
public int getNumFaces()
{
return numFaces;
}
//setFaceValule - sets face values of the die to supplied value
public int setValue(int faceValue) {
return faceValue;
}
//toString returns a string representation of the face value in the form of (faceValue)
public String toString()
{
String result = Integer.toBinaryString(faceValue);
return result;
}
//Roll - rolls the die to generate a new face value. The instances face value will be set to this new value.
//You can use the random method of the Math class to accomplish this task. The random method generates a random number between 0 and 1.
//by multiplying this number by number of faces, casting the result to an integer and adding one to it.
//you will be able to generate a random number between 1 and numFaces
public int roll()
{
faceValue = (int )(Math.random()*numFaces+1);
return faceValue;
}
public static void main(String[] args) {
Die die1;
Die die2;
int sum;
die1= new Die();
die1.roll();
die2= new Die();
die2.roll();
sum = (die1.getFaceValue())+(die2.getFaceValue()) ;
System.out.println("Toss 0 generated a " + die1.getFaceValue()+ " and a " + die2.getFaceValue() +" for a total of " +sum);
}
}
this should roll die1 and give me the face value of die1, then it should give me the value of die2, then it should total the amount of both dice.
I think you should have an instance variable not a class variable.
Basically, It's only one copy of that variable that is shared with all objects(instances) of that class. If that variable is changed, All other class objects will see the that changes.
Just remove 'static' keyword from your local variables.
public class Die {
private static final int DEFAULT_NUM_FACES = 6;
private int faceValue;
private int numFaces;
public Die() {
this.faceValue = DEFAULT_NUM_FACES;
this.numFaces = DEFAULT_NUM_FACES;
}
// getFaceValue() - returns the faceValue of the instance
public int getFaceValue() {
return faceValue;
}
// getNumFaces - return the numFaces of the Die
public int getNumFaces() {
return numFaces;
}
// setFaceValule - sets face values of the die to supplied value
public int setValue(int faceValue) {
return faceValue;
}
// toString returns a string representation of the face value in the form of
// (faceValue)
public String toString() {
String result = Integer.toBinaryString(faceValue);
return result;
}
public int roll() {
faceValue = (int) (Math.random() * numFaces + 1);
return faceValue;
}
public static void main(String[] args) {
Die die1;
Die die2;
int sum;
die1 = new Die();
die1.roll();
die2 = new Die();
die2.roll();
sum = (die1.getFaceValue()) + (die2.getFaceValue());
System.out.println("Toss 0 generated a " + die1.getFaceValue() + " and a " + die2.getFaceValue()
+ " for a total of " + sum);
}
}
Have a good day..