Create a GameDie class that can be used to represent a die with any positive num
ID: 3633790 • Letter: C
Question
Create a GameDie class that can be used to represent a die with any positive number of sides (within reason). It should include two constructors – one that accepts an int parameter that indicates the number of sides, and another with no parameter that will create a standard six-sided die. Here’s a simple driver and what the output might look like.…
GameDie die1 = new GameDie(26);
GameDie die2 = new GameDie();
die1.roll(); die2.roll();
System.out.println (die1);
System.out.println (die2);
… Output will vary every run but could
look like any of these columns
7 23 15 6 17 26 1
4 2 6 6 1 3 1
Explanation / Answer
please rate - thanks
public class GameDie
{
private int max;
private int value;
public GameDie()
{
max=6;
}
public GameDie(int m)
{
max=m;
}
public void roll()
{value = (int)(Math.random() * max) +1;
}
public String toString()
{return " "+value;
}
public static void main(String[] args)
{GameDie die1 = new GameDie(26);
GameDie die2 = new GameDie();
die1.roll(); die2.roll();
System.out.println (die1);
System.out.println (die2);
}
}