Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In JAVA Abstract Class Add an abstract class called Pet not containing the main method. The class should contain: An attribute of type String called

In JAVA

Abstract Class

Add an abstract class called Pet not containing the main method. The class should contain:

An attribute of type String called name.

An attribute of type int called birthYear.

A default constructor and a constructor with values for the two attributes.

Three abstract void methods called makeSound, doTrick, and output with no parameters.

-Add a second class to the project called TestPets containing the main method. Try declaring a variable of type Pet and assigning it a new object of this type to see what kind of error you get.

B. Derived Classes.

Add three more classes to the project, called Cat, Dog, and Parrot. Implement the three abstract methods in each of these classes, without the abstract keyword. Add a constructor with a name and a year to each of these classes.

In the class TestPets, in the main, declare an array of the type Pet and assign it an array with 6 element. Then assign two objects of each of the derived classes to the elements of the array using the constructors with a name and a birth year.

Use a for loop to output each of the objects of the array. Then use another for loop to call the methods makeSound and playTrick on each object.

C. Sorting. Now we would like to sort the array of pets. We need to add implements Comparable to the class Pet. Then in the class, define the function

@Override public int compareTo(Pet p) In this function, return the value of the year of the target object (this) minus the year of the object p.

In the main, add a call to Arrays.sort() with the array of pets as parameter, then output the array again. import java.util.Arrays for this.

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

7. Where Do We Begin?

Answered: 1 week ago

Question

a. What is the purpose of the team?

Answered: 1 week ago

Question

b. What are its goals and objectives?

Answered: 1 week ago