Dice are used in many games. One die can be thrown to randomly show a value from
ID: 669768 • Letter: D
Question
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 (from 1 to 6). Include a constructor that randomly assigns a value to a die object. 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. random Value = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE +LOWEST_DIE_VALUE);
Also include a method in the class to return a die’s value. Save the class as Die.java .Write an application that randomly “ throws ” two dice and displays their values. After you read the chapter Making Decisions , you will be able to have the game determine the higher die. For now, just observe how the values change as you execute the program multiple times. Save the application as TwoDice.java
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class TwoDice
{
private int value;
private int const HIGHEST_DIE_VALUE=6;
private int const LOWEST_DIE_VALUE = 1;
public TwoDice()
{
this.value = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE +LOWEST_DIE_VALUE);
}
public int getValue()
{
return this.value;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
TwoDice die1 = new TwoDice();
TwoDice die2 = new TwoDice();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.