subject

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
(File is marked as read-only)
Dog. java
public class Dog extends Pet {
private String dogBreed;
public void setBreed(String userBreed) {
dogBreed = userBreed;
}
public String getBreed() {
return dogBreed;
}
}
(File is marked as read-only)
Pet. java
public class Pet {
protected String petName;
protected int petAge;
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);
}
}
PetInformation. java
import java. util. Scanner;
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
myPet. setPetName(petName);
myPet. setPetAge(petAge);
myPet. printInfo();
// TODO: Create dog pet (using dogName, dogAge, dogBreed) and then call printInfo
myDog. setPetName(dogName);
myDog. setPetAge(dogAge);
myDog. setDogBreed(dogBreed);
myDog. printInfo();
// TODO: Use getBreed(), to output the breed of the dog
System. out. println(" Breed: " + myDog. getDogBreed());
}
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 18:30
Which cultural aspect does this type of song best portray? a german polka dance
Answers: 1
question
Computers and Technology, 23.06.2019 08:30
When you interpret the behavior of others according to your experiences and understanding of the world your evaluation is
Answers: 1
question
Computers and Technology, 23.06.2019 14:30
Select the correct answer. sean is a computer programmer. he has programmed an application for toddlers that plays nursery rhymes. however, a logic error has occurred in the program. which problem is a likely consequence of the error? a. the program crashes every time the user wants to play the nursery rhymes. b. the program crosses its buffer boundaries and overwrites an adjacent program. c. the program plays a different nursery rhyme than the one the user intended to play. d. the program shows different structures in its programming language code. e. the program introduces new viruses every time the user plays a nursery rhyme.
Answers: 1
question
Computers and Technology, 23.06.2019 18:00
File account.java (see previous exercise) contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a string representation. note that the constructor for this class creates a random account number. save this class to your directory and study it to see how it works. then write the following additional code: 1. suppose the bank wants to keep track of how many accounts exist. a. declare a private static integer variable numaccounts to hold this value. like all instance and static variables, it will be initialized (to 0, since it’s an int) automatically. b. add code to the constructor to increment this variable every time an account is created. c. add a static method getnumaccounts that returns the total number of accounts. think about why this method should be static - its information is not related to any particular account. d. file testaccounts1.java contains a simple program that creates the specified number of bank accounts then uses the getnumaccounts method to find how many accounts were created. save it to your directory, then use it to test your modified account class.
Answers: 3
You know the right answer?
The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet cla...
Questions
question
Mathematics, 05.12.2019 21:31
Questions on the website: 13722361