1 /* 2 * Program to make and use the Die class. 3 */ 4 5 package chanceapps; 6 7 import chance.Die; 8 9 public class Roller { 10 11 public static void main(String[] args) { 12 createAndRollStandardDieFiveTimes(); 13 createAndRollTwentySidedDieFiveTimes(); 14 createAndRollStandardDie(20); 15 createAndRollStandardDie(30); 16 createAndRollNineSidedDie(20); 17 createAndRollNineSidedDie(30); 18 System.out.println("ten times, roll a standard die for a 1."); 19 for (int i = 1; i <= 10; i++) { 20 createAndRollStandardDieFor1(); 21 } 22 System.out.println("ten times, roll a twelve sided die for a 1."); 23 for (int i = 1; i <= 10; i++) { 24 createAndRollTwelveSidedDieFor1(); 25 } 26 } 27 28 private static void createAndRollStandardDieFiveTimes() { 29 System.out.println("Roll a standard die 5 times ..."); 30 Die die = new Die(); 31 die.roll(); System.out.print(die.top() + " "); 32 die.roll(); System.out.print(die.top() + " "); 33 die.roll(); System.out.print(die.top() + " "); 34 die.roll(); System.out.print(die.top() + " "); 35 die.roll(); System.out.print(die.top() + " "); 36 System.out.println(); 37 } 38 39 private static void createAndRollTwentySidedDieFiveTimes() { 40 System.out.println("Roll a 20 sided die 5 times ..."); 41 Die die2 = new Die(20); 42 die2.roll(); System.out.print(die2.top() + " "); 43 die2.roll(); System.out.print(die2.top() + " "); 44 die2.roll(); System.out.print(die2.top() + " "); 45 die2.roll(); System.out.print(die2.top() + " "); 46 die2.roll(); System.out.print(die2.top() + " "); 47 System.out.println(); 48 49 } 50 51 private static void createAndRollStandardDie(int nrOfTimes) { 52 System.out.println("Roll a standard die " + nrOfTimes + " times ..."); 53 Die lucky = new Die(); 54 int i = 1; 55 while (i <= nrOfTimes) { 56 lucky.roll(); 57 System.out.println(lucky.top() + " "); 58 i = i +1; 59 } 60 System.out.println(); 61 } 62 63 private static void createAndRollNineSidedDie(int nrOfTimes) { 64 System.out.println("Roll a 9 sided die " + nrOfTimes + " times ..."); 65 Die lucky2 = new Die(9); 66 int i = 1; 67 while (i <= nrOfTimes) { 68 lucky2.roll(); 69 System.out.println(lucky2.top() + " "); 70 i = i +1; 71 } 72 System.out.println(); 73 } 74 75 private static void createAndRollStandardDieFor1() { 76 Die die2 = new Die(); 77 die2.roll(); 78 System.out.print(die2.top() + " "); 79 while (die2.top() != 1) { 80 die2.roll(); 81 System.out.print(die2.top() + " "); 82 } 83 System.out.println(); 84 } 85 86 private static void createAndRollTwelveSidedDieFor1() { 87 Die die3 = new Die(12); 88 die3.roll(); 89 System.out.print(die3.top() + " "); 90 while (die3.top() != 1) { 91 die3.roll(); 92 System.out.print(die3.top() + " "); 93 } 94 System.out.println(); 95 } 96 97 } 98