|
| 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 | +As written the code in `GameConsole.Controller` |
| 11 | +looks at its own `isPoweredOn` field. |
| 12 | + |
| 13 | +Disambiguate the usage so that it is uses the `isPoweredOn` field from |
| 14 | +the `GameConsole` instance wrapping it in its `status` method. |
| 15 | + |
| 16 | +```java,editable |
| 17 | +class GameConsole { |
| 18 | + boolean isPoweredOn; |
| 19 | +
|
| 20 | + GameConsole() { |
| 21 | + this.isPoweredOn = false; |
| 22 | + } |
| 23 | +
|
| 24 | + class Controller { |
| 25 | + boolean isPoweredOn; |
| 26 | +
|
| 27 | + Controller() { |
| 28 | + this.isPoweredOn = false; |
| 29 | + } |
| 30 | +
|
| 31 | + String status() { |
| 32 | + "Controller[" |
| 33 | + + isPoweredOn ? "ON" : "OFF" + "] - GameConsole[" |
| 34 | + + isPoweredOn ? "ON" : "OFF" + "]"; |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | +
|
| 39 | +class Main { |
| 40 | + void main() { |
| 41 | + var console = new GameConsole(); |
| 42 | + var controller = console.new Controller(); |
| 43 | +
|
| 44 | + IO.println(controller.status()); |
| 45 | +
|
| 46 | + console.isPoweredOn = true; |
| 47 | + IO.println(controller.status()); |
| 48 | +
|
| 49 | + controller.isPoweredOn = true; |
| 50 | + IO.println(controller.status()); |
| 51 | +
|
| 52 | + console.isPoweredOn = false; |
| 53 | + IO.println(controller.status()); |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Challenge 2. |
| 59 | + |
| 60 | +Make the `Controller` class from the previous example a `static` inner class. |
| 61 | +The `status` method should keep the same behavior. This means you will need |
| 62 | +to explicitly pass an instance of `GameConsole` to the constructor of `Controller` |
| 63 | +and store it in a field. |
| 64 | + |
| 65 | +## Challenge 3. |
| 66 | + |
| 67 | +Successfully make an instance of `Fly` from the `Main` class. |
| 68 | + |
| 69 | +```java,editable |
| 70 | +class OldLady { |
| 71 | + class Horse { |
| 72 | + class Cow { |
| 73 | + class Goat { |
| 74 | + class Dog { |
| 75 | + class Cat { |
| 76 | + class Bird { |
| 77 | + class Spider { |
| 78 | + class Fly { |
| 79 | + Fly() { |
| 80 | + IO.println("She's dead, of course"); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | +
|
| 92 | +class Main { |
| 93 | + Fly f = /* CODE HERE */; |
| 94 | + IO.println(f); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## Challenge 4. |
| 99 | + |
| 100 | +Go back to some of the programs you wrote entirely within the anonymous |
| 101 | +main class. Turn that main class into a named class. |
| 102 | + |
| 103 | +First make sure your program works the same as it did. Then |
| 104 | +go through all the inner classes in your program and mark them |
| 105 | +as many of them as you can `static`. |
| 106 | + |
| 107 | +Which ones can't be trivially made `static`? Note what state they |
| 108 | +access in the enclosing instance. |
0 commit comments