Implement compareTo method

Report a typo

For the example given in the topic about people, implement the compareTo method. It should compare people by name, and if they have the same name, compare them by age.

public class Person {
    private String name;
    private int age;
    private int height;
    private int weight;
    
    // constructor

    // getters, setters
}

Sample Input 1:

Tom-22-185-65
Bob-22-175-85
Kris-30-180-90

Sample Output 1:

Bob 22 175 85
Kris 30 180 90
Tom 22 185 65
Write a program in Java 17
class Person implements Comparable<Person> {
private String name;
private int age;
private int height;
private int weight;

public Person(String name, int age, int height, int weight) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}

public String getName() {
return this.name;
}

public int getAge() {
return this.age;
}

public int getHeight() {
return this.height;
}

public int getWeight() {
return this.weight;
}

@Override
public int compareTo(Person otherPerson) {
// add your code here!
}
}
___

Create a free account to access the full topic