Question: public abstract class Animal { // instance variables for species private String species; // default constructor public Animal() { } // Constructor with parameter public
public abstract class Animal { // instance variables for species private String species; // default constructor public Animal() { } // Constructor with parameter public Animal(String species) { this.species = species; } // abstarct method abstract String sound(); abstract String movement(); // equales method public boolean equals(Object o) { if (this == o) // self check return true; if (o== null) // null check return false; if (getClass() != o.getClass()) return false; Animal other = (Animal) o; if (species == null) { if (other.species != null) return false; } else if (!species.equals(other.species)) return false; return true; } public String toString() { return "Species = " + species; } } public class Elephant1 extends Animal { private double weight; public Elephant1(String species, double weight) { super(species); this.weight = weight; } String sound() { return "Trumpet"; } String movement() { return "Walk"; } //Getters and setters public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public boolean equals(Object o) { if (this == o) return true; if (!super.equals(o)) return false; if (getClass() != o.getClass()) return false; Elephant other = (Elephant) o; if (Double.doubleToLongBits(weight) != Double.doubleToLongBits(other.weight)) return false; return true; } public String toString() { return "Elephant: Weight = " + weight + " Sound = " + sound() + " Movement = " + movement() +" "+super.toString(); } } public class Elephant1 extends Animal { private double weight; public Elephant1(String species, double weight) { super(species); this.weight = weight; } String sound() { return "Trumpet"; } String movement() { return "Walk"; } //Getters and setters public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public boolean equals(Object o) { if (this == o) return true; if (!super.equals(o)) return false; if (getClass() != o.getClass()) return false; Elephant other = (Elephant) o; if (Double.doubleToLongBits(weight) != Double.doubleToLongBits(other.weight)) return false; return true; } public String toString() { return "Elephant: Weight = " + weight + " Sound = " + sound() + " Movement = " + movement() +" "+super.toString(); } } public class Elephant1 extends Animal { private double weight; public Elephant1(String species, double weight) { super(species); this.weight = weight; } String sound() { return "Trumpet"; } String movement() { return "Walk"; } //Getters and setters public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public boolean equals(Object o) { if (this == o) return true; if (!super.equals(o)) return false; if (getClass() != o.getClass()) return false; Elephant other = (Elephant) o; if (Double.doubleToLongBits(weight) != Double.doubleToLongBits(other.weight)) return false; return true; } public String toString() { return "Elephant: Weight = " + weight + " Sound = " + sound() + " Movement = " + movement() +" "+super.toString(); } } public class Elephant1 extends Animal { private double weight; public Elephant1(String species, double weight) { super(species); this.weight = weight; } String sound() { return "Trumpet"; } String movement() { return "Walk"; } //Getters and setters public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public boolean equals(Object o) { if (this == o) return true; if (!super.equals(o)) return false; if (getClass() != o.getClass()) return false; Elephant other = (Elephant) o; if (Double.doubleToLongBits(weight) != Double.doubleToLongBits(other.weight)) return false; return true; } public String toString() { return "Elephant: Weight = " + weight + " Sound = " + sound() + " Movement = " + movement() +" "+super.toString(); } } public class Hawk extends Animal { //instance variables for wingspan private double wingspan; //Parameterized constructor public Hawk(String species, double wingspan) { super(species); this.wingspan = wingspan; } // using the absract class String sound() { return "Shriek"; } String movement() { return "Fly"; } // Getters and Setters public double getWingspan() { return wingspan; } public void setWingspan(double wingspan) { this.wingspan = wingspan; } public boolean equals(Object o) { if (this == o) return true; if (!super.equals(o)) return false; if (getClass() != o.getClass()) return false; Hawk other = (Hawk) o; if (Double.doubleToLongBits(wingspan) != Double.doubleToLongBits(other.wingspan)) return false; return true; } public String toString() { return "Hawk: Wingspan = " + wingspan + " Sound = " + sound() + " Movement = " + movement() +" "+super.toString(); } } public class Snake extends Animal { // instance variable for snake private double length; //Parameterized constructor public Snake(String species, double length) { super(species); this.length = length; } String sound() { return "Hiss"; } String movement() { return "Slither"; } // Getters and Setters public double getLength() { return length; } public void setLength(double length) { this.length = length; } public boolean equals(Object o) { if (this == o) return true; if (!super.equals(o)) return false; if (getClass() != o.getClass()) return false; Snake other = (Snake) o; if (Double.doubleToLongBits(length) != Double.doubleToLongBits(other.length)) return false; return true; } public String toString() { return "Snake: Length = " + length + " Sound = " + sound() + " Movement = " + movement() +" "+super.toString(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
