wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/JavaExercises

Version 13 (modified by azhang, 9 years ago) (diff)

Animal Farm 3

http://www.ntu.edu.sg/home/ehchua/programming/java/j2a_basicsexercises.html

Recommended exercises:

Another challenge:

Write a high-low guessing game; have a person think of the number, and have the computer try to guess it. After each guess, the human will tell the computer whether it's higher or lower

Another challenge: Animal Farm

Create an abstract class called Animal that has a mass and a name. These should be private.

  • All animals should have a constructor which gives them a mass and a name when they are created
  • All animals can eat an amount, which increases their mass by amount
  • All animals can also move a distance, which decreases their mass by distance
  • All animals can speak, but they all speak differently
  • Everybody can use getName to get the name of an animal
  • Everybody can use getMass to get the mass of an animal

Create a class called Cat that extends Animal

  • When a cat speak, it prints Meow!

Create a class called Dog that extends Animal

  • When a dog speak, it prints Woof!

Create a class called Farm

  • The Farm has a main method
  • In the main method, the Farm creates a Cat with a mass of 12.2 named Cloud
  • In the main method, the Farm creates a Dog with a mass of 15.4 named Spot

In the main method, the Farm will:

  • print the names of both animals and their masses.
    • Ex. Spot is a Dog with a mass of 15.4.
  • Then, Cloud will eat 0.2, move 0.8, and speak.
  • Spot will then eat 0.4, move 0.6, and speak.
  • Farm will print the names of both animals and their masses once again.

You will probably get weird numbers like 15.200000000001. This is imprecision with floating point numbers, and is normal

Animal Farm step 2: Constructor Overloading

  • All animals can also be created with only a mass.
  • If an animal is created without a name, it's name is set to Steve by default.
  • All animals now also have a setName method which sets their name.
  • All animals can printInformation, which prints information on the specific animal.
    • Ex. Spot is a Dog with a mass of 15.4

Create a class called Hummingbird

  • All Hummingbird have a wingspan.
  • Hummingbirds must be created with a mass, wingspan, and name.
  • When a Hummingbird speak, it prints Tweet!
  • Everybody can use getWingspan to get the wingspan of a Hummingbird.

Farm should:

  • Create an array of 5 Animal called animals that contains:
    • 1 Dog with mass 10
    • 1 Dog with mass 15 and name Fido
    • 1 Cat with mass 13 and name Cloud
    • 1 Hummingbird with mass 1.1, name Birdie, and wingspan 2.2
    • 1 Cat with mass 5
  • printInformation on all Animal in the array

Questions to think about:

  • animals[4] is a Hummingbird, but does animals[4].getWingspan() work?
  • How can you get the wingspan of animals[4]?

Animal Farm step 3: Method Overloading and Overriding

  • 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.

Hummingbirds are very small, so they will only eat a maximum of their own mass.

  • If a Hummingbird with mass 1 eats 5, it will end with a mass of 2
  • 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
  • mass and name should stay private. This may be a bit challenging at first, but be creative!
  • Hint: super.method() will run method() of the superclass