Question

In: Computer Science

5) Consider the hierarchy of classes shown for a small part of a bird sanctuary. Notice...

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.

Solutions

Expert Solution

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.

}

==========================================================

 

Related Solutions

Consider the following randomly arranged in 3 classes data of a sample. Classes frequency 5-9 9...
Consider the following randomly arranged in 3 classes data of a sample. Classes frequency 5-9 9 10-14 25 15-19 8 Plot the cumulative histogram and the polygon and interpret. (2.5+2.5+2.5=7.5) Determine the Range (5) Determine the mean and the median. (7.5+7.5=15) Determine the standard deviation. (7.5) Determine the relative frequency and determine the percentage of observation having a value equal or lower than 12. (5+2.5)
5) Bird's Eye View manufactures three different sizes of bird cages: small (for finches and canaries),...
5) Bird's Eye View manufactures three different sizes of bird cages: small (for finches and canaries), medium (for cockatiels and small parrots), and large (for cockatoos and other large parrots). The company has recently implemented an activity-based costing system. Bird's Eye View has identified five different production activities as well as the best cost driver for each activity. Each activity and driver is listed below, along with the budgeted amount that is associated with each activity for next year. ActivityCost...
34% of Letecia’s goats over the age of 5 have attended some college classes. Consider a...
34% of Letecia’s goats over the age of 5 have attended some college classes. Consider a random sample of 15 of Letecia’s goats over the age of 5. a. Describe the population in this scenario. Be specific. b. How large does the population need to be to assume independence? c. Describe the variable in this scenario. What kind of a variable is it? d. Supposing the population is larger than the minimum you found in part b, can this scenario...
As a seasoned small business owner of 5-10 years, with 2-5 part-time employees, what advice would...
As a seasoned small business owner of 5-10 years, with 2-5 part-time employees, what advice would you give when it comes to decision making?
Consider the balanced chemical equation shown below: 2 NH3 + 5 F2 ---> N2F4 + 6...
Consider the balanced chemical equation shown below: 2 NH3 + 5 F2 ---> N2F4 + 6 HF 1. How many moles of N2F4 form per mole of reaction? (Answer is in mol / mol reaction) 2. If 0.022 moles of HF form, how many moles of reaction have been completed? I tried both .5, and 0.0146 for answer for 1, and .0256 and then 0.0183 ​for my answer for 2 - but none of those are correct.
Consider McCallum’s small open-economy macro model under fixed exchange rates. Write down the 5 equations of...
Consider McCallum’s small open-economy macro model under fixed exchange rates. Write down the 5 equations of the model and explain their meaning. Make sure you explain all notation. Classify the model’s variables into endogenous and exogenous. Show graphically and explain verbally the short-run and long-run effects of expansionary monetary policy
Part C Consider the titration of 50.0 mL of 0.20 M NH3 (Kb=1.8×10−5) with 0.20 M...
Part C Consider the titration of 50.0 mL of 0.20 M NH3 (Kb=1.8×10−5) with 0.20 M HNO3. Calculate the pH after addition of 50.0 mL of the titrant at 25 ∘C. Part D A 30.0-mL volume of 0.50 M CH3COOH (Ka=1.8×10−5) was titrated with 0.50 M NaOH. Calculate the pH after addition of 30.0 mL of NaOH at 25 ∘C.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT