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
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.
2 1. (10 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 0
Which field should be used to sort Students(1-3):
1. Name
2. ID 3.
GPA 4
Which field should be used to sort Students(1-3):
1. Name
2. ID
3. GPA
Java Code:
package TrumpCIS265AS3;
import java.io.PrintWriter;
/**
* The Class Student.
*/
public abstract class Student {
/* The name. /
private String name;
/* The id. /
private int id;
/* The gpa. /
private float gpa;
/**
* Instantiates a new student.
*/
public Student() {
this.name = "";
this.id = 0;
this.gpa = 0;
}
/**
* Instantiates a new student.
*
* @param name the name
* @param id the id
* @param gpa the gpa
*/
public Student(String name, int id, float gpa) {
super();
this.name = name;
this.id = id;
this.gpa = gpa;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Gets the id.
*
* @return the id
*/
public int getId() {
return id;
}
/**
* Gets the gpa.
*
* @return the gpa
*/
public float getGpa() {
return gpa;
}
/**
* Prints the student.
*/
public abstract void printStudent();
/**
* Prints the student.
*
* @param output the output
*/
public abstract void printStudent(PrintWriter output);
}
/**********************************GradStudent.java***********************/
package TrumpCIS265AS3;
import java.io.PrintWriter;
/**
* The Class GradStudent.
*/
public class GradStudent extends Student {
/* The college. /
private String college;
/**
* Instantiates a new grad student.
*
* @param name the name
* @param id the id
* @param gpa the gpa
* @param college the college
*/
public GradStudent(String name, int id, float gpa, String college)
{
super(name, id, gpa);
this.college = college;
}
/**
* Gets the college.
*
* @return the college
*/
public String getCollege() {
return college;
}
@Override
public void printStudent() {
System.out.println("Student name: " + getName());
System.out.println("Student id: " + getId());
System.out.println("Student GPA: " + getGpa());
System.out.println("Student College: " + college);
}
/*
* (non-Javadoc)
*
* @see
TrumpCIS265AS3.Student#printStudent(java.io.PrintWriter)
*/
@Override
public void printStudent(PrintWriter output) {
output.write(getName() + "," + getId() + "," + getGpa()
+ "," + getCollege());
output.write("\n");
output.flush();
}
}
/***************************************UndergradStudent.java******************************/
package TrumpCIS265AS3;
import java.io.PrintWriter;
/**
* The Class UndergradStudent.
*/
public class UndergradStudent extends Student {
/* The is transfer. /
private boolean isTransfer;
/**
* Instantiates a new undergrad student.
*
* @param name the name
* @param id the id
* @param gpa the gpa
* @param isTransfer the is transfer
*/
public UndergradStudent(String name, int id, float gpa, boolean
isTransfer) {
super(name, id, gpa);
this.isTransfer = isTransfer;
}
/**
* Checks if is transfer.
*
* @return true, if is transfer
*/
public boolean isTransfer() {
return isTransfer;
}
/*
* (non-Javadoc)
*
* @see TrumpCIS265AS3.Student#printStudent()
*/
@Override
public void printStudent() {
System.out.println("Student name: " + getName());
System.out.println("Student id: " + getId());
System.out.println("Student GPA: " + getGpa());
System.out.println("Student College: " + isTransfer);
}
/*
* (non-Javadoc)
*
* @see
TrumpCIS265AS3.Student#printStudent(java.io.PrintWriter)
*/
@Override
public void printStudent(PrintWriter output) {
output.write(getName() + "," + getId() + "," + getGpa()
+ "," + isTransfer);
output.write("\n");
output.flush();
}
}
/**************************************SpidermanAssign3.java*******************/
package TrumpCIS265AS3;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* The Class SpidermanAssign3.
*/
public class SpidermanAssign3 {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
try {
Scanner scan = new Scanner(new File("students.txt"));
while (scan.hasNextLine()) {
String line = scan.nextLine();
String[] data = line.split(",");
String name = data[0];
int id = Integer.parseInt(data[1]);
float gpa = Float.parseFloat(data[2]);
String type = data[3];
boolean isTransfer;
String college;
if (type.equalsIgnoreCase("graduate")) {
college = data[4];
students.add(new GradStudent(name, id, gpa,
college));
} else {
isTransfer = Boolean.parseBoolean(data[4]);
students.add(new UndergradStudent(name, id, gpa,
isTransfer));
}
}
scan.close();
} catch (Exception e) {
}
Collections.shuffle(students);
for (Student student : students) {
student.printStudent();
}
try {
PrintWriter writer = new PrintWriter(new
FileWriter("students_shuffled.txt"));
for (Student student : students) {
student.printStudent(writer);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output :
Student name: Tayer Smoke
Student id: 249843
Student GPA: 2.4
Student College: false
Student name: Michelle Chang
Student id: 200224
Student GPA: 3.3
Student College: Cleveland State University
Student name: Abby Wasch
Student id: 294830
Student GPA: 3.6
Student College: West Virginia
Student name: David Jones
Student id: 265334
Student GPA: 2.7
Student College: true
Screenshot:
Please give me thumbs up,if you like the answer .
Please let me know if you don't understand any step before giving
Down rating directly :). Happy to help....
Get Answers For Free
Most questions answered within 1 hours.