Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Die class as defined bellow, design and implement a class called PairOf

ID: 3785404 • Letter: C

Question

Create a Die class as defined bellow, 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.

Die class definition:

features:

faceValue: int //between 1 and MAX where MAX is constant equal with 6 methods:

getFaceValue(): int setFaceValue(int newFaceValue): void

roll(): int // roll the die, get a random integer between 1 and 6, and return it

toString(): String // returns a String containing the value of the face

Explanation / Answer

RollingDice2.java


public class RollingDice2 {

  
   public static void main(String[] args) {
       PairOfDice p = new PairOfDice();
       System.out.println("The sum of the two die values: "+p.getSum());

   }

}

PairOfDice.java


public class PairOfDice {
   int sum;
   public PairOfDice(){
   Die d1 = new Die();
   Die d2 = new Die();
   int dice1Value = d1.roll();
   int dice2Value = d2.roll();
   sum = dice1Value + dice2Value;
   System.out.println(d1.toString());
   System.out.println(d2.toString());
  
   }
   public int getSum() {
       return sum;
   }
  
}

Die.java


import java.util.Random;

public class Die {
   int faceValue;
   public Die(){
      
   }
   public int getFaceValue() {
       return faceValue;
   }
   public int roll(){
       Random r = new Random();
       faceValue = r.nextInt(7);
       return faceValue;
   }
   public String toString(){
       return "Face Value is "+faceValue;
   }
}

Output:

Face Value is 6
Face Value is 5
The sum of the two die values: 11