Question: 2 . 1 7 Lab 2 : Pet information ( derived classes ) The base class Pet has private fields petName, and petAge. The derived

2.17 Lab 2: 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
2
Kreacher
3
German Schnauzer
the output is:
Pet Information:
Name: Dobby
Age: 2
Pet Information:
Name: Kreacher
Age: 3
Breed: German Schnauzer
Dog.java
public class Dog extends Pet {
private String breed;
public void setBreed(String userBreed){
breed = userBreed;
}
public String getBreed(){
return breed;
}
}
Pet.java
public class Pet {
protected String name;
protected int age;
public void setName(String userName){
name = userName;
}
public String getName(){
return name;
}
public void setAge(int userAge){
age = userAge;
}
public int getAge(){
return age;
}
public void printInfo(){
System.out.println("Pet Information: ");
System.out.println(" Name: "+ name);
System.out.println(" Age: "+ age);
}
}
PetInformation.java
public class PetInformation {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
Pet myPet = new Pet();
Dog myDog = new Dog();
String petName, dogName, dogBreed;
int petAge, dogAge;
petName = scnr.nextLine();
petAge = scnr.nextInt();
scnr.nextLine();
dogName = scnr.next();
dogAge = scnr.nextInt();
scnr.nextLine();
dogBreed = scnr.nextLine();
// 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 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 Programming Questions!