Question: I need help correcting this java lab please, we should use ArrayList my output is incorrect and I don't know if its because I did
I need help correcting this java lab please, we should use ArrayList my output is incorrect and I don't know if its because I did something wrong in my classes or what?
thank you in advance.
Question: Class Pet has name, age, weight and breed as attributes. Give your class a default and another constructor that initializes all attributes. Add the necessary set and get methods in addition to a toString method.
Create a new class called Dog that is derived from the Pet class. The new class Dog has the additional attribute 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, you should also read 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.
public class Pet { private String name , breed ; private double weight ; private int age ; public Pet(String name, String breed, double weight, int age) { this.name = name; this.breed = breed; this.weight = weight; this.age = age; } public Pet() { name = "" ; age = 0 ; weight = 0 ; breed = "" ; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBreed() { return breed; } public void setBreed(String breed) { this.breed = breed; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "Pet{" + "name='" + name + '\'' + ", breed='" + breed + '\'' + ", weight=" + weight + ", age=" + age + '}'; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Dog extends Pet { private boolean BoosterShot ; public Dog() { super(); } public Dog(String name, String breed, double weight, int age, boolean BoosterShot) { super(name, breed, weight, age); BoosterShot = this.BoosterShot; } public boolean GetBoosterShot() { return BoosterShot; } public void setBoosterShot(boolean boosterShot) { BoosterShot = boosterShot; } public String toString() { return "Dog{" + "BoosterShot=" + BoosterShot + '}'; } //////////////////////////////////////////////////// public class MainPet { public static void main(String[] args) { Scanner input = new Scanner(System.in) ; ArrayList list = new ArrayList() ; Dog info = new Dog("silvester" , "pug" , 6.5 , 9 , true); Dog dog2 = new Dog("lilly" , "chiuaua" , 3.5 , 1 , false); list.add(info); list.add(dog2); info.setBoosterShot(true); list.toString(); //System.out.println( info.GetBoosterShot() +" name is "+info.getName()+" "+ info.getAge() +" "+ info.getBreed() ); for(int i=0;i 2 && list.get(i).GetBoosterShot() == false) { System.out.println("Dog name: " + list.get(i).getName() + " Breed: " + list.get(i).getBreed()); } // if } // for } // main } // class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
