Question: 2 . 1 7 Lab 2 : Pet information ( derived classes ) The base class Pet has private fields petName, and petAge. The derived
Lab : Pet information derived classes
The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main to:
create a generic pet and print information using printInfo
create a Dog pet, use printInfo to print information, and add a statement to print the dog's breed using the getBreed method.
Ex If the input is:
Dobby
Kreacher
German Schnauzer
the output is:
Pet Information:
Name: Dobby
Age:
Pet Information:
Name: Kreacher
Age:
Breed: German Schnauzer
Dog.java
public class Dog extends Pet
private String breed;
public void setBreedString userBreed
breed userBreed;
public String getBreed
return breed;
Pet.java
public class Pet
protected String name;
protected int age;
public void setNameString userName
name userName;
public String getName
return name;
public void setAgeint userAge
age userAge;
public int getAge
return age;
public void printInfo
System.out.printlnPet Information: ;
System.out.println Name: name;
System.out.println Age: age;
PetInformation.java
public class PetInformation
public static void mainString args
Scanner scnr new ScannerSystemin;
Pet myPet new Pet;
Dog myDog new Dog;
String petName, dogName, dogBreed;
int petAge, dogAge;
petName scnrnextLine;
petAge scnrnextInt;
scnrnextLine;
dogName scnrnext;
dogAge scnrnextInt;
scnrnextLine;
dogBreed scnrnextLine;
TODO: Create generic pet using petName, petAge and then call printInfo
TODO: Create dog pet using dogName, dogAge, dogBreed and then call printInfo
TODO: Use getBreed to output the breed of the dog
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
