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

} -------------------------------------------------------------------- You will

ID: 3595454 • Letter: #

Question

}

--------------------------------------------------------------------

You will use your Die class from the pre-lab to create a PairOfDice class. Then you will create a DiceRoller program to play a dice game against the computer using your PairOfDice.

Part 1: Create the PairOfDice class

The PairOfDice class will manage a pair of Die objects so we don't have to keep track of 2 dice in the main method of our DiceRoller.

Instance Data

Your PairOfDice class will need variables to store two Die objects.

Constructors

Both dice in a PairOfDice will have the same maximum face value.

Declaration: PairOfDice()

Creates 2 Die objects with the default number of sides.

Declaration: PairOfDice(int numSides)

Accepts an integer parameter specifying the number of sides and creates 2 Die objects with the same number of sides.

Getter Methods

Getter: public int getFaceValue1(): Returns the current face value of the first Die only.

Getter: public int getFaceValue2(): Returns the current face value of the second Die only.

Getter: public int getTotal(): Returns the sum of the current face values.

Setter: (we don't need any setters)

Public methods

Method declaration: public int roll(): Rolls both Die objects and returns the sum of their face values.

toString Method

Write a public String toString() method that returns a representation of the PairOfDice. The string you return should look something like:

Part 2: Use your PairOfDice to play a game

Implement DiceRoller to use a PairOfDice object to play a game where the user and the computer each get to roll pairs of dice and the one with the higher total wins.

Instead of two individual Die objects, use one PairOfDice object. When you create your PairOfDice, pass in a reasonable maximum face value (e.g. 6, 4, 12).

Now, modify the main method of DiceRoller to contain a loop that

Rolls the PairOfDice for the user and then rolls the same PairOfDice again for the computer.

Reports the results of each roll (user and computer).

The report for each roll will contain the total value of the PairOfDice followed by the face value of each Die (see sample session in section below).

Determines and announces the winner (or a tie).

Asks the user if they would like to roll again.

The loop will continue to repeat as long as the user continues entering 'y'.

As long as the play continues, DiceRoller should keep track of the number of wins for each player and report the total score after each round.

import java.util.Random;

Explanation / Answer

Die.java

import java.util.Random;

/**
* Die.java
*
* Represents one die (singular of dice) with faces showing values between 1 and
* 6.
*
* @author Java Foundations
* @author CS121 Instructors (modified a few things from book)
*/
public class Die {
private int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die
private Random rand;

/**
* Constructor: Sets the initial face value of this die.
*/
public Die() {
rand = new Random();
faceValue = 1;
}

public Die(int noOfSides) {
this.MAX = noOfSides;
rand = new Random();
}

/**
* Computes a new face value for this die and returns the result.
*
* @return The new face value.
*/
public int roll() {
// faceValue = (int)(Math.random() * MAX) + 1;
faceValue = rand.nextInt(MAX) + 1;
return faceValue;
}

/**
* Face value mutator. The face value is not modified if the specified value
* is not valid.
*
* @param value
* The new face value. Must be between 1 and max face value.
*/
public void setFaceValue(int value) {
if (value > 0 && value <= MAX) {
faceValue = value;
}
}

/**
* Face value accessor.
*
* @return The current face value.
*/
public int getFaceValue() {
return faceValue;
}

/**
* Returns a string representation of this die.
*/
public String toString() {
String result = "Die [faceValue = " + faceValue + "]";
return result;
}
}

_________________

PairOfDice.java

public class PairOfDice {
private Die d1, d2;

public PairOfDice() {
// Roll the dice by setting each of the dice to be
// a random number between 1 and 6.
d1 = new Die();
d2 = new Die();
}

public PairOfDice(int numSides) {
d1 = new Die(numSides);
d2 = new Die(numSides);


}

public int getFaceValue1() {
return d1.getFaceValue();
}
public int getFaceValue2() {
return d2.getFaceValue();
}

public int getTotal() {
return getFaceValue1() + getFaceValue2();
}

public int roll() {
int num1 = d1.roll();
int num2 = d2.roll();


return num1 + num2;
}
@Override
public String toString() {
return getTotal() + "(" + getFaceValue1() + "+" + getFaceValue2() + ")";
}


}

___________________

DiceRoller.java

import java.util.Scanner;

public class DiceRoller {

public static void main(String[] args) {

int cntUserWins = 0, cntComputerWins = 0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

PairOfDice p = new PairOfDice(6);
int userRolled, computerRolled;
while (true) {
userRolled = p.roll();
String user = p.toString();

computerRolled = p.roll();
String computer = p.toString();

if (userRolled > computerRolled) {
System.out.println("User Wins ! " + user);
cntUserWins++;
} else if (computerRolled > userRolled) {
System.out.println("Computer Wins ! " + computer);
cntComputerWins++;
} else {
System.out.println("It's a Tie.");
}

//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to continue(Y/N) ::");
char ch = sc.next(".").charAt(0);
if (ch == 'Y' || ch == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}

}

System.out.println("Total no of times user wins:" + cntUserWins);
System.out.println("Total no of times computer wins:" + cntComputerWins);

}

}

____________________

Output:

Computer Wins ! 9(3+6)
Do you want to continue(Y/N) ::y
User Wins ! 7(3+4)
Do you want to continue(Y/N) ::y
Computer Wins ! 8(6+2)
Do you want to continue(Y/N) ::y
User Wins ! 7(1+6)
Do you want to continue(Y/N) ::y
User Wins ! 8(6+2)
Do you want to continue(Y/N) ::y
Computer Wins ! 9(3+6)
Do you want to continue(Y/N) ::y
It's a Tie.
Do you want to continue(Y/N) ::y
User Wins ! 7(1+6)
Do you want to continue(Y/N) ::y
User Wins ! 10(4+6)
Do you want to continue(Y/N) ::y
Computer Wins ! 10(6+4)
Do you want to continue(Y/N) ::y
Computer Wins ! 11(6+5)
Do you want to continue(Y/N) ::y
Computer Wins ! 3(1+2)
Do you want to continue(Y/N) ::y
Computer Wins ! 6(2+4)
Do you want to continue(Y/N) ::y
User Wins ! 6(4+2)
Do you want to continue(Y/N) ::n
:: Program Exit ::
Total no of times user wins:6
Total no of times computer wins:7

_____________Could you rate me well.Plz .Thank You