233k views
21 votes
The cat store needs your help! The base class Animal has private fields animalName, and animalAge. The derived class Cat extends the Animal class and includes a private field for catBreed. Complete main() to:

create a generic animal and print information using printInfo().
create a Cat animal, use printInfo() to print information, and add a statement to print the cat's breed using the getBreed() method.
Ex. If the input is:
Dobby
2
Kreacher
3
Maine Coon
the output is:
Pet Information:
Name: Dobby
Age: 2
Pet Information:
Name: Kreacher
Age: 3
Breed: Maine Coon
AnimalIformation.java
import java.util.Scanner;
public class AnimalInformation {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Animal myAnimal = new Animal();
Cat myCat = new Cat();
String animalName, catName, catBreed;
int animalAge, catAge;
animalName = scnr.nextLine();
animalAge = scnr.nextInt();
scnr.nextLine();
catName = scnr.next();
catAge = scnr.nextInt();
scnr.nextLine();
catBreed = scnr.nextLine();
// TODO: Create generic animal (using animalName, animalAge) and then call printInfo
// TODO: Create cat animal (using catName, catAge, catBreed) and then call printInfo
// TODO: Use getBreed(), to output the breed of the cat
}
}
Animal.java
public class Animal {
protected String animalName;
protected int animalAge;
public void setName(String userName) {
animalName = userName;
}
public String getName() {
return animalName;
}
public void setAge(int userAge) {
animalAge = userAge;
}
public int getAge() {
return animalAge;
}
public void printInfo() {
System.out.println("Animal Information: ");
System.out.println(" Name: " + animalName);
System.out.println(" Age: " + animalAge);
}
}
Cat.java
public class Cat extends Animal {
private String catBreed;
public void setBreed(String userBreed) {
catBreed = userBreed;
}
public String getBreed() {
return catBreed;
}
}

1 Answer

12 votes

Answer:

Answered below

Step-by-step explanation:

//TODO1

myAnimal.setName(animalName);

myAnimal.setAge(animalAge);

myAnimal.printInfo();

//TODO2

myCat.setName(catName);

myCat.setAge(catAge);

myCat.setBreed(catBreed);

myCat.printInfo();

//TODO

//Call the getter method of the cat class and //save the returned string in a variable to print //it out.

String breed = myCat.getBreed();

System.out.print(breed);

User Zmt
by
3.6k points