Part 2 Write a Java class called Die as in the singular of D

Part 2:

Write a Java class called Die (as in the singular of Dice) that represents one die with faces showing values between 1 and 6. It should contain :

--one integer instance variable, faceValue, that represents the current face value of the die, an integer constant (MAX) that represents the maximum face value of the die.

--a constructor

--and five regular methods: roll, setFaceValue, getFaceValue, toString and equals.

The roll method should compute a random integer between 1 and 6 and set the current face value with that as a way of simulating the rolling of the die. (What Java class and method would you use to get a random integer ?? find out..)

Now, write a driver called RollingDice, in order to test your Die class.

Part 3:

Using the Die class, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values,toString and equals methods, a method to roll the dice (call it roll), and a method that returns the current sum of the two die values (call it getDiceSum).

As Homework:

Properly using your PairOfDice class, write a Java program (a driver with a main function), called DiceGame.java, that allows the user to play the a game with the computer. Here are the rules of the game: A player rolls the dice repeatedly until she rolls at least one 1 or voluntarily gives up the dice. Each time she rolls the dice the total on the faces of the dice is added to her score, except when she rolls a 1. If she rolls one 1, she loses all the points she\'s accumulated so far in her turn. If she rolls two 1\'s, she loses all of her points so far in the game. (This means you have to keep track of points in each turn and points since the beginning of the game). The first player to get a total of 100 wins. The computer follows the same rules, except that it turns over the dice as soon as its score for the current turn is at least 20.

Here\'s an excerpt from a working program.

Your program doesn\'t have to print exactly the same messages, but its output should contain the same information and it should run in the same way. You can assume that the user will make no mistakes when she types in her input.

Solution

import java.util.*;

class Die{

/*
Write a Java class called Die (as in the singular of Dice) that represents one die with faces showing values between 1 and 6. It should contain :
--one integer instance variable, faceValue, that represents the current face value of the die, an integer constant (MAX) that represents the maximum face value of the die.
--a constructor
--and five regular methods: roll, setFaceValue, getFaceValue, toString and equals.

The roll method should compute a random integer between 1 and 6 and set the current face value with that as a way of simulating the rolling of the die. (What Java class and method would you use to get a random integer ?? find out..)
Now, write a driver called RollingDice, in order to test your Die class.

*/
private int faceValue;
private final int MAX = 6;

public Die(){
faceValue = 0;
}
public void roll(){

Random rand = new Random();

int n = rand.nextInt(MAX) + 1;

setFaceValue(n);

}

public void setFaceValue(int n){
faceValue = n;
}

public int getValue(){
return faceValue;
}

public String toString(){
return getValue() + \"\";
}

}

class PairOfDice{

public Die die1;
public Die die2;

public PairOfDice(){
die1 = new Die();
die2 = new Die();
}

public void set_first(int n){
die1.setFaceValue(n);
}

public void set_second(int n){
die2.setFaceValue(n);
}

public int get_first(){
return die1.getValue();
}

public int get_second(){
return die2.getValue();
}

public int getDiceSum(){
return get_first() + get_second();
}

public void roll(){
die1.roll();
die2.roll();

System.out.println(\"you rolled \" + get_first() + \" \" + get_second());


}

public boolean equals(){
return (get_first() == 1 || get_second() == 1);
}


}

public class DiceGame{

public static void main(String args[]) {

PairOfDice p1 = new PairOfDice();
PairOfDice p2 = new PairOfDice();
int grand_p1 = 0;
int grand_p2 = 0;
String msg = \"Welcome to the DiceGame. It\'s you against the computer. You play by rolling the dice. The first player to get 100 points wins. However, if you roll one 1 you lose all the points you\'ve accumulated in your turn. If you roll two 1\'s, you lose all your points. You can turn the dice over at any time. However, if you roll one or two 1\'s, you lose your turn. I (the computer) play by the same rules, except I\'ll always turn over the dice when I\'ve rolled 20 or more points in a single turn. Ready to begin? Type \'y\' when you\'re ready \";
System.out.println( msg);
Scanner sc = new Scanner(System.in);

String input = sc.nextLine();

if(input.equals(\"y\") ){
System.out.println( \"You\'re rolling the dice . . .\");
p1.roll();
grand_p1 += p1.getDiceSum();
System.out.println(\"This gives you a turn total of \" + p1.getDiceSum());
System.out.println(\" and a grand total of \" + p1.getDiceSum());
System.out.println(\" The computer has a total of \" + grand_p2);

}
}
}

Part 2: Write a Java class called Die (as in the singular of Dice) that represents one die with faces showing values between 1 and 6. It should contain : --one
Part 2: Write a Java class called Die (as in the singular of Dice) that represents one die with faces showing values between 1 and 6. It should contain : --one
Part 2: Write a Java class called Die (as in the singular of Dice) that represents one die with faces showing values between 1 and 6. It should contain : --one

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site