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

Hello, Anyone please help me with this. I know for this we need to use the compa

ID: 3709339 • Letter: H

Question

Hello, Anyone please help me with this.

I know for this we need to use the compare method but it is Kind of really confusing to me. Really appreciate the help Thanks in advance.

Classy Aristocrats
In his memoir So, Anyway, comedian John Cleese writes of the class difference between his father
(who was middle-middle-middle-lower-middle class and his mother (who was upper-upper-
lower-middle class). These fine distinctions between classes tend to confuse American readers, so
you are to write a program to sort a group of people by their classes to show the true distinctions.
For this problem, there are three main classes: upper, middle, and lower. Obviously, the highest is
upper and the lowest is lower. But there can be distinctions within a class, so upper-upper is a
higher class than middle-upper, which is higher than lower-upper. However, all of the upper
classes (upper-upper, middle-upper, and lower-upper) are higher than any of the middle classes.
Within a class like middle-upper, there can be further distinctions as well, leading to classes like
lower-middle-upper-middle-upper. When comparing classes, once you've reached the lowest level
of detail, you should assume that all further classes are the same as the middle level of the
previous level of detail. So upper class and middle-upper class are equivalent, as are middle-
middle-lower-middle and lower-middle.
The Input
The first line of input will indicate the number of test cases. For each test case that follows, the
first line of input contains n, the number of names to follow. Each of the following n lines
contains the name of a person as a sequence of 1 or more lower case letters, then a colon, then a
single space, and then the class of the person. The class of the person will include one or more
modifiers, separated by a single space, followed by a single space and then the word class. The
entire line will be no longer than 256 characters.
The Output
For each test case, the output will consist of a list of names from highest to lowest class, one name
per line. If two people have the same class, they should be listed in alphabetical order by name.
Print one blank line after each test case.Sample Input
2
5
mom: upper upper lower middle class
dad: middle middle lower middle class
queenelizabeth: upper upper class
chair: lower lower class
unclebob: middle lower middle class
10
rich: lower upper class
mona: upper upper class
dave: middle lower class
charles: middle class
tom: middle class
william: lower middle class
carl: lower class
violet: middle class
frank: lower class
mary: upper class
Sample Output
queenelizabeth
mom
dad
unclebob
chair
mona
mary
rich
charles
tom
violet
william
carl
dave
frank

Explanation / Answer

Below is your code. Please let me know in comments if you have any issues....

public class SocialClass {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

int times = keyboard.nextInt();

for (int t = 0; t < times; t++) {

// read n from user

int n = keyboard.nextInt();

keyboard.nextLine();

int maxLevels = 0;

// Strings taken directly from user

String[] rawData = new String[n];

final String[] names = new String[n];

// read Data from user

for (int i = 0; i < n; i++) {

rawData[i] = keyboard.nextLine();

}

// find maximum never of levels

for (int i = 0; i < n; i++) {

if (rawData[i].split(" ").length - 2 > maxLevels) {

maxLevels = rawData[i].split(" ").length - 2;

}

}

// creating array to store levels of classes

int[][] levels = new int[n][maxLevels];

// Parsing and storing data from rawData to levels and names array

for (int i = 0; i < n; i++) {

String[] x = rawData[i].split(" ");

names[i] = x[0].substring(0, x[0].length() - 1);

for (int j = 0; j < maxLevels; j++) {

int len = x.length - 2;

if (j < len) {

if (x[len - j].equals("upper")) {

levels[i][j] = 3;

}

if (x[len - j].equals("middle")) {

levels[i][j] = 2;

}

if (x[len - j].equals("lower")) {

levels[i][j] = 1;

}

} else {

// middle class

levels[i][j] = 2;

}

}

}

/*

* Gives a relative rank based on classes of different persons If

* both persons have same class then they will be assigned equal

* rank. Rank is assigned according to a weight with higher weight

* of upper levels and lower weight of lower levels So basically

* rank is weight multiplied by class number class number are as

* follows upper class - 3 middle class - 2 lower class - 1

*/

int[][] rankScore = new int[n][2];

for (int i = 0; i < n; i++) {

for (int j = 0; j < maxLevels; j++) {

rankScore[i][0] = i;

rankScore[i][1] += (maxLevels - j) * levels[i][j];

}

}

// This is used to sort the array rankScore according to our rules

// i.e we want higher rank on top and people will same rank should

// come in

// Alphabetical order

Arrays.sort(rankScore, new Comparator<int[]>() {

public int compare(int[] o1, int[] o2) {

final int aScore = o1[1];

final int bScore = o2[1];

final int aPosition = o1[0];

final int bPosition = o2[0];

if (aScore != bScore) {

return Integer.compare(bScore, aScore);

} else {

return names[aPosition].compareTo(names[bPosition]);

}

}

});

// Finally print the names using sorted indexes from rankScore

for (int i = 0; i < n; i++) {

System.out.println(names[rankScore[i][0]]);

}

System.out.println();

}

}

}

Output

2
5
mom: upper upper lower middle class
dad: middle middle lower middle class
queenelizabeth: upper upper class
chair: lower lower class
unclebob: middle lower middle class
queenelizabeth
mom
dad
unclebob
chair

10
rich: lower upper class
mona: upper upper class
dave: middle lower class
charles: middle class
tom: middle class
william: lower middle class
carl: lower class
violet: middle class
frank: lower class
mary: upper class
mona
mary
rich
charles
tom
violet
william
carl
dave
frank

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