Question: PlayfulPets ( 7 0 ) Pet class This class has a static List variable of type Pet named pets that is instantiated as an ArrayList.

PlayfulPets (70)
Pet class
This class has a static List variable of type Pet named pets that is instantiated as an ArrayList. This class has the following private instance variables:
String name
String animal
String breed
String color
double price
Each instance variable has a simple mutator and accessor.
There is a default constructor that does nothing.
There is a second constructor with parameters for each of the five instance variables. It calls the mutator for each parameter in order to set the corresponding instance variable. There is a toString method that prints out a line like this:
Max: a black poodle dog purchased for $540.50
There is an equals method that returns true if the item being compared with is also a Pet and if the animal and breed are the same.
PetMatcher interface
PetMatcher is a functional interface with both an abstract and default method.
method matchPet is the abstract method (dont use the word abstract since we are in an interface.) This method returns a List of type Pet and has a Pet as an input parameter
default method firstPet has an input of type Pet and a return value of type Pet. Get the index of the first matching Pet in Pet.pets (the ArrayList in Pet)(use indexOf) if the index is less than 0(indexOf does this for you)
return null
else
return a reference to the Pet at that index
PlayfulPets class
This class has a main method and one additional method called matchPet. It also creates two different implementations of the PetMatchers functional interfaces abstract method.
method main
Create five Pets and add them to the Pet.pets (the static ArrayList of Pet)
You can use the following:
Scruffy","dog","poodle","white",895.00
Meow","cat","siamese","white",740.25
Max","dog","poodle","black",540.50
Cuddles","dog","pug","black",1282.75
Slider","snake","garden","green",320.00
Please use a Lambda Expression for breedMatcher
Create a new PetMatcher called breedMatcher that implements petMatch returning a List of all those Pets of the same breed. In this case we dont override the firstPet method.
The following instances of PetMatcher (priceMatcher and color Matcher) can be done using inner classes; This is necessary in order to override the default method the easiest way.
Create a new PetMatcher called priceMatcher that implements petMatch returning a List of all those Pets less than or equal to the price of the input parameter Pet. priceMatcher also overrides the firstPet method, creating a new firstPet that searches Pets.pet for a Pet less than or equal to the price of the input Pet, returning a reference to the first such instance of such a Pet. If no such Pet exists return null.
Create a new PetMatcher called colorMatcher that implements petMatch returning a List of all those Pets equal to the color of the input parameter Pet.(Remember color is a String so use the equals method on the colors.) colorMatcher also overrides the firstPet method, creating a new firstPet that searches Pets.pet for a Pet with a color that equals the color of the input Pet, returning a reference to the first such instance of such a Pet. If no such Pet exists return null.
Make the following calls in main:
matchPetsFromTheList("Poodles",breedMatcher, new Pet(null, "dog", "poodle", null, 0.0)); matchPetsFromTheList("Pets for $700 or less",priceMatcher, new Pet(null, null, null, null, 700.0));
matchPetsFromTheList("Pets that are white",colorMatcher, new Pet(null, null, null, "white", 0.0));
6
method matchPetsFromTheList has a void return and three parameters String criteria, PetMatcher matcher and Pet called myPet. Note: The second parameter allows you to send code as a parameter.
In this method you will first use matcher with firstPet to print out the first Pet. Then create a List with all the matches using the matcher and print out the results.
Sample output for this example:
Poodles:
First: Scruffy: a white poodle dog purchased for $895.00
All matches:
Scruffy: a white poodle dog purchased for $895.00
Max: a black poodle dog purchased for $540.50
Pets for $700 or less:
First: Max: a black poodle dog purchased for $540.50
All matches:
Max: a black poodle dog purchased for $540.50
Slider: a green garden snake purchased for $320.00
Pets that are white:
First: Scruffy: a white poodle dog purchased for $895.00
All matches:
Scruffy: a white poodle dog purchased for $895.00
Meow: a white siamese cat purchased for $740.25

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 Finance Questions!