Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Maintaining hunger level Make sure to include get/set methods for hungerLevel and name. The setter method for hungerLevel should guarantee that it is set

Java

image text in transcribed

Maintaining hunger level

Make sure to include get/set methods for hungerLevel and name. The setter method for hungerLevel should guarantee that it is set in the range 0 to 10 (ie, trying to set hungerLevel to 11 would result in it being set to 10, and trying to set it to -4 would result in it being set to 0).

Additional methods in the Animal class include a sleep() method that simply println's "sleeping..." and maximizes the hungerLevel. The class also has a roam() method that simply println's "moving around..." and increases hungerLevel by one. The class has an abstract makeNoise() method and an abstract eat() method. Only the get methods described above should return anything. Only the set methods described above should take a parameter.

Other classes

  • Canine is an abstract class that is a subclass of Animal. Canine overrides the roam() method to println "like canines roam in packs..." (and still increases hungerLevel by one). Like the Animal class, its constructor contains the parameters Zoo myzoo and name. This is true for every Animal sub-class.
  • Feline is another abstract sub-class of Animal: This abstract class overrides the roam() method with felines like to roam alone (and hunger level increases by 1).
  • Dog is a concrete canine class. The method makeNoise() should "bark...". The method eat() should "slop..." and decreases hunger level by three using mutator and accessor.
  • Wolf is a concrete canine class. The method makeNoise() should "growl...". The method eat() should "rip with teeth..." and decreases hunger by two.
  • Cat is a concrete feline class. The method makeNoise() should meow. The method eat() should pick and decrease the hunger level by three.
  • Hippo is a concrete animal class. The method makeNoise() should "blub...". The method eat() should "slurp..." and decreases hunger by one.
  • Add a Coyote class (think "howl" and "gnaws" and decrease hunger like a wolf).
  • Add a Lion class (think roar and rip with teeth and decrease hunger like a wolf.
  • Add a FeralCat and WildDog class similar to the Cat and Dog classes above.

Activity 2 - Polymorphism

  • Write a Zoo class that has four fields: the name of the zoo, two doubles for latitude and longitude, and an ArrayList called zooAnimals. The constructor takes a String as the name of the zoo and two doubles: the latitude and longitude. It also should create the ArrayList. The class should have get methods for the latitude and longitude. Also have an addAnimal method which takes an Animal as a parameter, and adds it to your ArrayList. Also, have a testAnimals() method which prints the name of the zoo, a newline, the location of the zoo in latitude, a newline, the longitude of the zoo, a newline, the number of animals in the zoo, a newline, and uses a for-each loop to print the behavior for each animal. For each animal: (1) first have it go to sleep(). (2) They will then be hungry (sleep changes hungerLevel, remember!) so have them makeNoise() and then eat(). (3) After eating they are full and will roam().
  • Write a main method inside the Zoo class. The main method should create a Zoo object, create one animal object of each kind of animal and add it to the zoo. Choose an appropriate name of each animal that clearly indicates the type of animal, such as Leo the Lion. Do not accept keyboard input within you main method. There, youve tested all the methods on all the objects! Each of the method calls in the testAnimal method are polymorphic since you get an Animal out of the zooAnimal ArrayList and call the method but not really knowing (or caring) the specific type of animal.

Activity 3 - Interfaces

  • Add a Pet interface that defines a play() and beFriendly() method for the Cat and Dog classes. The feral cat and wild dog will not implement this interface.
  • Have the appropriate animal(s) above implement the interface (make sure the cats frolic when they play and purr when they are friendly) and then test this out in your Zoo classes testAnimals() method. Because not all animals will implement the Pet interface, youll have to use the instanceof keyword to determine if the current Animal contains the play() and beFriendly() methods.
  • Add an AnimalLocation interface for a place that contains a bunch of animals (such as a Zoo). The AnimalLocation interface should define the following methods: String getName(), setName(String), int getNumOfAnimals(). Now modify your Zoo class to implement this interface.
Zoo Animal interface AnimalLocation Hippo 1- + Hippo(myZoo:Z00, name: String) + makeNoise(): void + eat:void -name: String - zoo: Zoo |- hungerLevel: int + Animal(myZoo Zoo, animalName : String) + getHungerLevel(): int + setHungerLevel(hunger : int) : void + getName(): String + setName(animalName : String): void + sleep(): void + roam(): void + makeNoise(): void + eat:void + getName(): String + setName(locName : String): void + getNumOfAnimals(): int - name : String - latitude: double longitude: double - zooAnimals : ArrayList

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions