30.0k views
3 votes
Complete the methods in the StudentProcessor class. The main method should ask the user to input the name of an input file and also an output file. The main method then calls the readFile method which reads the file. Each line of the file is a student record (an input file is included in the project folder). Each line is read and a student object is created and placed in an ArrayList. The method returns the ArrayList. The main method then calls the writeFile method which writes all students with a gpa of at least 3.0 to the specified output file in alphabetical order. The format for writing should be one student per line. Last name first in a field of width 10, left justified. First name in a field width of 10, left justified. Gpa in a field width of 5 with two decimal places displayed, right justified. DO NOT change the Student class in any way. DO NOT change any method headers. You do not need to do any error checking, you will add this in the next part.

User Yoarthur
by
4.6k points

2 Answers

5 votes

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();

}

}

User Steinbitglis
by
4.5k points
3 votes

Answer:

See explaination

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));

}

}

User Bismark
by
4.6k points