Skip to content

Instantly share code, notes, and snippets.

@keplersj
Last active August 29, 2015 14:07
Show Gist options
  • Save keplersj/463cddb2debfafb5107b to your computer and use it in GitHub Desktop.
Save keplersj/463cddb2debfafb5107b to your computer and use it in GitHub Desktop.
Die Code Sample
import java.util.Random
/**
* Models a playing die with sides numbered 1 to N.
* All sides have uniform probablity of being rolled.
*
* @author Summer CS 307 class
*/
class Die
{ static final DEFAULT_SIDES = 6;
private static ourRandNumGen = new Random()
private final int iMyNumSides
private int iMyResult
/**
* Default constructor.<p>
* pre: none<br>
* post: getNumSides() = DEFAULT_SIDES, getResult() = 1
*/
public Die()
{ this(DEFAULT_SIDES)
}
/**
* Create a Die with numSides sides<p>
* pre: numSides > 1<br>
* post: getNumSides() = numSides, getResult() = 1<br>
* An exception will be generated if the preconditions are not met
*/
def Die(int numSides)
{ assert numSides > 1 : "Violation of precondition: numSides = " + numSides + "numSides must be greater than 1"
iMyNumSides = numSides
iMyResult = 1
assert getResult() == 1 && getNumSides() == numSides
}
/**
* Create a Die with numSides and top side and result set to result<p>
* pre: numSides > 1, 1 <= result <= numSides<br>
* post: getNumSides() = numSides, getResult() = 1<br>
* An exception will be generated if the preconditions are not met
*/
def Die(int numSides, int result)
{ assert numSides > 1 && 1 <= result && result <= numSides : "Violation of precondition"
iMyNumSides = numSides
iMyResult = result
}
/**
* roll this Die. Every side has an equal chance of being the new result<p>
* pre: none<br>
* post: 1 <= getResult() <= getNumSides()
* @return the result of the Die after the roll
*/
public int roll()
{ iMyResult = ourRandNumGen.nextInt(iMyNumSides) + 1
assert ( 1 <= getResult() ) && ( getResult() <= getNumSides() )
iMyResult
}
/**
* return how many sides this Die has<p>
* pre: none<br>
* post: return how many sides this Die has
* @return the number of sides on this Die
*/
def getNumSides()
{ iMyNumSides }
/**
* get the current result or top number of this Die<p>
* pre: none<br>
* post: return the number on top of this Die
* @return the current result of this Die
*/
def getResult()
{ iMyResult }
/**
* returns true if this Die and the parameter otherObj are equal<p>
* pre: none<br>
* post: return true if the parameter is a Die object with the same number of sides as this Die and currently has the same result.
* @return true if the the two Dice are equal, false otherwise
*/
def equals(Object otherObj)
{ result = true
if(otherObj == null)
result = false
else if(this == otherObj)
result = true
else if(this.getClass() != otherObj.getClass())
result = false
else
{ Die otherDie = (Die)otherObj
result = this.iMyResult == otherDie.iMyResult
&& this.iMyNumSides == otherDie.iMyNumSides
}
result
}
/**
* returns a String containing information about this Die<p>
* pre: none<br>
* post: return a String with information about the current state of this Die
* @return: A String with the number of sides and current result of this Die
*/
def toString(){
"Num sides " + getNumSides() + " result " + getResult()
}
}
import java.util.Random;
/**
* Models a playing die with sides numbered 1 to N.
* All sides have uniform probablity of being rolled.
*
* @author Summer CS 307 class
*/
public class Die
{ public static final int DEFAULT_SIDES = 6;
private static Random ourRandNumGen = new Random();
private final int iMyNumSides;
private int iMyResult;
/**
* Default constructor.<p>
* pre: none<br>
* post: getNumSides() = DEFAULT_SIDES, getResult() = 1
*/
public Die()
{ this(DEFAULT_SIDES);
}
/**
* Create a Die with numSides sides<p>
* pre: numSides > 1<br>
* post: getNumSides() = numSides, getResult() = 1<br>
* An exception will be generated if the preconditions are not met
*/
public Die(int numSides)
{ assert numSides > 1 : "Violation of precondition: numSides = " + numSides + "numSides must be greater than 1";
iMyNumSides = numSides;
iMyResult = 1;
assert getResult() == 1 && getNumSides() == numSides;
}
/**
* Create a Die with numSides and top side and result set to result<p>
* pre: numSides > 1, 1 <= result <= numSides<br>
* post: getNumSides() = numSides, getResult() = 1<br>
* An exception will be generated if the preconditions are not met
*/
public Die(int numSides, int result)
{ assert numSides > 1 && 1 <= result && result <= numSides : "Violation of precondition";
iMyNumSides = numSides;
iMyResult = result;
}
/**
* roll this Die. Every side has an equal chance of being the new result<p>
* pre: none<br>
* post: 1 <= getResult() <= getNumSides()
* @return the result of the Die after the roll
*/
public int roll()
{ iMyResult = ourRandNumGen.nextInt(iMyNumSides) + 1;
assert ( 1 <= getResult() ) && ( getResult() <= getNumSides() );
return iMyResult;
}
/**
* return how many sides this Die has<p>
* pre: none<br>
* post: return how many sides this Die has
* @return the number of sides on this Die
*/
public int getNumSides()
{ return iMyNumSides; }
/**
* get the current result or top number of this Die<p>
* pre: none<br>
* post: return the number on top of this Die
* @return the current result of this Die
*/
public int getResult()
{ return iMyResult; }
/**
* returns true if this Die and the parameter otherObj are equal<p>
* pre: none<br>
* post: return true if the parameter is a Die object with the same number of sides as this Die and currently has the same result.
* @return true if the the two Dice are equal, false otherwise
*/
public boolean equals(Object otherObj)
{ boolean result = true;
if(otherObj == null)
result = false;
else if(this == otherObj)
result = true;
else if(this.getClass() != otherObj.getClass())
result = false;
else
{ Die otherDie = (Die)otherObj;
result = this.iMyResult == otherDie.iMyResult
&& this.iMyNumSides == otherDie.iMyNumSides;
}
return result;
}
/**
* returns a String containing information about this Die<p>
* pre: none<br>
* post: return a String with information about the current state of this Die
* @return: A String with the number of sides and current result of this Die
*/
public String toString()
{ return "Num sides " + getNumSides() + " result " + getResult();
}
}// end of Die class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment