3. Classes (40 points). 20 points for correctness, 20 for following the coding g
ID: 3884241 • Letter: 3
Question
3. Classes (40 points). 20 points for correctness, 20 for following the coding guidelines. Design and implement a class called Die (for a die in a pair of dice). Include a constructor for a die (which will set its value as null or -1), methods to set and get the individual die values, and a method to print the die. For instance, a 5 will print as * * * * * Create a driver class called CreatePairOfDice to instantiate and print a pair of dice. The value of each die should be a randomly generated integer from 1 to 6. You must use Math.random().
Explanation / Answer
class Die {
private int value;
public Die() {
value = -1;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void display()
{
int value=this.getValue();
for(int i=0;i<value;i++)
System.out.print("*");
System.out.println();
}
}
public class CreatePairOfDice {
public static void main(String[] args) {
// TODO Auto-generated method stub
Die die1=new Die();
Die die2=new Die();
die1.setValue((int)(6.0 * Math.random()) + 1);
die2.setValue((int)(6.0 * Math.random()) + 1);
die1.display();
die2.display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.