Advertisement
Crenox

Die Java Program

Sep 17th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. // By: Sammy Samkough
  2. // Die
  3. // To make a die roll
  4.  
  5. import java.util.Random;
  6.  
  7. public class Die
  8. {
  9. private static Random r = new Random();
  10.  
  11. private int numFaces;
  12. private int faceValue;
  13.  
  14. public Die()
  15. {
  16. numFaces = 6;
  17. faceValue = 1;
  18. }
  19.  
  20. public Die(int sides)
  21. {
  22. numFaces = sides;
  23. faceValue = 1;
  24. }
  25.  
  26. public void roll()
  27. {
  28. faceValue = (int)(Math.random() * numFaces) + 1;
  29. }
  30.  
  31. // get results of rolled die
  32. public int getFaceValue()
  33. {
  34. return faceValue;
  35. }
  36.  
  37. public String toString()
  38. {
  39. // we put strings since faceValue is an int
  40. return "" + faceValue;
  41. }
  42. }
  43. -------------------------------------------------------------------------------------------------------------------------------
  44. // By: Sammy Samkough
  45. // Die
  46. // To make a die roll
  47.  
  48. public class DieMain
  49. {
  50. public static void main(String args[])
  51. {
  52. // create
  53. Die d1 = new Die();
  54. Die d2 = new Die(6);
  55.  
  56. int sum = 0;
  57.  
  58. // roll
  59. d1.roll();
  60. d2.roll();
  61.  
  62. // get results
  63. d1.getFaceValue();
  64. d2.getFaceValue();
  65.  
  66. sum = d1.getFaceValue() + d2.getFaceValue();
  67.  
  68. // print out
  69. System.out.println("D1 = " + d1);
  70. System.out.println("D2 = " + d2);
  71. System.out.println("Sum = " + sum);
  72. }
  73. }
  74.  
  75. /*
  76. D1 = 5
  77. D2 = 1
  78. Sum = 6
  79. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement