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

Step 1) Using the Die class defined in Chapter 4, write a class called PairOfDic

ID: 3592109 • Letter: S

Question

Step 1)
Using the Die class defined in Chapter 4, write a class called PairOfDice, composed of two Die objects. Include methods to set and get each of the individual die values, a method to roll the two die, and a method that returns the current sum of the two die values. The constructor should initialize each die to 1.
Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.

Note 1: Die class does not change.

Note 2: Make sure PairOfDice is working before you proceed with the remainder of assignment.

Here is the UML:

PairOfDice

- die1: Die
- die2: Die

+ PairOfDice(): PairOfDice
+ getDie1(): Die
+ getDie2(): Die
+ setDie1(int): void
+ setDie2(int): void
+sumDice(): int
+rollDice():int
+toString():String

Step 2)
Using the PairOfDice class, design and implement a program to play a game called Hog. In this game, a computer user (player 1) competes again a human user (player 2). On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 50 points before your opponent does.
If, on any turn, the player rolls a 1 on either die, all points accumulated for that round (turn) are forfeited and control of the dice moves to the other player. The human player may voluntarily turn over the dice after each roll. Therefore, the human player must decide to either roll again and risk losing points, or relinquish control of the dice, possibly allowing the other player to win.
Implement the computer player such that it always relinquishes the dice after accumulating 20 or more points in any given round/turn.

I'll get you started by providing an algorithm (this is VERY high-level, you still have PLENTY of designing to do) and some of the variables/data you will need (feel free to add more or use less):

Data
char answer
PairOfDice dice
Die die1
Die die2
int computerTotalScore
humanTotalScore
computerRoundTotal //points for one turn
humanRoundTotal //points for one turn
Feel free to use more variables

____________________________________________________________________________________________

mport java.util.*;

class Die

{

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

{

private Die die1;

private 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);

}

}

________________________________________

import java.util.Scanner;

public class Hog

{

public static void main(String args[])

{

PairOfDice p1 = new PairOfDice();

PairOfDice p2 = new PairOfDice();

int grand_p1 = 0;

int grand_p2 = 0;

int count=0;

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);

  

String msg = "Type 'y' when you're ready ";

System.out.println( msg);

Scanner sc = new Scanner(System.in);

String input = sc.nextLine();

while(input.equals("y"))

{

count++;

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);

System.out.println( msg);

Scanner s = new Scanner(System.in);

String in = s.nextLine();

}

  

}

}

________________________________________

how to modify the code to accumulate the sum for each round and switch the round to a computer

Note 1: Die class does not change.

Note 2: Make sure PairOfDice is working before you proceed with the remainder of assignment.

Explanation / Answer

After a PairOfDice object has just been created, the number on each die is definitely between 1 and 6, just like the number on a real die. Can we be sure that this will always be true? Not if the instance variables die1 and die2 are public, since they can be changed from outside the class. There is nothing to stop someone from changing them to 42 and -17 or anything else. It's not good enough to say that you're not supposed to do that with dice. I want an absolute guarantee that my dice objects can only have the values that real dice could have. By making die1 and die2 private, I can have that guarantee, because the code that I write in the PairOfDice class is the only code that will ever affect the values of the variables.

So, we will make die1 and die2 private, and add instance methods getDie1() and getDie2() to return the values of die1 and die2. These are "getter" methods that get the value of a member variable, and their names follow the convention that the name of a getter method consists of "get" followed by a capitalized version of the variable name. It would still be useful to be able to set the values of the dice, but we want to make it impossible to set the value to anything outside the range 1 to 6. The solution is to provide "setter" methods that throw an exception when the value is illegal.

I also added the toString() method from Subsection 5.3.3. As for other improvements, I can foresee that people who use my class will often be interested in the total on the dice, so they will tend to say things like "dice.getDie1() + dice.getDie2()" a lot. If this is going to be done over and over, why not provide a method in the class to do it? So, I will also add a method getTotal() that returns the total value showing on the two dice. The complete, modified PairOfDice class is shown below.

(Here is another improvement that I thought about. We could modify the roll() method so that in addition to rolling the dice, it would also return an int value giving the total on the dice. For example, this would allow us to replace "dice.roll(); val = dice.getTotal()" with "val = dice.roll()". Since it's legal to call a function with a subroutine call statement, we could still say "dice.roll()" if we just want to roll the dice without recording the total immediately. However, I decided that it was a little clearer to leave this feature out.)

The main program is easy, especially since we've done the same problem before without using objects (in Exercise 3.1). Note how the pair of dice object is used. To test whether or not the total on the dice is 2, I use the test "while (dice.getTotal() != 2)". Because of the toString() method, I just have to say say

to show the numbers on the two dice.

The Solution

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote