Examples of smaller Java programs that do various interesting things.

Die.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import java.util.Random;
  2. /**
  3. * Models a playing die with sides numbered 1 to N.
  4. * All sides have uniform probablity of being rolled.
  5. *
  6. * @author Summer CS 307 class
  7. */
  8. public class Die
  9. { public static final int DEFAULT_SIDES = 6;
  10. private static Random ourRandNumGen = new Random();
  11. private final int iMyNumSides;
  12. private int iMyResult;
  13. /**
  14. * Default constructor.<p>
  15. * pre: none<br>
  16. * post: getNumSides() = DEFAULT_SIDES, getResult() = 1
  17. */
  18. public Die()
  19. { this(DEFAULT_SIDES);
  20. }
  21. /**
  22. * Create a Die with numSides sides<p>
  23. * pre: numSides > 1<br>
  24. * post: getNumSides() = numSides, getResult() = 1<br>
  25. * An exception will be generated if the preconditions are not met
  26. */
  27. public Die(int numSides)
  28. { assert numSides > 1 : "Violation of precondition: numSides = " + numSides + "numSides must be greater than 1";
  29. iMyNumSides = numSides;
  30. iMyResult = 1;
  31. assert getResult() == 1 && getNumSides() == numSides;
  32. }
  33. /**
  34. * Create a Die with numSides and top side and result set to result<p>
  35. * pre: numSides > 1, 1 <= result <= numSides<br>
  36. * post: getNumSides() = numSides, getResult() = 1<br>
  37. * An exception will be generated if the preconditions are not met
  38. */
  39. public Die(int numSides, int result)
  40. { assert numSides > 1 && 1 <= result && result <= numSides : "Violation of precondition";
  41. iMyNumSides = numSides;
  42. iMyResult = result;
  43. }
  44. /**
  45. * roll this Die. Every side has an equal chance of being the new result<p>
  46. * pre: none<br>
  47. * post: 1 <= getResult() <= getNumSides()
  48. * @return the result of the Die after the roll
  49. */
  50. public int roll()
  51. { iMyResult = ourRandNumGen.nextInt(iMyNumSides) + 1;
  52. assert ( 1 <= getResult() ) && ( getResult() <= getNumSides() );
  53. return iMyResult;
  54. }
  55. /**
  56. * return how many sides this Die has<p>
  57. * pre: none<br>
  58. * post: return how many sides this Die has
  59. * @return the number of sides on this Die
  60. */
  61. public int getNumSides()
  62. { return iMyNumSides; }
  63. /**
  64. * get the current result or top number of this Die<p>
  65. * pre: none<br>
  66. * post: return the number on top of this Die
  67. * @return the current result of this Die
  68. */
  69. public int getResult()
  70. { return iMyResult; }
  71. /**
  72. * returns true if this Die and the parameter otherObj are equal<p>
  73. * pre: none<br>
  74. * 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.
  75. * @return true if the the two Dice are equal, false otherwise
  76. */
  77. public boolean equals(Object otherObj)
  78. { boolean result = true;
  79. if(otherObj == null)
  80. result = false;
  81. else if(this == otherObj)
  82. result = true;
  83. else if(this.getClass() != otherObj.getClass())
  84. result = false;
  85. else
  86. { Die otherDie = (Die)otherObj;
  87. result = this.iMyResult == otherDie.iMyResult
  88. && this.iMyNumSides == otherDie.iMyNumSides;
  89. }
  90. return result;
  91. }
  92. /**
  93. * returns a String containing information about this Die<p>
  94. * pre: none<br>
  95. * post: return a String with information about the current state of this Die
  96. * @return: A String with the number of sides and current result of this Die
  97. */
  98. public String toString()
  99. { return "Num sides " + getNumSides() + " result " + getResult();
  100. }
  101. }// end of Die class