Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

AP Computer Science Java The Parrot class represents a parrot with an age in years and the ability to learn sounds which can repeat back

AP Computer Science Java

The Parrot class represents a parrot with an age in years and the ability to learn sounds which can repeat back when asked to speak. The declaration of the Parrot class is shown below.

public class Parrot

{

//creates a new Parrot object

public Parrot(String name)

{

//implementation not shown

}

//returns the age of the parrot

public int getAge()

{

//implementation not shown

} //adds a sound to the list of sounds the parrot can make

public void train(String sound)

{

//implementation not shown

}

public String speak()

{

//implementation not shown

}

A pirate parrot is a type of parrot. A pirate parrot knows how to make the sound Polly want a cracker immediately upon birth. A pirate parrot can also steal souls whose age becomes part of the pirate parrots age. A pirate parrot is represented by the PirateParrot class, which you will write.

Assume that the following code segment appears in a class other than PirateParrot. The code segment shows an example of using the PirateParrot class.

PirateParrot polly = new PirateParrot(Polly);

System.out.println(polly.getAge()); //prints 0

// code to increase Pollys age by 5 years

System.out.println(polly.getAge()); //prints 5

polly.stealSoul(5);

polly.stealSoul(10);

System.out.println(polly.getAge()); //prints 20

polly.train(Walk the plank);

polly.train(Off with his head);

//Polly retires from his life as a pirate to a cushy life as a pet

Parrot myPetPolly = polly;

System.out.println(myPetPolly.getAge()); //prints 20

myPetPolly.train(Time for bed);

System.out.println(myPetPolly.speak());

/* prints one of the following at random

* Polly wants a cracker

* Walk the plank

* Off with his head

* Time for bed

*/

Write the PirateParrot class. Your code must produce the indicated results.

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

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago