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

Create a class called Dice, which has the roll () final instance method, which r

ID: 3777311 • Letter: C

Question

Create a class called Dice, which has the roll () final instance method, which returns a random int based 011 the number of sides a die has. Create a D4, D6, and D8 class, each of which is a child of the Dice class. Write a driver class that will allow two players to play some kind of dice game. This class should also contain a static method rollAllDice(Dice[] dice), which rolls all the dice in an array of dice, and returns their sum. You have a large amount of flexibility towards completing this assignment and roll() should be written in such a way as to not need to overridden in the subclasses.

Explanation / Answer

This game is a two player game where each player has to guess the sum of each dice roll The player whose guess is close wins that round Each player will get their chance one by one. At the end of 5 rounds, player with most wins wins the game. public class Dice { private int mNoOfFaces; protected Dice(int noOfFaces) { mNoOfFaces = noOfFaces; } public final int roll() { return (int) Math.ceil(Math.random() * mNoOfFaces); } } public class D4 extends Dice { public D4() { super(4); } } public class D6 extends Dice { public D6() { super(6); } } public class D8 extends Dice { public D8() { super(8); } } import java.util.Scanner; public class DiceDrive { public static void main(String[] args) { Dice[] dices = {new D4(), new D6(), new D8()}; int noOfRounds = 5; System.out.println("This game is a two player game where each player has to guess the sum of each dice roll " + "The player whose guess is close wins that round " + "Each player will get their chance one by one. " + "At the end of " + noOfRounds + " rounds, player with most wins wins the game. "); int noOfGamesPlayerOneWon = 0; int noOfGamesPlayerTwoWon = 0; for (int i = 0; i playerTwoDifference) { System.out.println("Player 1 is more precise!!"); noOfGamesPlayerOneWon++; } else if (playerTwoDifference > playerOneDifference) { System.out.println("Player 2 is more precise!!"); noOfGamesPlayerTwoWon++; } else System.out.println("Both the players are just superb!!"); } System.out.println("In a total of " + noOfRounds + " games, player 1 won " + noOfGamesPlayerOneWon + " games and player 2 won " + noOfGamesPlayerTwoWon + " games."); if (noOfGamesPlayerOneWon > noOfGamesPlayerTwoWon) System.out.println("So the winner is Player 1!!"); else if (noOfGamesPlayerTwoWon > noOfGamesPlayerOneWon) System.out.println("So the winner is Player 2!!"); else System.out.println("Its a Tie!!"); } private static int getSumAfterRolling(Dice[] dices) { int sum = 0; for (Dice dice : dices) sum += dice.roll(); return sum; } private static int getInput() { Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } }
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