package test1; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * A utility class that provides methods related to 6-sided dice. * */ public class DiceUtil { private DiceUtil() { // empty by design } /** * The number of sides on a standard die. */ public static final int SIDES = 6; /** * Returns the smallest number of dice required to roll the * given value. For example, 1 die is required to roll * a value between 1 and 6, 2 dice are required to roll * a value between 7 and 12, and 3 dice are require to roll * a value between 13 and 18. * * @param value a value * @return the smallest number of dice required to roll the given value * @pre. value is greater than zero */ public static int neededToRoll(int value) { if (value % SIDES == 0) { return value / SIDES; } return value / SIDES + 1; } /** * Given a list of dice sorted in ascending order, returns the * median value of the dice in the list. The median value is * defined as the value of the die in the middle of the list. * If the list has an even number of dice, then the value of * the last die in the front half of the list is returned. * For example: * *
* dice returns * ---------------------------- * [2, 2, 3] 2 * [3, 4, 5, 5] 4 * [1, 2, 3, 4, 4] 3 * [1, 1, 4, 5, 5, 6] 4 ** * @param dice a list of dice * @return the median value of the dice in the list * @throws IllegalArgumentException if the list of dice is empty */ public static int median(List
* dice returns * ---------------------------- * [] true * [3] true * [1, 4] true * [1, 2, 2] true * [3, 2] false * [1, 6, 4] false * [1, 2, 5, 3] false * [4, 4, 4, 4] true ** * @param dice a list of dice * @return true if a list of dice is sorted in ascending order, * and false otherwise */ public static boolean isSorted(List
* 5 dice form a full house if exactly 2 dice have the same value * (form a pair) and the remaining 3 dice have the same value * (form a three-of-a-kind) which is different than the value * of the pair. For example, the following lists are all full houses: * *
* [1, 1, 1, 5, 5] two 5s and three 1s * [4, 4, 4, 2, 2] two 2s and three 4s * [6, 3, 3, 6, 3] two 6s and three 3s ** *
* but the list: * *
* [2, 2, 2, 2, 2] two 2s and three 2s??? ** *
* is not considered a full house.
*
* @param dice a list of dice
* @return true if a list of exactly 5 dice is a full house, and
* false otherwise
* @pre. dice.size() == 5
*/
public static boolean isFullHouse(List