Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. CLASS:
  2. ------
  3.  
  4. package Assignment4;
  5.  
  6. /**
  7. *
  8. * @author
  9. */
  10. public class Die {
  11.  
  12. private double sides; // the amount of sides on the die
  13. private int sideShowing; // side currently showing
  14.  
  15. public Die(int sides) {
  16.  
  17. this.sides = sides;
  18. sideShowing = rollDice();
  19.  
  20. }
  21.  
  22. //get the current face of the die
  23. public int rollDice() {
  24. sideShowing = (int)(this.sides * Math.random()) + 1;
  25. return sideShowing;
  26. }
  27.  
  28. public int getCurrentSide() {
  29. return sideShowing;
  30. }
  31.  
  32.  
  33.  
  34. @Override
  35. public String toString(){
  36.  
  37. return "d" + (int)sides + "=" + (int)sideShowing;
  38.  
  39.  
  40. }
  41.  
  42.  
  43.  
  44. }
  45.  
  46.  
  47. -----------------------------------------------------
  48.  
  49. Main Method:
  50.  
  51. package Assignment4;
  52.  
  53. import java.util.Arrays;
  54. import java.util.Scanner;
  55.  
  56. /**
  57. *
  58. * @author
  59. */
  60. public class MainHistogram {
  61.  
  62.  
  63.  
  64. public static void main(String[] args) {
  65.  
  66.  
  67.  
  68.  
  69. int menuChoice, diceAmt, rolls, sides;
  70.  
  71.  
  72.  
  73. Scanner keyboard = new Scanner (System.in);
  74.  
  75. System.out.println("How many dice?");
  76.  
  77. diceAmt = keyboard.nextInt();
  78.  
  79.  
  80.  
  81.  
  82. Die[] diceArray = new Die[diceAmt];
  83.  
  84. for (int i = 0; i < diceArray.length; i++){
  85.  
  86. System.out.println("Enter the number of sides on die " + (i+1) + ": ");
  87.  
  88. sides = keyboard.nextInt();
  89.  
  90.  
  91. diceArray[i] = new Die(sides);
  92.  
  93. }
  94.  
  95.  
  96.  
  97. do{
  98.  
  99. System.out.println("Dice Collection: " + Arrays.toString(diceArray));
  100.  
  101.  
  102. System.out.println(" 1=roll once, 2=roll 100,000 times, 3=quit: ");
  103.  
  104.  
  105. menuChoice = keyboard.nextInt();
  106.  
  107. if (menuChoice == 1){
  108. rolls = 1;
  109. System.out.println(Arrays.toString(diceArray));
  110.  
  111.  
  112. }
  113.  
  114. if (menuChoice == 2){
  115.  
  116. rolls = 100000;
  117.  
  118. }
  119.  
  120.  
  121.  
  122.  
  123. }while ( menuChoice != 3);
  124.  
  125. System.out.println("BYE!!!");
  126.  
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement