In: Computer Science
Create a class to represent a Mammal object that inherits from (extends) the Animal class.
View javadoc for the Mammal class and updated Animal class in homework 4
http://comet.lehman.cuny.edu/sfakhouri/teaching/cmp/cmp326/s19/hw/hw4/
Use the description provided below in UML.
Mammal - tailLength : double - numLegs : int + Mammal() + Mammal(double, int) + Mammal(String, int, double, double, char, double, int) //pass values to parent’s overloaded constructor //and assign valid values to tailLength, numLegs or -1 if invalid + setTailLength(double) : void //if the value is invalid set tailLength to -1 + getTailLength() : double + setNumLegs(int) : void //if the value is invalid set numLegs to -1 + getNumLegs() : int + printDetails() : void @Override //see Note1: + toString() : String @Override //see Note1: + equals(Object) : boolean @Override //see Note2: NOTE1: For both the printDetails() and toString() methods include the data from the Animal class, as well as Mammal attributes. Format the Mammal data as “Mammal: Tail Length: %10.2f | Number of Legs: %4d\n” Hint: Use super.printDetails() and super.toString() NOTE2: For the equals(Object) method, two Mammal objects are considered equal if the parent class’s equals method returns true, if their numLegs are equal, and their tailLength values are within .1 of each other
View javadoc for the Mammal class and updated Animal class in homework 4
http://comet.lehman.cuny.edu/sfakhouri/teaching/cmp/cmp326/s19/hw/hw4/
public class Mammal extends Animal{
//class instance variables
private double tailLength;
private int numLegs;
//default constructor
public Mammal() {
super();
//consider default values of the
class parameters
this.tailLength = 5;
this.numLegs = 4;
}
//parameterized constructor
public Mammal(double tailLength, int numLegs)
{
super();
this.numLegs = numLegs;
this.tailLength = tailLength;
}
//parameterized constructor
public Mammal(String name, int birthYear, double
weight, double height, char gender, double tailLength, int
numLegs)
{
super(name, birthYear, weight,
height);
this.numLegs = numLegs;
this.tailLength = tailLength;
}
//getter and setter methods
public double getTailLength() {
return tailLength;
}
public void setTailLength(double tailLength) {
if(tailLength < 0)
this.tailLength = -1;
this.tailLength = tailLength;
}
public int getNumLegs() {
return numLegs;
}
public void setNumLegs(int numLegs) {
if(numLegs < 0) this.numLegs =
-1;
this.numLegs = numLegs;
}
//method printDetails
void printDetails() {
//calling method of parent
super.printDetails();
System.out.println("Tail Lengths: "
+ getTailLength());
System.out.println("Number of Legs:
" + getNumLegs());
}
//method toString
public String toString(){
//calling method of parent
String res =
super.toString();
res += "Tail Lengths: " +
getTailLength() + "\n";
res += "Number of Legs: " +
getNumLegs();
return res;
}
//equals method
public boolean equals(Mammal data) {
if(super.equals(data) &&
this.getNumLegs() == data.getNumLegs() &&
this.getTailLength() == data.getTailLength())
return
true;
return false;
}
}