Input Your output Expected output Dobby 2 Kreacher 3 Scottish Fold Pet Information: Name: Dobby Age: 2 Pet Information: Name: Kreacher Age: 3 Breed: Scottish Fold Pet Information: Name: Dobby Age: 2 Pet Information: Name: Kreacher Age: 3 Breed: Scottish Fold
public class Cat extends Pet {
private String breed;
public void setBreed(String userBreed) {
breed = userBreed;
}
public String getBreed() {
return breed;
}
}
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);
}
}
import java.util.Scanner;
public class PetInformation {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
// create a generic Pet and a Cat
Pet myPet = new Pet();
Cat myCat = new Cat();
// declare variables for pet and cat info
String petName, catName, catBreed;
int petAge, catAge;
// get input for pet and cat info
petName = scnr.nextLine();
petAge = scnr.nextInt();
scnr.nextLine(); // consume the remaining newline character
catName = scnr.nextLine();
catAge = scnr.nextInt();
scnr.nextLine(); // consume the remaining newline character
catBreed = scnr.nextLine();
// set the info for the generic Pet and print it
myPet.setName(petName);
myPet.setAge(petAge);
myPet.printInfo();
// set the info for the Cat and print it
myCat.setName(catName);
myCat.setAge(catAge);
myCat.setBreed(catBreed);
myCat.printInfo();
// print the breed of the Cat using getBreed()
System.out.println("Breed: " + myCat.getBreed());
}
}



Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images









