Question: / * APCSA Ch 8 2 CBU 0 9 6 _ 4 _ Pet Adapted from College Board Unit 9 . 6 , FRQ# 1

/* APCSA Ch82CBU096_4_Pet
Adapted from College Board Unit 9.6, FRQ#1 'Pet'
Burkham, 2024
PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been
imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls
are not null and that methods are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods
that are listed in classes defined in that question. Writing significant amounts of
code that can be replaced by a call to one of these methods will not receive full credit.
The Pet class is used to represent pets and print information about each pet.
Each Pet object has attributes for the pet name and species.
The Dog class is a subclass of the Pet class that has one additional attribute:
a String variable named breed that is used to represent the breed of the dog.
The Dog class also contains a printPetInfo method to print the name and breed of the dog.
CONDITIONS:
- Up to 5 Java statements (total for all 3 parts) may be added
- Other lines of code may not be changed
- Your final implementation should conform to the 'FINAL OUTPUT' example (at end)
- Format, line spacing, etc. must match exactly
NOTES:
Properly use at least one method super.
Do not use any getter method!!
Use a comment for each line.
PART A:
Consider the following code segment.
Pet pet1= new Pet("Floppy", "rabbit");
Pet pet2= new Pet("Arty", "dog");
pet1.printPetInfo();
pet2.printPetInfo();
The code segment is intended to print the following output.
Floppy is a rabbit
Arty is a dog
Complete the Pet method printPetInfo().
PART B:
Consider the following code segment.
Dog fluffy = new Dog("Fluffy", "pomeranian");
fluffy.printPetInfo();
The code segment is intended to print the following output.
Fluffy is a dog of breed pomeranian
Complete the Dog method printPetInfo().
PART C:
The PetOwner class below is used to generate a description about a pet and its owner.
The PetOwner constructor takes a Pet object and a String value (representing the name
of the pet owner) as parameters.
Code Segment Result Printed
owner1.printOwnerInfo(); Floppy is a rabbit owned by Jerry
owner2.printOwnerInfo(); Arty is a dog of breed pug owned by Kris
Complete the PetOwner method printOwnerInfo().
FINAL OUTPUT:
The final formatted output (all 3 parts completed) should be exactly this:
Floppy is a rabbit
Arty is a dog
Fluffy is a dog of breed pomeranian
Floppy is a rabbit owned by Jerry
Arty is a dog owned by Kris
*/
public class Ch82CBU096_4a_Pet_STU
{
public static void main ( String[] args )
{
Pet pet1= new Pet("Floppy", "rabbit");
Pet pet2= new Pet("Arty", "dog");
pet1.printPetInfo();
pet2.printPetInfo();
Dog fluffy = new Dog("Fluffy", "pomeranian");
fluffy.printPetInfo();
PetOwner owner1= new PetOwner(pet1, "Jerry");
PetOwner owner2= new PetOwner(pet2, "Kris");
owner1.printOwnerInfo();
owner2.printOwnerInfo();
}
}
class Pet
{
private String name;
private String species;
public Pet(String n, String s)
{
name = n;
species = s;
}
public String getName()
{
return name;
}
public void printPetInfo()
{
/* to be implemented in PART A */
}
}
class Dog extends Pet
{
private String breed;
public Dog(String n, String b)
{
super(n, "dog");
breed = b;
}
public void printPetInfo()
{
/* to be implemented in PART B */
}
}
class PetOwner
{
private Pet thePet;
private String thePetOwner;
public PetOwner(Pet p, String o)
{
super();
thePet = p;
thePetOwner = o;
}
public void printOwnerInfo()
{
/* to be implemented in PART C */
}
}

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!