Question: *******Java********** Yahtzee.java This class represents the Yahtzee game in which there are 5 rolled dice and the scoring is performed. This class is much longer
*******Java**********
Yahtzee.java
This class represents the Yahtzee game in which there are 5 rolled dice and the scoring is performed. This class is much longer and more complex than the Dice class.
The class must have the following private variables:
- dice (Dice[ ])
The class must have the following public methods:
- public Yahtzee() [constructor]
- Initialize the dice array and roll them for random values
- public Yahtzee(Dice[ ]) [constructor]
- Initialize the dice array with the given argument
- public int[ ] getValueCount()
- Count how many dice show each of the possible values from 1-6 and record all of them in an int array. Return this array with the 6 counters.
- Entry 0 in this array is for the number of ones, entry 1 in the array is for the number of twos, and so on.
- i.e. if the 5 dice are: {1, 4, 2, 1, 5}, this method should return the array:
[2, 1, 0, 1, 1, 0] because there are 2 ones, 1 two, 0 threes, 1 four, 1 five, and 0 sixes.
- public int[ ] getScoreOptions()
- Create an int array with 13 elements, to record all the possible scores for the dice in the instance variable. These 13 scores must be ordered exactly as described in the introduction (beginning with the "Sum of 1s" in index 0 and ending with the "Yahtzee" (5 of a Kind) in index 12).
- Hint: you may want to create one or more private helper methods that can be called from this method. You also may want to use the getValueCount() method.
- public int[ ] score()
- Call getScoreOptions() and then determine the maximum value from the array of possible scores, and the index at which the maximum is found (if the maximum value appears more than once, find the smallest index where the maximum value appears)
- Return an int array containing 2 values: the maximum value and then the corresponding index of that value
- public boolean equals(Yahtzee)
- Compare the given Yahtzee object from the argument with the "this" object to see if they are equal. Consider equality to be the same 5 dice but in any order.
- i.e. The dice: {2, 6, 5, 1, 2} is considered equal to the dice: {5, 1, 2, 2, 6}.
- public String toString()
- Return a string of the dice values formatted this way: "Dice: {3, 5, 1, 1, 2}"
Here are classes:
import java.util.Random;
public class RandomNumber { private static int seed = 123456789; private static Random rnd = new Random(seed); /** * Generate a random integer between min and max (inclusive) * @param min: the minimum value in the range for the random number (inclusive) * @param max: the maximum value in the range for the random number (inclusive) * @return random integer within the range min-max (inclusive) */ public static int getRandomNumber (int min, int max) { int num = rnd.nextInt(max-min+1) + min; return num; } } =================================
public class Dice { public int value; public Dice() { value = -1; } public Dice(int value) { this.value = value; } public void roll() { RandomNumber R = new RandomNumber(); value = R.getRandomNumber(1, 6); } public int getValue() { return value; } } ================================
public class TestGame {
public static void main(String[] args) { Yahtzee game1, game2, game3; game1 = new Yahtzee(); game2 = new Yahtzee(new Dice[] {new Dice(2), new Dice(5), new Dice(3), new Dice(5), new Dice(6)}); game3 = new Yahtzee(new Dice[] {new Dice(6), new Dice(1), new Dice(1), new Dice(5), new Dice(2)});
// Test 1 - constructors and toString if (game1.toString().equals("Dice: {2, 1, 6, 5, 1}")) { System.out.println("Test 1 Passed"); } else { System.out.println("Test 1 Failed"); }
// Test 2 - constructors and toString if (game2.toString().equals("Dice: {2, 5, 3, 5, 6}")) { System.out.println("Test 2 Passed"); } else { System.out.println("Test 2 Failed"); } // Test 3 - equals
if (!game1.equals(game2)) { System.out.println("Test 3 Passed"); } else { System.out.println("Test 3 Failed"); } // Test 4 - equals if (game1.equals(game3)) { System.out.println("Test 4 Passed"); } else { System.out.println("Test 4 Failed"); } // Test 5 - getValueCount
if (showArray(game1.getValueCount()).equals("[2, 1, 0, 0, 1, 1]")) { System.out.println("Test 5 Passed"); } else { System.out.println("Test 5 Failed"); } // Test 6 - getValueCount game3 = new Yahtzee(); if (showArray(game3.getValueCount()).equals("[0, 2, 0, 1, 1, 1]")) { System.out.println("Test 6 Passed"); } else { System.out.println("Test 6 Failed"); } // Test 7 - getScoreOptions game1 = new Yahtzee(new Dice[] {new Dice(5), new Dice(3), new Dice(5), new Dice(5), new Dice(4)}); if (showArray(game1.getScoreOptions()).equals("[0, 0, 3, 4, 15, 0, 22, 0, 0, 0, 0, 0, 22]")) { System.out.println("Test 7 Passed"); } else { System.out.println("Test 7 Failed"); } // Test 8 - getScoreOptions game1 = new Yahtzee(new Dice[] {new Dice(4), new Dice(4), new Dice(4), new Dice(4), new Dice(4)}); if (showArray(game1.getScoreOptions()).equals("[0, 0, 0, 20, 0, 0, 20, 20, 0, 0, 0, 50, 20]")) { System.out.println("Test 8 Passed"); } else { System.out.println("Test 8 Failed"); } // Test 9 - getScoreOptions game2 = new Yahtzee(new Dice[] {new Dice(1), new Dice(3), new Dice(1), new Dice(3), new Dice(3)}); if (showArray(game2.getScoreOptions()).equals("[2, 0, 9, 0, 0, 0, 11, 0, 25, 0, 0, 0, 11]")) { System.out.println("Test 9 Passed"); } else { System.out.println("Test 9 Failed"); }
// Test 10 - getScoreOptions game3 = new Yahtzee(new Dice[] {new Dice(4), new Dice(5), new Dice(5), new Dice(6), new Dice(3)}); if (showArray(game3.getScoreOptions()).equals("[0, 0, 3, 4, 10, 6, 0, 0, 0, 30, 0, 0, 23]")) { System.out.println("Test 10 Passed"); } else { System.out.println("Test 10 Failed"); } // Test 11 - score game2 = new Yahtzee(); if (showArray(game2.score()).equals("[21, 12]")) { System.out.println("Test 11 Passed"); } else { System.out.println("Test 11 Failed"); } // Test 12 - score game3 = new Yahtzee(new Dice[] {new Dice(4), new Dice(5), new Dice(1), new Dice(2), new Dice(3)}); if (showArray(game3.score()).equals("[40, 10]")) { System.out.println("Test 12 Passed"); } else { System.out.println("Test 12 Failed"); } // Test 13 - score for (int i = 0; i < 4; i++) game3 = new Yahtzee(); if (showArray(game3.score()).equals("[15, 6]")) { System.out.println("Test 13 Passed"); } else { System.out.println("Test 13 Failed"); } } public static String showArray (int[] arr) { String s = "["; for (int i = 0; i < arr.length; i++) { s += arr[i]; if (i < arr.length - 1) s += ", "; } s += "]"; return s; }
} =================================
Please do the Yahtzee.class and make sure that it can pass all tests in TestGame.class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
