Advertisement
Guest User

Untitled

a guest
May 28th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4. The Die class simulates a six-sided die.
  5. */
  6.  
  7. public class Die
  8. {
  9. private int sides; // Number of sides
  10. private int value; // The die's value
  11.  
  12. /**
  13. The constructor performs an initial
  14. roll of the die.
  15. @param numSides The number of sides for this die.
  16. */
  17.  
  18. public Die(int numSides)
  19. {
  20. sides = numSides;
  21. roll();
  22. }
  23.  
  24. /**
  25. The roll method simlates the rolling of
  26. the die.
  27. */
  28.  
  29. public void roll()
  30. {
  31. // Create a Random object.
  32. Random rand = new Random();
  33.  
  34. // Get a random value for the die.
  35. value = rand.nextInt(sides) + 1;
  36. }
  37.  
  38. /**
  39. getSides method
  40. @return The number of sides for this die.
  41. */
  42.  
  43. public int getSides()
  44. {
  45. return sides;
  46. }
  47.  
  48. /**
  49. getValue method
  50. @return The value of the die.
  51. */
  52.  
  53. public int getValue()
  54. {
  55. return value;
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement