using System;
public class Die {
private int numberOfEyes;
private Random randomNumberSupplier;
private const int maxNumberOfEyes = 6;
public Die(){
randomNumberSupplier = new Random(unchecked((int)DateTime.Now.Ticks));
numberOfEyes = NewTossHowManyEyes();
}
public void Toss(){
numberOfEyes = NewTossHowManyEyes();
}
private int NewTossHowManyEyes (){
return randomNumberSupplier.Next(1,maxNumberOfEyes + 1);
}
public int NumberOfEyes() {
return numberOfEyes;
}
public override String ToString(){
return String.Format("[{0}]", numberOfEyes);
}
} | |
An instance variable: The number of eyes.
A reference to a Random object.
A constant.
The constructor.
The Toss method which calls a private method.
A private method that tosses the die.
A method that returns the number of eyes
shown by the die in its current state.
A method that returns a string that
makes it possible to observe the die.
|