|
1 | 1 | # Challenges |
| 2 | + |
| 3 | +Remember the rules for this are |
| 4 | + |
| 5 | +- Try to use only the information given up to this point in this book. |
| 6 | +- Try not to give up until you've given it a solid attempt |
| 7 | + |
| 8 | +## Challenge 1 |
| 9 | + |
| 10 | +Put this code in a file named `Pickle.java` |
| 11 | + |
| 12 | +```java |
| 13 | +class Pickle { |
| 14 | + boolean dill; |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +Put this code in a file named `Ramen.java` |
| 19 | + |
| 20 | +```java |
| 21 | +class Ramen { |
| 22 | + void mixWith(Pickle pickle) { |
| 23 | + IO.println("This is gross."); |
| 24 | + if (pickle.dill) { |
| 25 | + IO.println("Extra super gross."); |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +And finally put this code in `Main.java` |
| 32 | + |
| 33 | +```java |
| 34 | +class Main { |
| 35 | + void main() { |
| 36 | + var pickle = new Pickle(); |
| 37 | + pickle.dill = true; |
| 38 | + |
| 39 | + var ramen = new Ramen(); |
| 40 | + |
| 41 | + ramen.mixWith(pickle); |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Then run the entire program from your terminal by using `java Main.java` |
| 47 | +in the directory where you made these files. |
| 48 | + |
| 49 | +## Challenge 2 |
| 50 | + |
| 51 | +Move `Ramen.java`, `Pickle.java`, and `Main.java` into a folder named `src`. |
| 52 | + |
| 53 | +Make sure you can still run the program by typing `java src/Main.java`. |
| 54 | + |
| 55 | +## Challenge 3 |
| 56 | + |
| 57 | +The following code uses an anonymous main class. Make it instead use a named class. |
| 58 | + |
| 59 | +```java,editable |
| 60 | +int dogs = 99; |
| 61 | +
|
| 62 | +void main() { |
| 63 | + while (dogs > 0) { |
| 64 | + IO.println(dogs + " dogs on the floor."); |
| 65 | + IO.println("Wake one up, take it for a walk"); |
| 66 | + dogs--; |
| 67 | + } |
| 68 | + IO.println("No more dogs on the floor."); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Challenge 4 |
| 73 | + |
| 74 | + |
| 75 | +Combine the following programs while keeping them in separate files. |
| 76 | +This will require you to give names to both classes as well as |
| 77 | +parameterize the "dogs on the floor" program in some way. |
| 78 | + |
| 79 | +```java |
| 80 | +int dogs = 99; |
| 81 | + |
| 82 | +void main() { |
| 83 | + while (dogs > 0) { |
| 84 | + IO.println(dogs + " dogs on the floor."); |
| 85 | + IO.println("Wake one up, take it for a walk"); |
| 86 | + dogs--; |
| 87 | + } |
| 88 | + IO.println("No more dogs on the floor."); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +```java |
| 94 | +void main() { |
| 95 | + while (true) { |
| 96 | + try { |
| 97 | + int dogs = Integer.parseInt(IO.readln("How many dogs are on the floor? ")); |
| 98 | + IO.println("TODO"); // Call the first program here somehow. |
| 99 | + } catch (RuntimeException e) { |
| 100 | + IO.println("Goodbye!"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
| 105 | +``` |
0 commit comments