/**
 * Represents a dice
 */

public class Dice {
  private int nSides;
  private int value;
  
  /**
   * Constructs a standard Dice
   */
  public Dice() {
    this(6);         // Refers to the constructor below
  }
  
  /**
   * Constructs a dice with a specified number of sides
   * @param nSides number of sides
   */
  public Dice(int nSides) {
    if (nSides<2) {
      System.out.println("Wrong numnber of sides.");
      this.nSides = 6;
    } 
    else {
      this.nSides = nSides;
    }
    this.roll();
  }
  
  /**
   * Gets the current value of the dice
   * @return the current value
   */
  public int getValue() {
    return this.value;
  }
  
  /**
   * Rolls the dice. Updates its value.
   */
  public int roll() {
    this.value = (int)(Math.random()*this.nSides) + 1;
    return this.value;
  } 
  
  public String toString() {
    return "Dice(" + this.nSides + ", " + this.value + ")";
  }
  
  /**
   * Demonstration class
   */
  public static void main(String[] args) {
    Dice d6 = new Dice();
    Dice d2 = new Dice(2);
    Dice d47 = new Dice(47);
    for (int i = 0; i<10; i++) {
      System.out.println(d6.roll() + "\t" + 
                         d2.roll() + "\t" + 
                         d47.roll());
    }
    
    System.out.println(d6);
    System.out.println(d2);
    System.out.println(d47);
  }     
}