In: Computer Science
USING JAVA, PLEASE MAKE THIS AS SIMPLE AS POSSIBLE, THIS IS AN INTRODUCTORY JAVA COURSE
1. Design and implement a class called PairOfDice.
The class should include:
a. A non-default constructor, i.e. with parameters
b. A setter method for each instance data
c. A getter method for each instance data
d. A method roll() that rolls both dice
e. A toString() method that returns a string containing the colors of both dice, eg "Colors of both dice: Red,Blue "
f. A method pairSum that returns the current sum of the two die values. (in PairOfDice.java)
2. Write an application TestPairDice that uses the PairOfDice class to create a pair of dice. The application prints to the screen the sum of their face values. The program then rolls the pair of dice and prints to the screen the new sum of their face and the colors of both dice. (TestPairOfDice.java)
SAMPLE OUTPUT: Sum of face values before roll: 7
Sum of face values after roll: 6
Color of both dice: Red,Blue
Below is the solution:
public class PairOfDice {
// declare the variable
private int faceValue1; // die1 faceValue
private int faceValue2; // die1 faceValue
public String color; // string showing the color of
the die
// getter method
public int getFaceValue1() {
return faceValue1;
}
public int getFaceValue2() {
return faceValue2;
}
public String getColor() {
return color;
}
// setter method
public void setFaceValue1(int faceValue1) {
this.faceValue1 = faceValue1;
}
public void setFaceValue2(int faceValue2) {
this.faceValue2 = faceValue2;
}
public void setColor(String color) {
this.color = color;
}
// roll two die
public void roll() { // Rolls the die and returns the
result
faceValue1 = (int) (Math.random() *
6) + 1;
faceValue2 = (int) (Math.random() *
6) + 1;
}
// return the pair of sum two die
public int pairSum() {
return faceValue1 +
faceValue2;
}
// toString method
@Override
public String toString() {
return color;
}
// default constructor
public PairOfDice() {
// set the default value of the
die1 and die2
this.faceValue1 = 4;
this.faceValue2 = 3;
}
// non-default constructor, i.e. with
parameters
public PairOfDice(int faceValue1, int faceValue2)
{
super();
this.faceValue1 = faceValue1;
this.faceValue2 = faceValue2;
}
}
public class TestPairOfDice {
public static void main(String[] args) {
PairOfDice die1 = new PairOfDice();
// create an object
die1.setColor("Red"); // set the
first die color to red
System.out.println("Sum of face
values before roll: " + die1.pairSum()); // print sum of the
die
PairOfDice die2 = new
PairOfDice();
die2.setColor("Blue"); // set the
second die color to red
die2.roll();
System.out.println("Sum of face
values after roll: " + die2.pairSum());
System.out.println("Color of both
dice:" + die1.toString() + "," + die2.toString());
}
}
sample output 1:
Sum of face values before roll: 7
Sum of face values after roll: 6
Color of both dice:Red,Blue
sample output 2:
Sum of face values before roll: 7
Sum of face values after roll: 10
Color of both dice:Red,Blue