Advertisement
Guest User

minilabUsingClasses

a guest
Sep 2nd, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. // this is my class for the die and the rolls
  2. import java.util.Scanner;
  3.  
  4. public class DiceGame
  5. {
  6. int numSides;
  7. String name;
  8. int totalSides;
  9.  
  10. public static void main(String[] args)
  11. {
  12. Die die1 = new Die();
  13.  
  14. Die die2;
  15.  
  16.  
  17. DiceGame test = new DiceGame();
  18. test.readInput();
  19. test.output();
  20. test.numRolls();
  21.  
  22. }
  23.  
  24. //to ask the user for the number of sides on the second die
  25. //and their name and stores the responses in the variable names.
  26. public void readInput()
  27. {
  28. Scanner keyboard = new Scanner(System.in);
  29. System.out.print("Please enter the number of sides on the second die: ");
  30. numSides = keyboard.nextInt();
  31. keyboard.nextLine(); // Clearing the input stream.
  32. System.out.println("Please enter your name (minimum 5 characters): ");
  33. name = keyboard.nextLine();
  34. int len = name.length();
  35. while (len < 5)
  36. {
  37. System.out.println("Name must be at least "
  38. + "5 characters long. Please reenter: ");
  39. name = keyboard.nextLine();
  40. len = name.length();
  41. }
  42. }
  43. //this is to count the number of rolls and print every 5000 rolls
  44. // Im not sure what is missing here
  45. public void numRolls()
  46. {
  47. int rollNum;
  48. for (rollNum = 0; rollNum <= 50000; rollNum++)
  49.  
  50.  
  51. {
  52. if (rollNum % 5000 == 0)
  53. System.out.println(rollNum);
  54.  
  55. }
  56. }
  57.  
  58. // this will out put the persons name and the column names(this was not needed i just wanted it to be more organized)
  59. public void output()
  60. {
  61. System.out.println("Experiment by: " + name.substring(0,6));
  62. System.out.println();
  63. System.out.printf("%-20s%-20s%s\n", "Number of Rolls", "Money Made", "Average Money Made");
  64. }
  65.  
  66. // this will calculate the money for each roll
  67. // there is something wrong here too im just not sure what.
  68. public void money()
  69. {
  70. int money = 0;
  71. if (totalSides < 5)
  72. {
  73. money += 2;
  74. }
  75. else if (totalSides == 5);
  76. {
  77. money += 8;
  78. }
  79. else
  80. {
  81. money -= 3;
  82. }
  83. }
  84. }
  85.  
  86. //this is his code
  87. //This class represents a Die (half a set of dice)
  88.  
  89. import java.util.*; //for Random
  90.  
  91. public class Die
  92. {
  93. //-------- data
  94. private int numSides;
  95.  
  96. //-------- constructors
  97. //default constructor - sets the number of sides to 6
  98. public Die()
  99. {
  100. numSides = 6;
  101. }
  102.  
  103. //parameterized constructor - sets the number of sides to whatever is passed in. Throws an
  104. // exception if number of sides if < 4
  105. public Die(int newSides)
  106. {
  107. if (newSides < 4)
  108. throw new IllegalArgumentException("number of sides cannot be less than 4");
  109. this.numSides = newSides;
  110. }
  111.  
  112. //-------- methods
  113. //roll - rolls the Die by returning a random number between 1 and numSides
  114. public int roll()
  115. {
  116. Random generator = new Random();
  117. return generator.nextInt(numSides)+1; //generator.nextInt(numSides) returns random in range 0 to numSides-1
  118. }
  119.  
  120. //getNumSides - returns the number of sides
  121. public int getNumSides()
  122. {
  123. return numSides;
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement