Main Method:
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class Assginment { public static void main(String[] args) throws FileNotFoundException, IOException { FileReader f = new FileReader(args[0]); BufferedReader b = new BufferedReader(f); String line = ""; ArrayList students=new ArrayList(); while((line=b.readLine())!= null) { String [] spt=line.split(","); if(spt[3].equals("graduate") == true) { students.add(new GraduateStudent(spt[0],Integer.parseInt(spt[1]),Float.parseFloat(spt[2]),spt[4])); } if(spt[3].equals("undergraduate") == true) { students.add(new UndergraduateStudent(spt[0],Integer.parseInt(spt[1]),Float.parseFloat(spt[2]),Boolean.parseBoolean(spt[4]))); } } b.close(); FileWriter writer = new FileWriter(args[1]); PrintWriter outWriter = new PrintWriter(writer); for(int i=students.size()-1; i>=0; i--) { System.out.println(" Invalid input:"); if(students.get(i) instanceof UndergraduateStudent) { UndergraduateStudent s=(UndergraduateStudent) students.get(i); s.printStudent(); } if(students.get(i) instanceof GraduateStudent) { GraduateStudent s=(GraduateStudent) students.get(i); s.printStudent(); } } } }
General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike assignment 5, the Student objects are sorted before they are written to the output file. The program must implement a main class, three student classes (Student, UndergradStudent, GradStudent), and a Comparator class called StudentIDComparator. The Student ID Comparator class must implement the java.util.Comparator interface, and override the compare() method. Since the Comparator interface is a generic interface, you must specify Student as the concrete type. The signature of the compare method be public int compare(Studeni sl, Student s2). The compare() method returns a negative, 0, or positive value if sl is less than, equals, or is greater than s2, respectively. To sort the ArrayList, you need to create a StudentIDComparator object and use it in the Collections' sort method: Student IDComparator idSorter - new Student IDComparator(); Collections. sort(students, idSorter); //students is an arrayList of Students IV. Bonus features (optional) You may implement as many bonus features as you want. If you implement any bonus feature in your program, please put a comment in your Blackboard submission and in your main Java file. In your comments, explain which bonus feature(s) you implemented. 1. (15 points) Your program will ask the user which data field is used to sort the students, name, ID, or gpa. It will then sort the students accordingly and write the sorted list to an output file provided at command line. A sample run may look like this: Which field should be used to sort Students (1-3): 1. Name 2. ID 3. GPA Which field should be used to sort Students (1-3): 1. Name 2. ID 3. GPA Which field should be used to sort Students (1-3): 1. Name 2. ID 3. GPA students.txt James Bond, 200304,3.2, undergraduate, true Michelle Chang, 200224,3.3, graduate, cleveland State University Tayer Smoke, 249843,2.4, undergraduate, false David Jones, 265334,2.7, undergraduate, true Abby Wasch, 294830, 3.6, graduate, West Virginia Nancy Drew, 244833,2.9, graduate, Case Western Lady Gaga, 230940,3.1, undergraduate, false Sam Jackson, 215443,3.9, graduate, Ohio State University The UML class diagram should be as follows. Student -name: String -id: int -gpa: float +Student +Student(name,id, gpa) +pring Student():void +getID:int +printStudent (Print Writer output:void UndegradStudent -boolean: is Transfer +UndergradStudent(name,id, gpa, is Transfer) +pring Student:void #printStudent(Print Writer output):void GradStudent -college:String +GradStudent(name,id, gpa.college) +pring Student():void +printStudent Print Writer output):void