Question: 10.14 L9Num1 The base class Pet has private fields petName, and petAge. Complete the derived class Dog , which extends the Pet class and includes
10.14 L9Num1
The base class Pet has private fields petName, and petAge. Complete the derived class Dog , which extends the Pet class and includes a private field for dogBreed.
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
PetInformation.java
import java.util.Scanner;
public class PetInformation { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);
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(); Pet myPet = new Pet(petName, petAge); Dog myDog= new Dog(dogName, dogAge, dogBreed); myPet.printInfo(); myDog.printInfo();
//new dog myDog.setName("Thor"); myDog.setBreed("Boxer"); myDog.setAge(1); myDog.printInfo(); } }
Pet.java
public class Pet {
protected String petName; protected int petAge; //constructors public Pet() { petName="None yet"; petAge=0;} public Pet(String n, int a) {petName = n; petAge=a; } public void setName(String userName) { petName = userName; }
public String getName() { return petName; }
public void setAge(int userAge) { petAge = userAge; }
public int getAge() { return petAge; }
public void printInfo() { System.out.println("Pet Information: "); System.out.println(" Name: " + petName); System.out.println(" Age: " + petAge); }
}
Dog.java
public class Dog extends Pet { private String dogBreed;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
