Advertisement
Crenox

SevenEleven Java Program

Sep 17th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. // By: Sammy Samkough
  2. // 7s & 11s
  3. // Counts number of times you roll a sum of 7 or 11
  4.  
  5. import java.util.Random;
  6.  
  7. public class SevenEleven
  8. {
  9. private static Random r = new Random();
  10.  
  11. private int numFaces;
  12. private int faceValue;
  13.  
  14. public SevenEleven()
  15. {
  16. numFaces = 6;
  17. faceValue = 1;
  18. }
  19.  
  20. public SevenEleven(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. // 7s & 11s
  46. // Counts number of times you roll a sum of 7 or 11
  47.  
  48. public class SevenElevenMain
  49. {
  50. public static void main(String args[])
  51. {
  52. // create
  53. SevenEleven d1 = new SevenEleven();
  54. SevenEleven d2 = new SevenEleven(6);
  55.  
  56. int sum = 0;
  57.  
  58. int x = 0, y = 0;
  59.  
  60. // rolls 5000 times
  61. for (int i = 0; i < 5000; i++)
  62. {
  63. // rolls
  64. d1.roll();
  65. d2.roll();
  66.  
  67. // get results
  68. d1.getFaceValue();
  69. d2.getFaceValue();
  70.  
  71. sum = d1.getFaceValue() + d2.getFaceValue();
  72.  
  73. if (sum == 7)
  74. {
  75. x++;
  76. }
  77.  
  78. if (sum == 11)
  79. {
  80. y++;
  81. }
  82.  
  83. }
  84.  
  85. // print out
  86. System.out.println("D1 = " + d1);
  87. System.out.println("D2 = " + d2);
  88. System.out.println("Sum = " + sum);
  89. System.out.println("The expected rolls for 7 were 833 but it actually gets called " + x + " time(s).");
  90. System.out.println("The expected rolls for 11 were 278 but it actually gets called " + y + " time(s).");
  91. }
  92. }
  93.  
  94. /*
  95. D1 = 6
  96. D2 = 5
  97. Sum = 11
  98. The expected rolls for 7 were 833 but it actually gets called 825 time(s).
  99. The expected rolls for 11 were 278 but it actually gets called 260 time(s).
  100. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement