I need help with this lab, please: Create a new class called Dog that is derived
ID: 3757630 • Letter: I
Question
I need help with this lab, please:
Create a new class called Dog that is derived from the Pet class given in Listing 6.1 of Chapter 6.
The Pet class has instance variables (name, age and weight), five constructors, accessor methods, mutator methods and writeOutput().
public Pet();
public Pet(String initialName, int initialAge, double initialWeight);
public Pet(String initialName);
public Pet(int initialAge);
public Pet(double initialWeight);
Fig 1: The constructors of Pet Class
The new class has the 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 booster shots.
Important
Beginning with Lab 7, your code must conform to the style described in "Supplement D - Expanded Guidelines on Programming Style and Documentation" from Y. Daniel Liang's "Introduction to Java Programming, Comprehensive Version, 10th Edition" and must contain Javadoc comments (see "Y - javadoccomments -- javadoc comments.pdf," also from Liang's book). I posted both PDFs on Blackboard.
You must run Javadoc on all of your classes - the HTML documentation will be generated in the project folder tree and must be submitted with your project.
You have some latitude regarding the style. Specifically, you may use either brace positioning, and you may insert spaces immediately within parentheses. Whichever styles you use, must be followed consistently throughout.
Failure to use this style - including javadoc comments - will result in a 25% penalty.
public Pet();
public Pet(String initialName, int initialAge, double initialWeight);
public Pet(String initialName);
public Pet(int initialAge);
public Pet(double initialWeight);
Explanation / Answer
public abstract class Pet { private String name; private int age,weight; public Pet(String n,int yr,int wgt) { this.name=n; this.age=yr; this.weight=wgt; } public void setName(String n) { name=n; } public void setAge(int yr) { age=yr; } public void setWeight(int wgt) { weight=wgt; } public String getName() { return name; } public int getAge() { return age; } public int getWeight() { return weight; } public String toString() { return "Pet Name: "+name+" Age: "+age+" Weight"+weight; } } class Dog extends Pet { private String breed; private boolean booster; public Dog(String n,int yr,int wgt,String br,boolean bst) { super(n,yr,wgt); this.breed=br; this.booster=bst; } void setBreed(String br) { breed=br; } void setBooster(boolean bst) { booster=bst; } public String getBreed() { return breed; } public boolean getBooster() { return booster; } public String toString() { return super.toString()+" Breed: "+breed+" Booster: "+booster; } } public class Test { public static String name,breed; public static int age; public static void main(String[] Args) { Dog pet1 = new Dog("Charlie",7,33,"Shit-Zu",true); Dog pet2 = new Dog("Max",4,35,"Shit-Zu",false); Dog pet3 = new Dog("Dave",1,22,"Terrier",true); Dog pet4 = new Dog("John",4,25,"Rot-Wieler",false); Dog pet5 = new Dog("Sparky",1,15,"German Shepard",true); System.out.println(pet1.toString()); System.out.println(pet2.toString()); System.out.println(pet3.toString()); System.out.println(pet4.toString()); System.out.println(pet5.toString()); } }