create a new class called Dog that is derieved from the Pet class. The new class
ID: 3546104 • Letter: C
Question
create a new class called Dog that is derieved from the Pet class. The new class has additional attributes of breed (type String) and boosterShot(type boolean), which is true if the pet has had its booster shot and false if not. Give your classes a reasonable complement of constructors and accessor methods. Write a driver program to test all your methods, then write a program that reads in five pets of type Dog and displays the name and breed of all dogs that are over two years old and have not had their booters shots.
This is what i have so far.
public class Pet {
public String name;
private int age; //in years
private double weight;//in pounds
public Pet() {
name = " No name yet.";
age = 0;
weight = 0;
}
public Pet(String initialName, int initialAge,
double initialWeight) {
name = initialName;
if ((initialAge < 0) || (initialWeight < 0)) {
System.out.println("Error: Negative age or weight.");
System.exit(0);
} else {
age = initialAge;
weight = initialWeight;
}
}
public void setPet(String newName, int newAge,
double newWeight) {
name = newName;
if ((newAge < 0) || (newWeight < 0)) {
System.out.println("Error: Negative age or weight.");
System.exit(0);
} else {
age = newAge;
weight = newWeight;
}
}
public Pet(String initialName) {
name = initialName;
age = 0;
weight = 0;
}
public void setName(String newName) {
name = newName; //age and weight are unchanged.
}
public Pet(int initialAge) {
name = "No name yet.";
weight = 0;
if (initialAge < 0) {
System.out.println("Error: Negative age.");
System.exit(0);
} else
age = initialAge;
}
public void setAge(int newAge) {
if (newAge < 0) {
System.out.println("Error: Negative age.");
System.exit(0);
} else
age = newAge;
//name and weight are unchanged.
}
public Pet(double initialWeight) {
name = "No name yet";
age = 0;
if (initialWeight < 0) {
System.out.println("Error: Negative weight.");
System.exit(0);
} else
weight = initialWeight;
}
public void setWeight(double newWeight) {
if (newWeight < 0) {
System.out.println("Error: Negative weight.");
System.exit(0);
} else
weight = newWeight; //name and age are unchanged.
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getWeight() {
return weight;
}
public void writeOutput() {
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Weight: " + weight + " pounds");
}
}
public class Dog extends Pet{
String breed;
boolean boosterShot;
public Dog(){
super();
breed = "None";
boosterShot = false;
}
Explanation / Answer
import java.util.*;
public class Pet
{
public String name;
private int age; //in years
private double weight;//in pounds
public Pet() {
name = " No name yet.";
age = 0;
weight = 0;
}
public Pet(String initialName, int initialAge,
double initialWeight) {
name = initialName;
if ((initialAge < 0) || (initialWeight < 0)) {
System.out.println("Error: Negative age or weight.");
System.exit(0);
} else {
age = initialAge;
weight = initialWeight;
}
}
public void setPet(String newName, int newAge,
double newWeight) {
name = newName;
if ((newAge < 0) || (newWeight < 0)) {
System.out.println("Error: Negative age or weight.");
System.exit(0);
} else {
age = newAge;
weight = newWeight;
}
}
public Pet(String initialName) {
name = initialName;
age = 0;
weight = 0;
}
public void setName(String newName) {
name = newName; //age and weight are unchanged.
}
public Pet(int initialAge) {
name = "No name yet.";
weight = 0;
if (initialAge < 0) {
System.out.println("Error: Negative age.");
System.exit(0);
} else
age = initialAge;
}
public void setAge(int newAge) {
if (newAge < 0) {
System.out.println("Error: Negative age.");
System.exit(0);
} else
age = newAge;
//name and weight are unchanged.
}
public Pet(double initialWeight) {
name = "No name yet";
age = 0;
if (initialWeight < 0) {
System.out.println("Error: Negative weight.");
System.exit(0);
} else
weight = initialWeight;
}
public void setWeight(double newWeight) {
if (newWeight < 0) {
System.out.println("Error: Negative weight.");
System.exit(0);
} else
weight = newWeight; //name and age are unchanged.
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getWeight() {
return weight;
}
public void writeOutput() {
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Weight: " + weight + " pounds");
}
}
public class Dog extends Pet{
String breed;
boolean boosterShot;
public Dog(){
super();
breed = "None";
boosterShot = false;
}
public Dog(String newBreed, boolean boosterShot)
{
this.boosterShot = boosterShot;
breed = newBreed;
}
public Dog(String name,int age,int weight,String breed,boolean boosterShot){
super(name,age,weight);
this.breed = breed;
this.boosterShot = boosterShot;
}
public String getBreed() {
return breed;
}
public boolean isBoosterShot() {
return boosterShot;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void setBoosterShot(boolean boosterShot)
{
this.boosterShot = boosterShot;
}
}
public class DogDriver
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Dog[] D_array = new Dog[5];
for(int i=0; i<5; i++)
{
System.out.println("Enter name of Dog :");
String name = in.next();
System.out.println("Enter Age of Dog :");
int age = in.nextInt();
System.out.println("Enter Weight of Dog :");
int weight = in.nextInt();
System.out.println("Enter name of breed of Dog :");
String breed = in.next();
System.out.println("is Dog has boosterShot (enter true or false) ");
String booster = in.next();
D_array[i] = new Dog(name,age,weight,breed,booster.equalsIgnoreCase("true"));
} //end for ....
for(int i=0; i<5; i++)
{
// check for age greater than 2 and doesnt have its booster shot.
if(D_array[i].getAge()>=2 && !D_array[i].isBoosterShot())
{
System.out.println("Name of Dog is " + D_array[i].getName());
System.out.println("breed of Dog is " + D_array[i].getBreed());
}
} // end for
} // end main
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.