Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Allison Finley
  2. This program will */
  3.  
  4. //import the scanner so inputs can be read from the keyboard
  5. import java.util.Scanner;
  6.  
  7. public class DiceGame
  8. {
  9. int numSides;
  10. String name;
  11. int totalSides;
  12. int money;
  13. double avgMoney;
  14.  
  15. // this is to run the class with the functions of both.
  16. public static void main(String[] args)
  17. {
  18.  
  19. //this creates an object of the class to use its methods
  20. DiceGame test = new DiceGame();
  21. test.readInput();
  22. test.output();
  23. test.numRolls();
  24.  
  25. }
  26.  
  27. //to ask the user for the number of sides on the second die
  28. //and their name
  29. public void readInput()
  30. {
  31. Scanner keyboard = new Scanner(System.in);
  32. System.out.print("Please enter the number of sides on the second die: ");
  33. numSides = keyboard.nextInt();
  34. keyboard.nextLine(); // Clearing the input stream.
  35. System.out.println("Please enter your name (minimum 5 characters): ");
  36. name = keyboard.nextLine();
  37. int len = name.length();
  38. while (len < 5)
  39. {
  40. System.out.println("Name must be at least "
  41. + "5 characters long. Please reenter: ");
  42. name = keyboard.nextLine();
  43. len = name.length();
  44. }
  45. }
  46. public void numRolls()
  47. {
  48. //this creates a new object for the first die.
  49. Die die1 = new Die();
  50.  
  51. //this creates a new die object for the second die.
  52. Die die2 = new Die(numSides);
  53.  
  54. int rollNum;
  55. for (rollNum = 0; rollNum <= 50000; rollNum++)
  56.  
  57. {
  58. //counts the total sides of both die so calculate the money exchange.
  59. totalSides = die1.roll() + die2.roll();
  60. System.out.println("this is the total fo both sides " + totalSides);
  61.  
  62. // this will take the number of sides and calculate the money
  63. if (totalSides < 5)
  64. {
  65. money += 2;
  66. }
  67. else if (totalSides == 5)
  68. {
  69. money += 8;
  70. }
  71. else
  72. {
  73. money -= 3;
  74. }
  75.  
  76. //this will calculate the average money per roll
  77. avgMoney = money/rollNum;
  78.  
  79. if (rollNum % 5000 == 0)
  80. System.out.printf("%-20i%-20i%d\n", rollNum, money, avgMoney);
  81.  
  82. }
  83. }
  84. public void output()
  85. {
  86. System.out.println("Experiment by: " + name.substring(0,6));
  87. System.out.println();
  88.  
  89. }
  90. }
  91.  
  92. //this is his code
  93. //This class represents a Die (half a set of dice)
  94.  
  95. import java.util.*; //for Random
  96.  
  97. public class Die
  98. {
  99. //-------- data
  100. private int numSides;
  101.  
  102. //-------- constructors
  103. //default constructor - sets the number of sides to 6
  104. public Die()
  105. {
  106. numSides = 6;
  107. }
  108.  
  109. //parameterized constructor - sets the number of sides to whatever is passed in. Throws an
  110. // exception if number of sides if < 4
  111. public Die(int newSides)
  112. {
  113. if (newSides < 4)
  114. throw new IllegalArgumentException("number of sides cannot be less than 4");
  115. this.numSides = newSides;
  116. }
  117.  
  118. //-------- methods
  119. //roll - rolls the Die by returning a random number between 1 and numSides
  120. public int roll()
  121. {
  122. Random generator = new Random();
  123. return generator.nextInt(numSides)+1; //generator.nextInt(numSides) returns random in range 0 to numSides-1
  124. }
  125.  
  126. //getNumSides - returns the number of sides
  127. public int getNumSides()
  128. {
  129. return numSides;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement