Question: 9.28 M03-ACT01-Pet Tricks IN JAVA ASAP Step 1: The Pet base class Given a partial abstract Pet base class, your job in this lab is

9.28 M03-ACT01-Pet Tricks

IN JAVA ASAP

Step 1: The Pet base class

Given a partial abstract Pet base class, your job in this lab is complete the implementation of Pet and to implement the following two concrete subclasses:

GoldFish representing a pet goldfish

Dog representing a pet dog

Instructions for Pet:

Add an abstract method called doTrick. The method should take a parameter of type Command and return a Response. Both the Response and Command types are modeled as enums and are provided as part of the lab (see the top of the Pet.java file).

add a defensive check to the constructor of Pet that throws an IllegalArgumentException if the input age is negative or 0. Give the thrown exception the message "age must be positive (and nonzero)"

Step 2: The GoldFish Subclass (this class should NOT be marked abstract)

Add a GoldFish class that extends Pet.

Add a constructor for the GoldFish that accepts a name and age; super(..) both of these up to the Pet constructor.

Implement the doTrick method. The fish should only do a trick if given the command Command.JUMP_THROUGH_FIRE_HOOLAHOOP in which case the method should return Resonse.JUMPS_THROUGH_FIREY_HOOP. Otherwise the method should always return Response.DOES_NOTHING.

Here's a partial implementation of step 3 above for the GoldFish

// the abstract class Pet should contain: // public abstract Response doTrick(Command cmd); // <- no body (it's an abstract method) //in GoldFish.java @Override public Response doTrick(Command cmd) { if (cmd == Command.JUMP_THROUGH_FIRE_HOOLAHOOP) { // todo } else { Response.DOES_NOTHING; } } 

Step 3: The Dog Subclass (this class should NOT be marked abstract)

Add a subclass for Dog that includes a field called likesFetch indicating whether or not the dog likes playing fetch. Now make a constructor that takes three parameters: a name String, an age, and whether or the dog likes playing fetch. The name and age parameters should be 'super-ed' up to the base class's constructor. Initialize the likesFetch field using the constructor parameter.

Now overload the first constructor for Dog with one that takes only a name and age (make it such that Dogs created via this constructor do not like fetch).

Add a getter method called likesPlayingFetch that takes no parameters and returns true if the dog likes playing fetch; false otherwise.

Implement the doTrick method for the dog. Here's how it should work:

when given the command FETCH, the dog will respond by fetching, but only if it likesFetch

when given the command SIT, the dog should respond by sitting

in all other cases the dog should respond by doing nothing

Step 4: Complete the Tester class

In the main, construct four pet objects:

the first pet should be a dog named "roofus" with an age of 5 -- doesn't like fetch

the second pet should be a goldfish named "gloopy" with an age of 1

the third pet should be a goldfish named "hiccup" with an age of 2

the fourth pet should be a dog named "echo" with an age of 12 -- likes fetch

Next, add each pet in the order given above to the pets ArrayList declared for you. Once this is done, the loop provided for you will iterate over each pet, issuing random commands for which the pets will respond (or not).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!