In: Computer Science
5) Consider the hierarchy of classes shown for a small part of a bird sanctuary.
Notice that an Owl is-a Bird, a SnowyOwl is-a Owl, and an ElfOwl is-a Owl.
The class Bird is specified as an abstract class as shown in the following implementation. Each Bird has a name and a noise that are specified when it is constructed.
public abstract class Bird
{
private String name;
private String noise;
/** Constructor for objects of class Bird */
public Bird(String birdName, String birdNoise)
{
name = birdName;
noise = birdNoise;
}
public String getName() { return name; }
public String getNoise() { return noise; }
public abstract String getFood();
}
An Owl is-a Bird whose noise is “hoot”. The food it eats depends on the type of Owl, which means that getFood cannot be implemented in the Owl class. Here is the implementation for the Owl class.
public abstract class Owl extends Bird
{
//Constructor
public Owl(String owlName)
{ super(owlName, “hoot”); }
}
5a) A SnowyOwl is-a Owl whose name is always “Snowy owl”. A SnowyOwl will randomly eat a hare, a lemming, or a small bird (depending on what’s available!), where each type of food is equally likely. The SnowyOwl class should use a random number to determine which food the SnowyOwl will eat. Assuming that the Owl class has been correctly defined, and given the class hierarchy shown previously, write a complete declaration of the class SnowyOwl, including implementation of the constructor and method(s).
5b) Consider the following partial declaration of class BirdSanctuary.
public class BirdSanctuary
{
/**The list of birds */
private Bird[] birdList;
/** Precondition: Each Bird in birdList has a getFood method
* implemented for it
* Postcondition: For each Bird in the birdList array, its name,
* followed by the result of a call to its getFood method has
* been printed, one line per Bird
*/
public void allEat()
{ /* to be implemented in this part */ }
//The constructor and other methods are not shown.
}
Write the BirdSanctuary method allEat. For each Bird in BirdSanctuary, allEat prints a line with the name of the Bird followed by the result of a call to its getFood method, one line per Bird.
public class SnowyOwl extends Owl { public SnowyOwl(String owlName) { super(owlName); } public String getFood() { Random random = new Random(); int num = random.nextInt(3); if (num == 0) return "a hare"; else if (num == 1) return "a lemming"; else return "a small bird"; } }
==========================================================
BELOW IS THE ALL EAT() METHOD
public class BirdSanctuary { /** * The list of birds */ private Bird[] birdList; /** * Precondition: Each Bird in birdList has a getFood method * <p> * implemented for it * <p> * Postcondition: For each Bird in the birdList array, its name, * <p> * followed by the result of a call to its getFood method has * <p> * been printed, one line per Bird */ public void allEat() { /* to be implemented in this part */ for(Bird bird:birdList){ System.out.println(bird.getName()+" "+bird.getFood()); } } //The constructor and other methods are not shown. }
==========================================================