import java.util.Random;
public class Die
{
int value;
public Die()
{
int value;
}
public int roll()
{
Random rand = new Random();
return value = rand.nextInt(6) + 1;
}
public int getCurrentValue()
{
return value;
}
public int compareTo(int die1, int die2)
{
if (die1 > die2)
return 1;
if (die1 < die2)
return -1;
else
return 0;
}
}
public class DieTester
{
public static void main(String[] args)
{
Die die1 = new Die();
Die die2 = new Die();
for (int x = 1; x <= 100; x++)
{
System.out.println("Trial " + x + ":");
int roll1 = die1.roll();
System.out.println("Value of die1: " + roll1);
int roll2 = die2.roll();
System.out.println("Value of die2: " + roll2);
int compare = die1.compareTo(roll1, roll2);
System.out.println("compareTo Result: " + compare);
if (compare == 1)
System.out.println("Die 1 is larger than Die 2. \n \n");
if (compare == -1)
System.out.println("Die 1 is smaller than Die 2. \n \n");
if (compare == 0)
System.out.println("Die 1 is equal to Die 2. \n \n");
}
}
}
{
public static void main(String[] args)
{
Die die1 = new Die();
Die die2 = new Die();
for (int x = 1; x <= 100; x++)
{
System.out.println("Trial " + x + ":");
int roll1 = die1.roll();
System.out.println("Value of die1: " + roll1);
int roll2 = die2.roll();
System.out.println("Value of die2: " + roll2);
int compare = die1.compareTo(roll1, roll2);
System.out.println("compareTo Result: " + compare);
if (compare == 1)
System.out.println("Die 1 is larger than Die 2. \n \n");
if (compare == -1)
System.out.println("Die 1 is smaller than Die 2. \n \n");
if (compare == 0)
System.out.println("Die 1 is equal to Die 2. \n \n");
}
}
}
−Phoenix
Hi Phoenix,
ReplyDeleteConsider:
public class Die {
....
}
Your Die class does not implement the Comparable interface. Revise when able.
To implement the “Comparable” interface, the “Die” class must respectively replace its second line and “compareTo” method with the following:
ReplyDeletepublic class Die implements Comparable
public int compareTo(Die other)
{
if (value > other.getCurrentValue())
return 1;
if (value < other.getCurrentValue())
return -1;
else
return 0;
}
Next, the “DieTester” class must have its fourteenth line of code replaced with the following as well:
int compare = die1.compareTo(die2);
−Phoenix
Hi Phoenix,
ReplyDeleteCan you combine everything together into one Die class that I can copy, paste, and test? Thanks.
Here is the code for the “Die” class:
ReplyDeleteimport java.util.Random;
public class Die implements Comparable
{
int value;
public Die()
{
int value;
}
public int roll()
{
Random rand = new Random();
return value = rand.nextInt(6) + 1;
}
public int getCurrentValue()
{
return value;
}
public int compareTo(Die other)
{
if (value > other.getCurrentValue())
return 1;
if (value < other.getCurrentValue())
return -1;
else
return 0;
}
}
And here is the code for the “DieTester” class:
public class DieTester
{
public static void main(String[] args)
{
Die die1 = new Die();
Die die2 = new Die();
for (int x = 1; x <= 100; x++)
{
System.out.println("Trial " + x + ":");
int roll1 = die1.roll();
System.out.println("Value of die1: " + roll1);
int roll2 = die2.roll();
System.out.println("Value of die2: " + roll2);
int compare = die1.compareTo(die2);
System.out.println("compareTo Result: " + compare);
if (compare == 1)
System.out.println("Die 1 is larger than Die 2. \n \n");
if (compare == -1)
System.out.println("Die 1 is smaller than Die 2. \n \n");
if (compare == 0)
System.out.println("Die 1 is equal to Die 2. \n \n");
}
}
}
−Phoenix
Awesome, thank you. 950 XPs awarded for this completion.
ReplyDelete