Question: Need help with just a couple Java Programming problems: For this assignment you will write a two classes: Animal and Bird . Animal which represents
Need help with just a couple Java Programming problems:
For this assignment you will 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 below.
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.
Animal.java:
package animals;
import java.time.LocalDate;
public class Animal implements Comparable
}
--------------------------------------------------------------------------------------------------------------------------------------------
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.
Bird.java:
package animals;
public class Bird extends Animal { private boolean canFly; public Bird() {
} public Bird(String n, String l, int year, boolean flight) {
} public String toString() { // replace this stub with the correct implementation return "A Bird"; }
}
-----------------------------------------------------------------------------------------------------------------------------------------------
I have provided a driver program TestAnimals.java to help you test your code.
TestAnimals.java:
package test;
import animals.*;
public class TestAnimals {
public static void main(String[] args) { Animal[] pets = new Animal[5]; pets[0] = new Animal(); pets[1] = new Animal("Djengo", "MEOW", "cat", 2000); pets[2] = new Animal("Future", "woof", "dog", 2019); pets[3] = new Bird(); pets[4] = new Bird("Sweets", "tweet", 2016, false); System.out.println("The animals are: "); for (int i = 0; i < pets.length; i++) { System.out.println(pets[i]); } System.out.println(); System.out.println("The largest Animal is: "); int index = findMax(pets); System.out.println(pets[index]);
} public static
}
--------------------------------------------------------------------------------------------------------------------------------------------------
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
Get step-by-step solutions from verified subject matter experts
