Question

In: Computer Science

Create a class to represent a Mammal object that inherits from (extends) the Animal class. View...

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/

Solutions

Expert Solution

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;
   }
  
}


Related Solutions

In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following:...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following: a variable roof that holds type of roof (ex: convertible, hard-top,softtop) a variable doors that holds the car's number of doors(ex: 2,4) implement the changespeed method to add 20 to the speed each time its called add exception handling to the changespeed method to keep soeed under 65 implement the sound method to print "brooom" to the screen. create one constructor that accepts the...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature...
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature { public void method1() { System.out.println("creature 1"); } public void method2() { System.out.println("creature 2"); } public String toString() { return "ocean-dwelling"; } } public class Whale extends Mammal { public void method1() { System.out.println("spout"); } public String toString() { return "BIG!"; } } public class Squid extends SeaCreature { public void method2() { System.out.println("tentacles"); } public String toString() { return "squid"; } } What...
In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...
In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now that you have successfully created Trivia objects, you will continue 6B by creating a linked list of trivia objects. Add the linked list code to the Trivia class. Your linked list code should include the following: a TriviaNode class with the attributes: 1. trivia game - Trivia object 2. next- TriviaNode 3. write the constructor, accessor, mutator and toString methods. A TriviaLinkedList Class which...
Package pacman.score Class ScoreBoard Object ScoreBoard public class ScoreBoard extends Object ScoreBoard contains previous scores and...
Package pacman.score Class ScoreBoard Object ScoreBoard public class ScoreBoard extends Object ScoreBoard contains previous scores and the current score of the PacmanGame. A score is a name and value that a valid name only contains the following characters: A to Z a to z 0 to 9 and must have a length greater than 0. The value is a integer that is equal to or greater than 0. Implement this for Assignment 1 Constructor Summary Constructors Constructor Description ScoreBoard() Creates...
Package pacman.score Class ScoreBoard Object ScoreBoard public class ScoreBoard extends Object ScoreBoard contains previous scores and...
Package pacman.score Class ScoreBoard Object ScoreBoard public class ScoreBoard extends Object ScoreBoard contains previous scores and the current score of the PacmanGame. A score is a name and value that a valid name only contains the following characters: A to Z a to z 0 to 9 and must have a length greater than 0. The value is a integer that is equal to or greater than 0. Implement this for Assignment 1 Constructor Summary Constructors Constructor Description ScoreBoard() Creates...
In java: -Create a class named Animal
In java: -Create a class named Animal
Animal class Create a simple class called Animal instantiated with a name and a method toString...
Animal class Create a simple class called Animal instantiated with a name and a method toString which returns the name. Cat class Create a simple class Cat which extends Animal, but adds no new instance variable or methods. RedCat class Create a simple class RedCat which extends Cat, but adds no new instance variable or methods. WildCardTester class Create a class with the main method and methods addCat, deleteCat and printAll as follows. addCat method Has two parameters, an ArrayList...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT