Java tutorial
/* * Copyright (c) 2011, Eric McIntyre * All rights reserved. * * This software is under the BSD license. * http://www.opensource.org/licenses/bsd-license.php */ package com.riversoforion.zambezi.dice; import java.util.Random; import org.apache.commons.lang3.builder.CompareToBuilder; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; /** * @author macdaddy */ public class Die implements Comparable<Die> { private static final int DEFAULT_SIDES = 6; private int sides; private int value; private Random randomizer = new Random(System.currentTimeMillis()); public Die() { this(DEFAULT_SIDES); } public Die(int sides) { this.sides = sides; } public int getSides() { return this.sides; } public int getValue() { return this.value; } public int roll() { value = randomizer.nextInt(sides) + 1; return this.value; } public int compareTo(Die o) { return new CompareToBuilder().append(sides, o.sides).append(value, o.value).toComparison(); } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null || !(obj instanceof Die)) return false; Die other = (Die) obj; return new EqualsBuilder().append(sides, other.sides).append(value, other.value).isEquals(); } @Override public int hashCode() { return new HashCodeBuilder().append(sides).append(value).toHashCode(); } @Override public String toString() { return new ToStringBuilder(this).append("# of sides", sides).append("value", value).build(); } }