Solution to PirateParrot free response practice question

Complete the PirateParrot practice problem before reviewing the solution.

Review the PirateParrot solution with AP CS Tutor Brandon Horn.

public class PirateParrot extends Parrot
{
 /** The total number of years the pirate parrot has stolen */
 private int yearsStolen = 0;

 public PirateParrot(String name)
 {
  super(name);
  train("Polly want a cracker");
 }

 public int getAge()
 {
  return super.getAge() + yearsStolen;
 }

 public void stealSoul(int soulAge)
 {
  yearsStolen += soulAge;
 }
}

You must store only the information specific to a pirate parrot as your state. Class Parrot already stores the parrot’s natural age and the list of sounds it can make. Specifically, you must store the number of additional years obtained by stealing souls rather than the pirate parrot’s total age.

You must specify method stealSoul to keep track of the sum of the ages of all stolen souls.

You must override method getAge, which is public in Parrot, to include the additional years obtained by stealing souls.

You must specify a constructor that accepts a String parameter. Since there is no visible default constructor for Parrot, you must explicitly run the constructor that accepts a String parameter. The call to the superclass constructor must be the first line in your constructor. Your constructor must run the train method from Parrot so the pirate parrot will be able to make the required sound immediately after construction.

You should not override method speak since a pirate parrot speaks as a parrot. You would lose points on the exam for overriding method speak to do anything other than returning super.speak.

Get AP CS Help

Leave a Reply