Changes between Version 12 and Version 13 of ControlSystems/SoftwareTeam/Training/GettingStarted/JavaExercises
- Timestamp:
- Nov 15, 2016, 7:36:34 PM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/Training/GettingStarted/JavaExercises
v12 v13 14 14 15 15 == Another challenge: Animal Farm 16 Create an `abstract class` called `Animal` that has a `mass` and a `name` 16 Create an `abstract class` called `Animal` that has a `mass` and a `name`. These should be `private`. 17 17 * All animals should have a constructor which gives them a `mass` and a `name` when they are created 18 18 * All animals can `eat` an `amount`, which increases their `mass` by `amount` 19 19 * All animals can also `move` a `distance`, which decreases their `mass` by `distance` 20 * All animals can `speak`, but they all speak differently 20 21 * Everybody can use `getName` to get the name of an animal 21 22 * Everybody can use `getMass` to get the mass of an animal … … 42 43 43 44 44 == Animal Farm step 2: 45 == Animal Farm step 2: Constructor Overloading 45 46 * All animals can also be created with only a `mass`. 46 47 * If an animal is created without a `name`, it's name is set to `Steve` by default. … … 56 57 57 58 `Farm` should: 58 * Create an array of 5 `Animal` that contains:59 * Create an array of 5 `Animal` called `animals` that contains: 59 60 * 1 `Dog` with `mass` `10` 60 61 * 1 `Dog` with `mass` `15` and `name` `Fido` 61 62 * 1 `Cat` with `mass` `13` and `name` `Cloud` 62 63 * 1 `Hummingbird` with `mass` `1.1`, `name` `Birdie`, and `wingspan` `2.2` 63 * 1 ` Dog` with `mass` `5`64 * 1 `Cat` with `mass` `5` 64 65 * `printInformation` on all `Animal` in the array 66 67 Questions to think about: 68 * `animals[4]` is a `Hummingbird`, but does `animals[4].getWingspan()` work? 69 * How can you get the wingspan of animals[4]? 70 71 == Animal Farm step 3: Method Overloading and Overriding 72 * All animals can also `eat` other `Animal`, which increases their `mass` by the `mass` of the `Animal` they ate, and sets the `mass` of the `Animal` they ate to `0`. 73 Hummingbirds are very small, so they will only `eat` a maximum of their own `mass`. 74 * If a Hummingbird with `mass` `1` eats `5`, it will end with a mass of `2` 75 * If a Hummingbird with `mass` `2` eats a `Cat` with `mass` `5`, it will end with a mass of `4`, and the `Cat` will end with a `mass` of `0` 76 * `mass` and `name` should stay `private`. This may be a bit challenging at first, but be creative! 77 * Hint: `super.method()` will run `method()` of the superclass