Answer:
Check the explanation
Step-by-step explanation:
Student.java
public class Student implements Comparable<Student> {
private String firstName;
private String lastName;
private float gpa;
public Student(String fn, String ln, float gpa) {
firstName = fn;
lastName = ln;
this.gpa = gpa;
}
public String getFirst() {
return firstName;
}
public String getLast() {
return lastName;
}
public float getGpa() {
return gpa;
}
public int compareTo(Student other) {
return (lastName.compareTo(other.lastName));
}
}
StudnetProcessor.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class StudentProcessor {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter input filename: ");
String inFile = sc.nextLine();
ArrayList<Student> students = readFile(inFile);
System.out.print("Enter output filename: ");
String outFile = sc.nextLine();
writeFile(students, outFile);
sc.close();
}
private static ArrayList<Student> readFile(String inFile) throws NumberFormatException, IOException {
ArrayList<Student> students = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(inFile));
String st;
while ((st = br.readLine()) != null) {
String[] parts = st.split(" ");
Student student = new Student(parts[1], parts[0], Float.parseFloat(parts[2]));
students.add(student);
}
br.close();
return students;
}
private static void writeFile(ArrayList<Student> list, String outFile) throws IOException {
ArrayList<Student> students = new ArrayList<>();
for(Student student: list) {
if (student.getGpa() >= 3) {
students.add(student);
}
}
Collections.sort(students);
FileWriter myWriter = new FileWriter(outFile);
for(Student student: students) {
String str = String.format("%-10s %-10s %5.2f", student.getLast(), student.getFirst(), student.getGpa());
myWriter.write(str);
}
myWriter.close();
}
}