Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with a Java problem: For this assignment you will do two things: add t

ID: 3829704 • Letter: N

Question

Need help with a Java problem:

For this assignment you will do two things: add to the Animal class to include a new accessor and write a driver program for the new Animal class. The Animal.java file is provided below.

First you should modify the Animal class by writing an accessor for the name of the Animal object: public String getName()

Next you need to write a driver program for the Animal class. It will use the Arrays class for printing and sorting. In particular, it must:

Have a static constant for the number of Animals in the array called MAX.

Create an array of MAX Animal objects and fill them with appropriate test values using calls to the parameterized constructor of the Animal class.

Print the filled Animal array using the toString() method of the Arrays class. Do NOT use a loop.

Sort the Animal objects using a call to Arays.sort() that uses the Comparable interface.

Print the newly sorted Animal array using the toString() method of the Arrays class. Do NOT use a loop.

Sort the Animals using a call to Arrays.sort() with a lambda expression. The lambda expression will sort based on the name of the Animal. This lambda expression must use the name accessor that you wrote in the first step of the assignment. Strings should be compared using the compareTo() method of the String class.

Print the newly sorted Animal array using the toString() method of the Arrays class. Do NOT use a loop.

Below is the sample output that my completed solution produced. You are not required to use my particular Animal objects in your solution, but I wanted to give you a concrete example to consider.

The Animal array: [Name: Tardar Sauce, species: cat, language: meow, age: 5, Name: Djengo, species: cat, language: meow, age: 17, Name: Cookie, species: cat, language: MEOW, age: 14]
The Animal array, after sorting (using compareTo): [Name: Tardar Sauce, species: cat, language: meow, age: 5, Name: Cookie, species: cat, language: MEOW, age: 14, Name: Djengo, species: cat, language: meow, age: 17]
The Animal array, after sorting (using the lambda): [Name: Cookie, species: cat, language: MEOW, age: 14, Name: Djengo, species: cat, language: meow, age: 17, Name: Tardar Sauce, species: cat, language: meow, age: 5]

--------------------------------------------------------------------------------------------------------------------------

Animal.java:

package animals;

import java.time.LocalDate;

public class Animal implements Comparable<Animal>{
  
   protected String name;
   protected String species;
   protected String lang;
   protected int bYear;
  
   public Animal() {
       name = "Tardar Sauce";
       species = "cat";
       lang = "meow";
       bYear = 2012;
   }
  
   public Animal(String n, String l, String s, int year) {
       name = n;
       lang = l;
       species = s;
       LocalDate today = LocalDate.now();
       if (year <= today.getYear())
           bYear = year;
       else bYear = today.getYear();
   }
  
   public String toString() {
       return "Name: " + name + ", species: " + species + ", language: " + lang + ", age: " + age();
   }
  
   public int age() {
       LocalDate today = LocalDate.now();
       return today.getYear() - bYear;
   }
  
   public int compareTo(Animal other) {
       if (age() < other.age())
           return -1;
       else if (age () > other.age())
           return 1;
       else return 0;
   }

}

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

################ Animal.java #################

import java.time.LocalDate;

public class Animal implements Comparable<Animal>{

   protected String name;

   protected String species;

   protected String lang;

   protected int bYear;

   public Animal() {

       name = "Tardar Sauce";

       species = "cat";

       lang = "meow";

       bYear = 2012;

   }

   public Animal(String n, String l, String s, int year) {

       name = n;

       lang = l;

       species = s;

       bYear = year;

   }

   public String toString() {

       return "Name: "+name+", species: "+species+", language: "+lang+", age: "+age();

   }

   public int age() {

       LocalDate date = LocalDate.now();

       int current_year = date.getYear();

       return Math.abs(current_year - bYear);

   }

   public int compareTo(Animal other) {

       if(age() > other.age())

           return 1;

       else if(age() < other.age())

           return -1;

       else

           return 0;

   }

}

################ TestAnimals.java ##############

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 Bird1();

       pets[4] = new Bird1("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 <T extends Comparable<T>> int findMax(T[] values) {

       int maxPos = 0;

       for (int index = 1; index < values.length; index++ )

       {

           if (values[index].compareTo(values[maxPos]) > 0)

               maxPos = index;

       }

       return maxPos;

   }

}

/*

Sample output:

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: 2

Name: Tweety, species: bird, language: chirp, age: 75, can fly? true

Name: Sweets, species: bird, language: tweet, age: 1, can fly? false

The largest Animal is:

Name: Tweety, species: bird, language: chirp, age: 75, can fly? true

*/