Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a java problem. i've a code that prints the sample input only but i need help adding a file reader that reads a

This is a java problem. i've a code that prints the sample input only but i need help adding a file reader that reads a txt file from the hard disk. make sure to add comments on the code. I'll attch my code at the end of the question. You are welcome to write new one as long as it readas large txt file.

image text in transcribedimage text in transcribed

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

// Main class to test our program

public class Main {

/**

* List of Person objects.

*/

static List thePeople = new ArrayList();

/**

* Accepts piped input from file and orders people on their 'class'. 'Class'

* here denotes rank in society. For instance, 'upper' class individuals are

* ranked higher than 'lower' class individuals. Input from file is

* converted to integers representing class hierarchies (i.e John: upper middle

* upper lower class = 3231); This integer is then reversed to start the

* comparison of ascii values at the low index because charAt(index) begins

* at 0.

*

* @param args

* no cmd line args. Only piped file input.

*/

public static void main(String[] args) {

Scanner scanIn = new Scanner(System.in);

int numOfPeople = scanIn.nextInt();

for (int i = 0; i

String name = scanIn.next();

name = name.substring(0, name.length() - 1);

StringBuilder sb = new StringBuilder();

while (!scanIn.hasNext("class")) {

int classToInt;

String theClass = scanIn.next();

if (theClass.equals("upper"))

classToInt = 3;

else if (theClass.equals("middle"))

classToInt = 2;

else

classToInt = 1;

sb.append(classToInt);

}

scanIn.next();

sb.reverse();

thePeople.add(new Person(name, sb));

}

scanIn.close();

Collections.sort(thePeople, new ClassyComparator());

for (int i = 0; i

System.out.println(thePeople.get(i).myName);

}

}

}

////////////////////////////////////////////////////

// a person class to construct and sets the initial value

public class Person {

// creats a name filed.

String myName;

// creats Class.

StringBuilder myClass;

/**

* Constructor that sets initial values.

*

* @param theName the name of the person.

* @param theClass the class of the person.

*/

public Person(String theName, StringBuilder theClass) {

myName = theName;

myClass = theClass;

}

}

////////////////////////////////////////////////

import java.util.Comparator;

public class ClassyComparator implements Comparator {

/**

* Compares Person's on class. If classes are the same,

* then orders Person objects on lexicographical order.

*/

@Override

public int compare(Person p1, Person p2) {

int classCompare = compareClasses(p1, p2);

if (classCompare

return 1;

} else if (classCompare == 0) {

int nameCompare = p1.myName.compareTo(p2.myName);

if (nameCompare

return -1;

} else {

return 1;

}

} else {

return -1;

}

}

/**

* Compares Person objects on class. If lengths of the class hierarchies are different, a series of '2's'

* are appended to the shorter person's class as required by the documentation. The string

* class is used to determine lexicographic order once the modifications are made (if any).

*

* @param p1 person 1.

* @param p2 person 2.

* @return integer representing the relative positioning of Person objects on class. '-1' if

* p1 comes before p2, '0' if p1 and p2 are the same, and '1' if p1 comes after p2.

*/

private int compareClasses(Person p1, Person p2) {

StringBuilder p1Temp = new StringBuilder(p1.myClass.toString());

StringBuilder p2Temp = new StringBuilder(p2.myClass.toString());

int p1Length = p1Temp.length();

int p2Length = p2Temp.length();

int difference = Math.abs(p1Length - p2Length);

if (p1Length > p2Length) {

for (int i = 0; i

p2Temp.append(2);

}

} else if (p1Length

for (int i = 0; i

p1Temp.append(2);

}

}

return p1Temp.toString().compareTo(p2Temp.toString());

}

}

Classy 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 There are three main classes upper, middle, and lower. Obviously, upper class is the highest and lower class is the lowest. 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 equivalent to 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. Input The first line of input contains n (1 S n S 1,000), the number of names to follow. Each of the following n lines contains the name of a person (a sequence of 1 or more lowercase letters z'-z') a colon, a space, and then the class of the person. The class of the person will include one or more modifiers and then the word class The colon, modifiers, and the word class will be separated from each other by single spaces. All modifiers are one of upper, middle, or lower. It is guaranteed that the input is well-formed. Additionally, no two people have the same name. Input lines are no longer than 256 characters. Output Print the n names, each on a single line, from highest to lowest class. If two people have equivalent classes, they should be listed in alphabetical order by name

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How is communication defi ned?

Answered: 1 week ago