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
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
$ java Hog **************************************** Current Status: Computer: 0 You: 0 Die 1: 2 Die 2: 1 Busted!!! **************************************** Current Status: Computer: 0 You: 0 Die 1: 4 Die 2: 6 Current Round: 10 Potential Total: 10 Take another turn (y/n)? y Die 1: 5 Die 2: 5 Current Round: 20 Potential Total: 20 Take another turn (y/n)? y Die 1: 5 Die 2: 5 Current Round: 30 Potential Total: 30 Take another turn (y/n)? y Die 1: 2 Die 2: 2 Current Round: 34 Potential Total: 34 Take another turn (y/n)? y Die 1: 4 Die 2: 1 Busted!!! **************************************** Current Status: Computer: 0 You: 0 Die 1: 1 Die 2: 5 Busted!!! **************************************** Current Status: Computer: 0 You: 0 Die 1: 3 Die 2: 4 Current Round: 7 Potential Total: 7 Take another turn (y/n)? y Die 1: 4 Die 2: 4 Current Round: 15 Potential Total: 15 Take another turn (y/n)? y Die 1: 1 Die 2: 2 Busted!!! **************************************** Current Status: Computer: 0 You: 0 Die 1: 5 Die 2: 5 Current Round: 10 Potential Total: 10 Die 1: 5 Die 2: 5 Current Round: 20 Potential Total: 20 **************************************** Current Status: Computer: 20 You: 0 Die 1: 3 Die 2: 2 Current Round: 5 Potential Total: 5 Take another turn (y/n)? y Die 1: 2 Die 2: 2 Current Round: 9 Potential Total: 9 Take another turn (y/n)? y Die 1: 5 Die 2: 3 Current Round: 17 Potential Total: 17 Take another turn (y/n)? y Die 1: 5 Die 2: 4 Current Round: 26 Potential Total: 26 Take another turn (y/n)? y Die 1: 4 Die 2: 3 Current Round: 33 Potential Total: 33 Take another turn (y/n)? y Die 1: 5 Die 2: 3 Current Round: 41 Potential Total: 41 Take another turn (y/n)? y Die 1: 5 Die 2: 1 Busted!!! **************************************** Current Status: Computer: 20 You: 0 Die 1: 3 Die 2: 5 Current Round: 8 Potential Total: 28 Die 1: 3 Die 2: 6 Current Round: 17 Potential Total: 37 Die 1: 5 Die 2: 6 Current Round: 28 Potential Total: 48 **************************************** Current Status: Computer: 48 You: 0 Die 1: 3 Die 2: 2 Current Round: 5 Potential Total: 5 Take another turn (y/n)? y Die 1: 3 Die 2: 6 Current Round: 14 Potential Total: 14 Take another turn (y/n)? y Die 1: 1 Die 2: 4 Busted!!! **************************************** Current Status: Computer: 48 You: 0 Die 1: 3 Die 2: 5 Current Round: 8 Potential Total: 56 The computer has won! Final Results: Computer: 56 You: 0
public class Die { private final int MAX = 6; private int faceValue; public Die() { faceValue = 1; } public int roll() { faceValue = (int)(Math.random() * MAX) + 1;
return faceValue; }
public void setFaceValue (int value) { faceValue = value; }
public int getFaceValue() { return faceValue; }
public String toString() { String result = Integer.toString(faceValue);
return result; } }
what is the code for this output ???
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
