I need a ready to go main method with the RolllingDice2 and
I need a ready to go main method with the RolllingDice2 and PairOfDice Classes. Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.
public class Die { private final int MAX = 6; // maximum face value private int faceValue; // current value showing on the die //----------------------------------------------------------------- // Constructor: Sets the initial face value. //----------------------------------------------------------------- public Die() { faceValue = 1; } //----------------------------------------------------------------- // Rolls the die and returns the result. //----------------------------------------------------------------- public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; } //----------------------------------------------------------------- // Face value mutator. //----------------------------------------------------------------- public void setFaceValue (int value) { faceValue = value; } //----------------------------------------------------------------- // Face value accessor. //----------------------------------------------------------------- public int getFaceValue() { return faceValue; } //----------------------------------------------------------------- // Returns a string representation of this die. //----------------------------------------------------------------- public String toString() { String result = Integer.toString(faceValue); return result; } }
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
########### PairOfDice.java ############
public class PairOfDice {
// two Die reference
private Die dice1;
private Die dice2;
// constructor
public PairOfDice(){
//creating Die Objects
dice1 = new Die();
dice2 = new Die();
}
// set face values for dices
public void setFaceValueFirstDie(int faceValue){
dice1.setFaceValue(faceValue);
}
public void setFaceValueSecondDie(int faceValue){
dice2.setFaceValue(faceValue);
}
// get face values
public int getFaceValueFirstDie(){
return dice1.getFaceValue();
}
public int getFaceValueSecondDie(){
return dice2.getFaceValue();
}
// roll dice
public void roll(){
dice1.roll();
dice2.roll();
}
public int getSum(){
return dice1.getFaceValue()+dice2.getFaceValue();
}
}
############# RollingDice2 .java ################
public class RollingDice2 {
public static void main(String[] args) {
// creating Object
PairOfDice pair = new PairOfDice();
System.out.println(\"Sum : \"+pair.getSum());
pair.setFaceValueFirstDie(4);
pair.setFaceValueSecondDie(3);
System.out.println(\"Get Face Value Die 1: \"+pair.getFaceValueFirstDie());
System.out.println(\"Get Face Value Die 1: \"+pair.getFaceValueSecondDie());
System.out.println(\"Sum : \"+pair.getSum());
pair.roll();
System.out.println(\"\ After roll: \");
System.out.println(\"Get Face Value Die 1: \"+pair.getFaceValueFirstDie());
System.out.println(\"Get Face Value Die 1: \"+pair.getFaceValueSecondDie());
System.out.println(\"Sum : \"+pair.getSum());
}
}
/*
Sample run:
Sum : 2
Get Face Value Die 1: 4
Get Face Value Die 1: 3
Sum : 7
After roll:
Get Face Value Die 1: 2
Get Face Value Die 1: 5
Sum : 7
*/


