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

Java API /JavaCS2/src/comparables/Person.java /JavaCS2/src/comparables/Student.j

ID: 3539114 • Letter: J

Question

Java API /JavaCS2/src/comparables/Person.java /JavaCS2/src/comparables/Student.java /JavaCS2/src/comparables/Employee.java Suppose we want to alter the Person class so that it implements the Comparable interface. The class declaration now includes the interface Comparable as follows: public class Person implements Comparable Provide a body for the compareTo method in the space below, so that person A is considered less than person B if the age of person A age is less than the age of person B. Also, person A should be equal to person B if their ages are equal. /** * Compares this object with the specified object for order. * @param o the Object to be compared. */ public int compareTo(Object obj) { // Insert code here }

Explanation / Answer

I have written code what you required, Please see the blow code, Please test with your input.


public class Person implements Comparable<Person>{
    String name;
    int age;
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }
    @Override
    public int compareTo(Person person) {   
        if(age > person.age){
            return 1;
        }else if(age == person.age){
            return 0;
        }else{
            return -1;
        }
    }
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote