Question: Below is my project. I am working with inheritance. I have the Animal class that is the parent and Fish and Bear that are its

Below is my project. I am working with inheritance. I have the Animal class that is the parent and Fish and Bear that are its children. On my application class I have to create an animal object to test the animal class. The type of the animal (fish or bear) is going to be defined by using the fish and bear classes but I don't know how to do that. It needs to be done randomly in the application class. Please help public class Animal { private String gender; private int strength; Animal(){ //Empty constructor } Animal(String gender,int strength){ //parameterized constructor this.gender=gender; this.strength=strength; } /*The following four methods are getters and setters for the instance variables 'gender' and 'strength'*/ public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getStrength() { return strength; } public void setStrength(int strength) { this.strength = strength; } /*This method compares the gender of the animal passed to the method to the gender of the animal this method is called upon and returns true if both the genders are same and returns false otherwise */ public boolean compareGender(Animal animal){ if(this.getGender().equals(animal.getGender())){ /*Here,we get the gender of the animal that the method is called upon using this.getGender(). Similarly,we get the gender of the animal that is passed to the method using animal.getGender().Then we compare both using .equals() method of string */ return true; //returns true if both the genders are same } else{ return false; //returns false if both the genders are not same } } /*This method compares the type of the animal passed to the method to the type of the animal this method is called upon and returns true if both the animals belong to same type and returns false otherwise */ public boolean compareType(Animal animal){ if(this.getClass()==animal.getClass()){ /* Here,we get the class of the animal the method is called upon using this.getClass(). Similarly, we get the class of the animal passed as method using animal.getClass(). We then compare them using == operator*/ return true; //returns true if both the animals belong to same type } else{ return false; //returns false if both the animals do not belong to same type } } /*This method compares the strength of the animal passed to the method to the strength of the animal this method is called upon and returns true if both the animals have same strength and returns false otherwise */ public boolean compareStrength(Animal animal){ if(this.getStrength()== animal.getStrength()){ /*Here,we get the strength of the animal that the method is called upon using this.getStrength(). Similarly,we get the strength of the animal that is passed to the method using animal.getStrength().Then, we compare both using == operator */ return true; //returns true if strengths of both the animals is same } else{ return false; //returns false if strengths of both the animals is not same } } /*This method increases the strength of the animal the method is called upon by 50*/ public void increaseStrength(){ this.setStrength(this.getStrength()+50); /*First, we get strength of the animal using this.getStrength() , add 50 to it and set the new strength to the animal using this.setStrength()*/ } /*This is a toString() method which returns the values of instance variables of the animal the function is called upon as string*/ @Override public String toString() { return "Animal{" + "gender='" + gender + '\'' + ", strength=" + strength + '}'; } }

public class Bear extends Animal{ public String toString() { return super.toString(); } }

public class Fish extends Animal{ public String toString() { return super.toString(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!