Changes between Version 12 and Version 13 of ControlSystems/SoftwareTeam/Training/GettingStarted/JavaExercises


Ignore:
Timestamp:
Nov 15, 2016, 7:36:34 PM (9 years ago)
Author:
azhang
Comment:

Animal Farm 3

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/JavaExercises

    v12 v13  
    1414
    1515== Another challenge: Animal Farm
    16 Create an `abstract class` called `Animal` that has a `mass` and a `name`
     16Create an `abstract class` called `Animal` that has a `mass` and a `name`. These should be `private`.
    1717* All animals should have a constructor which gives them a `mass` and a `name` when they are created
    1818* All animals can `eat` an `amount`, which increases their `mass` by `amount`
    1919* All animals can also `move` a `distance`, which decreases their `mass` by `distance`
     20* All animals can `speak`, but they all speak differently
    2021* Everybody can use `getName` to get the name of an animal
    2122* Everybody can use `getMass` to get the mass of an animal
     
    4243
    4344
    44 == Animal Farm step 2:
     45== Animal Farm step 2: Constructor Overloading
    4546* All animals can also be created with only a `mass`.
    4647* If an animal is created without a `name`, it's name is set to `Steve` by default.
     
    5657
    5758`Farm` should:
    58 * Create an array of 5 `Animal` that contains:
     59* Create an array of 5 `Animal` called `animals` that contains:
    5960  * 1 `Dog` with `mass` `10`
    6061  * 1 `Dog` with `mass` `15` and `name` `Fido`
    6162  * 1 `Cat` with `mass` `13` and `name` `Cloud`
    6263  * 1 `Hummingbird` with `mass` `1.1`, `name` `Birdie`, and `wingspan` `2.2`
    63   * 1 `Dog` with `mass` `5`
     64  * 1 `Cat` with `mass` `5`
    6465* `printInformation` on all `Animal` in the array
     66
     67Questions 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`.
     73Hummingbirds 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