I’m learning Java at work, and the exercise we’re supposed to do states the following:
Create a class representing a die. Create a method to roll the die (random number from 1 to 6)
Also override the equals and toString methods provided by the Object class.
Coming straight from C++ with no Java experience, I think the first part is relatively straightforward. However, I’m not sure how to override equals and toString methods?
Here is my code so far, any advice would be greatly appreciated:
package this;
import java.lang.Object;
public class Die
{
public static void main(String[] args)
{
int die;
die = (int)(Math.random()*6 + 1);
System.out.println (die);
}
}
A Die instance should represent a die. The Die class should not be a procedural application that launches a die.
A Die has a state, which is its current face value (1 to 6). Rolling it should make it go from its current face value to another one.
Its
toString()method could say that it’s a die, and say its current face value. I don’t really see the point in overriding equals(), because I don’t see why a die should ever be equal to another die. But you could choose to make two dies equal if they have the same face value.