X715: Comparing Comparators 1

For this question, we will be sorting Person objects. Here is the Person class so you’ll know what you have available:

public class Person{

    private int age;
    private String name;
    private String grade;

    public Person(int a, String n, String gr) {
        age = a;
        name = n;
        grade = gr;
    }


    public int getAge() {
        return age;
    }


    public String getGrade() {
        return grade;
    }

}

Below are three different comparator objects.

####Comparator 1:

public static final class Comparator1 implements Comparator<Person> {

    public int getFrequency(String str) {
        int counter = 0;
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == 'm' || charArray[i] == 'M') {
                counter++;
            }
        }
        return counter;
    }


    @Override
    public int compare(Person personLeft, Person personRight) {
        int leftFrequency = getFrequency(personLeft.getName());
        int rightFrequency = getFrequency(personRight.getName());
        return leftFrequency - rightFrequency;
    }

}

####Comparator 2:

public static final class Comparator2 implements Comparator<Person> {

    public int getFrequency(String str) {
        int counter = 0;
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == 'm' || charArray[i] == 'M') {
                counter++;
            }
        }
        return counter;
    }


    @Override
    public int compare(Person personLeft, Person personRight) {
        int leftFrequency = getFrequency(personLeft.getName());
        int rightFrequency = getFrequency(personRight.getName());
        return rightFrequency - leftFrequency;
    }

}

####Comparator 3:

public static final class Comparator3 implements Comparator<Person> {

    @Override
    public int compare(Person personLeft, Person personRight) {
        if (personLeft.getName().length() > personRight.getName()
            .length()) {
            return personLeft.getName().length() - personRight.getName()
                .length();
        }
        else {
            return personRight.getName().length() - personLeft.getName()
                .length();
        }
    }

}

Your task will be to choose the correct comparator that will sort Person objects by how frequently the letter “m” appears in their name. Choose the comparator that will help you sort the people from MOST times the letter “m” appears in their name to the LEAST.

For example, if the names were:

["Mum", "Molly", "Mammoth" ]

the desired order at the end would be

["Mammoth", "Mum", "Molly"]

This is because “Mammoth” has 3 of the letter “m” while “Molly” has just 1.

Call a sort method from the java Arrays class here with the correct comparator to get the desired outcome. You will be able to reference all three of these Comparators in your code below:

Your Answer:

Feedback

Your feedback will appear here when you check your answer.