In: Computer Science
I. 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 StudentIDComparator 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 should be public int compare(Student s1, Student s2). The compare() method returns a negative, 0, or positive value if s1 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: StudentIDComparator idSorter = new StudentIDComparator(); Collections.sort(students, idSorter); //students is an arrayList of Students
heres 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
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
StudentTest.java
package c15;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Student{
private String name;
private int ID;
private double marks;
public Student(String name, int iD, double marks)
{
super();
this.name = name;
ID = iD;
this.marks = marks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public double getMarks() {
return marks;
}
public void setMarks(double marks) {
this.marks = marks;
}
}
class UndergradStudent extends Student{
private String status;
private String isUnderGraduate;
public UndergradStudent(String name, int iD, double
marks,String s,String s2) {
super(name,iD,marks);
this.status = s;
this.isUnderGraduate = s2;
}
public UndergradStudent(String array[]) {
super(array[0],Integer.parseInt(array[1]),Double.parseDouble(array[2]));
this.status = array[3];
this.isUnderGraduate =
array[4];
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getIsUnderGraduate() {
return isUnderGraduate;
}
public void setIsUnderGraduate(String
isUnderGraduate) {
this.isUnderGraduate =
isUnderGraduate;
}
}
class GradStudent extends Student{
private String status;
private String clg;
public GradStudent(String name, int iD, double
marks,String s,String s2) {
super(name,iD,marks);
this.status = s;
this.clg = s2;
}
public GradStudent(String array[]) {
super(array[0],Integer.parseInt(array[1]),Double.parseDouble(array[2]));
this.status = array[3];
this.clg = array[4];
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getClg() {
return clg;
}
public void setClg(String clg) {
this.clg = clg;
}
}
class StudentIDComparator implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
if(o1.getID()>o2.getID())
{
return 1;
}else if(o1.getID()<o2.getID())
{
return -1;
}
return 0;
}
}
public class StudentTest {
public static void main(String[] args) {
File file = new
File("student.txt"); //reading data from this file
Scanner reader;
String line="";
try {
//read file
using scanner
reader = new
Scanner(file);
ArrayList<UndergradStudent> undergraduateStudent = new
ArrayList<UndergradStudent>();
ArrayList<GradStudent> graduateStudent = new
ArrayList<GradStudent>();
while(reader.hasNextLine()){
//read line
line = reader.nextLine();
String data[] = line.split(",");
if(data[3].equalsIgnoreCase("graduate")) {
graduateStudent.add(new
GradStudent(data));
}else {
undergraduateStudent.add(new
UndergradStudent(data));
}
}
System.out.println("Successfully loaded "+graduateStudent.size()+"
Graduate records.");
System.out.println("Successfully loaded
"+undergraduateStudent.size()+" Under Graduate records.");
StudentIDComparator idSorter = new StudentIDComparator();
Collections.sort(undergraduateStudent, idSorter); //students is an
arrayList of Students
Collections.sort(graduateStudent, idSorter); //students is an
arrayList of Students
PrintWriter
outputFile = new PrintWriter("GraduateStudent.txt");
for(GradStudent
tmp : graduateStudent) {
outputFile.write(tmp.getName()+" "+tmp.getID()+"
"+tmp.getMarks()+" "+tmp.getStatus()+" "+tmp.getClg()+"\n");
}
outputFile.close();
System.out.println("Data exported successfully for
GraduateStudent!!!");
PrintWriter
outputFile2 = new PrintWriter("UnderGraduateStudent.txt");
for(UndergradStudent tmp : undergraduateStudent) {
System.out.println(tmp);
outputFile2.write(tmp.getName()+"
"+tmp.getID()+" "+tmp.getMarks()+" "+tmp.getStatus()+"
"+tmp.getIsUnderGraduate()+"\n");
}
outputFile2.close();
System.out.println("Data exported successfully for
UnderGraduateStudent!!!");
} catch (FileNotFoundException e)
{
System.out.println("file not found");
}
}
}
output
student.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
GraduateStudent.txt
Michelle Chang 200224 3.3 graduate Cleveland State
University
Sam Jackson 215443 3.9 graduate Ohio State University
Nancy Drew 244833 2.9 graduate Case Western
Abby Wasch 294830 3.6 graduate West Virginia
UnderGraduateStudent.txt
James Bond 200304 3.2 undergraduate true
Lady Gaga 230940 3.1 undergraduate false
Tayer Smoke 249843 2.4 undergraduate false
David Jones 265334 2.7 undergraduate true