Question
Java class inheritence. write a two classes: Animal and Bird. Animal which represents a pet, and Bird is a subclass of Animal specific to pet
Java class inheritence.
write a two classes: Animal and Bird. Animal which represents a pet, and Bird is a subclass of Animal specific to pet birds.
The Animal class has four protected instance variables: name which is a String, species which is a String, lang which is a String, and bYear which is an int. The Animal class also implements the Comparable interface which compares Animal objects using their ages. The Bird class is a subclass of Animal and has a single instance variable: canFly which is a boolean. If its value is true, then the bird can fly. Otherwise the bird cannot fly. A template for the Animal and Bird classes can be found in the assignment zip file posted to D2L.
for the first part of the assignment you will complete the Animal class by writing the following methods: public Animal(): The default constructor for the class. It sets the name to "Tardar Sauce", the species to "cat", the language to "meow", and the birth year to 2012. (Because who wouldn't want your default pet to be Grumpy Cat?) public Animal(String n, String l, String s, int year): The parameterized constructor for the class. It sets the name, language, and species using the first three parameters without doing any error checking. It creates a LocalDate object to get the current year. If the year provided as a parameter is less than or equal to the current year, it uses the parameter to set the birth year. Otherwise it sets the birth year to the current year (obtained from the LocalDate object). No years should be hard-coded into this method. public int age(): This method returns the age of the Animal. It does this by creating a LocalDate object to obtain the current year and then subtracting the birth year from the current year. The resulting (non-negative) value is returned. No years should be hard-coded into this method. public String toString(): This method returns a String that displays the Animal's name, species, language, and age. See the output from the driver program below to see examples of this. Note that the age must be computed via a call to the age method. Solutions that duplicate the work of the age method rather than calling the age method will earn very little credit. public int compareTo(Animal other): This method returns 1 if the calling Animal is older than the parameter Animal. It returns -1 if the calling Animal is younger than the parameter Animal. It returns 0 if the two Animal objects have the same age. Note that the age must be computed via a call to the age method. Solutions that duplicate the work of the age method rather than calling the age method will earn very little credit.
For the second part of the assignment, you will complete the Bird class by writing the following methods: public Bird(): The default constructor for the class. It sets the name to "Tweety", the species to "bird", the language to "chirp", the year to 1942, and the canFly variable to true. The name, species, language, and year must be set using an appropriate call to the parameterized Animal constructor. Solutions that set the name, species, language, and year directly will earn very little credit. The canFly variable can and must be set directly in the Bird constructor. public Bird(String n, String l, int year, boolean flight): The parameterized constructor for the class. It sets the name, language, and year using the parameters and an appropriate call to Animal parameterized constructor. The Animal constructor should also be given "bird" as the species. Solutions that set the name, species, language, and year directly will earn very little credit. The canFly variable can and must be set directly using the parameter flight. public toString(): This method returns a String that displays the Bird's name, species, language, age, and ability to fly. See the output from the driver program below to see examples of this. Note that you must return a String that looks precisely like the examples shown below. Also note that the toString method of the Animal class must be called to get the part of the String representing the name, species, language, and age. Solutions that do not call the toString method of the Animal class will earn very little credit.
Below is the sample output that the test program produces for my solutions to the Animal and Bird classes:
The animals are: Name: Tardar Sauce, species: cat, language: meow, age: 5
Name: Djengo, species: cat, language: MEOW, age: 17
Name: Future, species: dog, language: woof, age: 0
Name: Tweety, species: bird, language: chirp, age: 75, can fly? yes
Name: Sweets, species: bird, language: tweet, age: 1, can fly? no
The largest Animal is: Name: Tweety, species: bird, language: chirp, age: 75, can fly? yes
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started