This is a Two Part Java assignment. Here is the First Part: Dice are used in man
ID: 3567769 • Letter: T
Question
This is a Two Part Java assignment.
Here is the First Part:
Dice are used in many games. One die can be thrown to randomly show a value from 1
through 6.Design a Die class that can hold an integer data field for a value (from1 to 6).
Include a constructor that randomly assigns a value to a die object. Appendix D
contains information on generating randomnumbers. To fully understand the process,
you must learn more about Java classes and methods. However, for now, you can copy
the following statement to generate a random number between 1 and 6 and assign it to
a variable. Using this statement assumes you have assigned appropriate values to the
static constants.
randomValue = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE +
LOWEST_DIE_VALUE);
Also include a method in the class to return a die
Explanation / Answer
public class FiveDice { private static final int LOWEST_DIE_VALUE = 1; private static final int HIGHEST_DIE_VALUE = 6; public int getDieValue() { int roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE); return roll; } public static void main(String[] args) { FiveDice roller = new FiveDice(); int compRoll1 = roller.getDieValue(); int compRoll2 = roller.getDieValue(); int compRoll3 = roller.getDieValue(); int compRoll4 = roller.getDieValue(); int compRoll5 = roller.getDieValue(); int compRoll6 = roller.getDieValue(); int playRoll1 = roller.getDieValue(); int playRoll2 = roller.getDieValue(); int playRoll3 = roller.getDieValue(); int playRoll4 = roller.getDieValue(); int playRoll5 = roller.getDieValue(); System.out.println("Results are: Computer"); System.out.println("Die 1 rolled a " + compRoll1); System.out.println("Die 2 rolled a " + compRoll2); System.out.println("Die 3 rolled a " + compRoll3); System.out.println("Die 4 rolled a " + compRoll4); System.out.println("Die 5 rolled a " + compRoll5); System.out.println("Player: Die 1 rolled a " + playRoll1 + " Die 2 rolled a " + playRoll2 + " Die 3 rolled a " + playRoll3 + " Die 4 rolled a " + playRoll4 + " Die 5 rolled a " + playRoll5); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.