Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives: In this activity, you will experiment with inheritance and polymorphism. You can either work locally on your computer using IntelliJ or directly in the

Objectives: In this activity, you will experiment with inheritance and polymorphism. You can either work locally on your computer using IntelliJ or directly in the zyLab IDE below. IntelliJ is recommended.

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).

Given code

Pet.java

public abstract class Pet {

public enum Command {SIT, FETCH, JUMP_THROUGH_FIRE_HOOLAHOOP} public enum Response { SITS, FETCHES, DOES_NOTHING, JUMPS_THROUGH_FIREY_HOOP }

protected String name; protected int age;

public Pet(String name, int age) { // TODO: Throw an IllegalArgumentException if the input age is negative or 0 // give it the message "age must be positive (and nonzero)"

// TODO: initialize name and age }

// TODO: add an abstract method named doTrick that takes a parameter of type Command and returns // a Response.

public int getAge() { return age; }

public String getName() { return name; }

@Override public String toString() { return "pet " + getClass().getSimpleName().toLowerCase() + ": " + name; } }

Tester.java

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Random;

public class Tester { public static void main(String[] args) {

// TODO: Instantiate the five pet objects here exactly in the order and // with the characteristics detailed in the instructions

StringBuilder sb = new StringBuilder(); ArrayList pets = new ArrayList<>();

// TODO: Add each pet to the pets arraylist above

Random random = new Random(53); for (Pet p : pets) { int randNum = random.nextInt(3); Pet.Command cmd = Pet.Command.values()[randNum]; sb.append(p).append(" given command: ") .append(cmd) .append(" ... ").append(p.doTrick(cmd)).append(" "); } System.out.println(sb); } }

GoldFish.java

//todo: GoldFish class goes here

Dog.java

//todo: Dog class goes here

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

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions