In this assignment, you will implement a Student Record sorter program. Your pro
ID: 3552927 • Letter: I
Question
In this assignment, you will implement a Student Record sorter program. Your program must include
the following elements:
1. A class called "Student" to hold the student records.
2. Different comparators to compare the different attributes of Student.
Student records will be specified in a file, one record per line. Each line of the file will have four
comma-separted fields: firstName, lastName, year, and gpa, in that order. An example file is shown
below:
Benjamin, Franklin, 4, 4.0
Alexander, Hamilton, 3, 3.5
Calvin, Collidge, 3, 3.5
Margaret, Thatcher, 2, 3.0
Richard, Nixon, 2, 2.1
The program must take two command line arguments:
1. the name of a file containing student records.
2. the name of a field (attribute) of the records on which to sort.
When run, your program must first print out your name and the program name, followed by a
formatted, sorted list of the records in the file, printed to the console. The fields of the formatted output
must be aligned, with a header printed above them, and 3 spaces between columns. The first and last
name should be left aligned, and the year and GPA should be right aligned.
If there are not enough command line arguments, or if the specified file cannot be found, the program
should print out a usage statement and exit.
As an example of program usage, if the user chooses to sort the example file above by last name, the
program output should look like the following:
java SortStudents students.txt lastName
Student sorting program by <your name>
First Last Year GPA
Calvin Collidge 3 3.5
Benjamin Franklin 4 4.0
Alexander Hamilton 3 3.5
Richard Nixon 2 2.1
Margaret Thatcher 2 3.0
Directions
1. Implement the SortStudents program as described above.
2. (Required) As part of your program, create a class called Student, and four comparators for the
four sorting methods. (Part of your grade will be based on doing this.)
3. Make sure to put the proper block comment at the top of your main file, including your name
and section number, etc.
4. Make sure that your program follows the class coding standards for indentation, variable
naming, etc. Part of your grade will be based on this.
5. Make sure your program prints out your name when it first starts. Also, make sure that the
program does not crash when less than two command line arguments are given.
6. Make sure the output of your program is properly aligned and that you print a header for your
output.
Explanation / Answer
package test; class Student { int id; String name; int age; String department; String course; String grade; double gpa; Student(int id, String name, int age, String department, String course, String grade, double gpa) { this.id = id; this.name = name; this.age = age; this.department = department; this.course = course; this.grade = grade; this.gpa = gpa; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } public String getDepartment() { return department; } public String getCourse() { return course; } public String getGrade() { return grade; } public double getGpa() { return gpa; } public String toString() { return getId() + " " + getName() + " " + getAge() + " " + getDepartment() + " " + getCourse() + " " + getGpa(); } } package test; import java.util.*; public class CalculateGPA { ArrayList<Student> list = new ArrayList(); public static void main(String[] args) { new CalculateGPA(); } public CalculateGPA() { Scanner input = new Scanner(System.in); double gpa = 0; for (int i = 0; i < 5; i++) { System.out.print("Enter id: "); int id = input.nextInt(); System.out.print("Enter Name: "); String name = input.next(); System.out.print("Enter Age: "); int age = input.nextInt(); System.out.print("Enter Department: "); String department = input.next(); System.out.print("Enter course: "); String course = input.next(); System.out.print("Enter grade: "); String grade = input.next(); if (grade.equals("A") || grade.equals("a")) { gpa = 4; } else if (grade.equals("B") || grade.equals("b")) { gpa = 3; } else if (grade.equals("C") || grade.equals("c")) { gpa = 2; } else if (grade.equals("D") || grade.equals("d")) { gpa = 1; } else if (grade.equals("F") || grade.equals("f")) { gpa = 0; } list.add(new Student(id, name, age, department, course, grade, gpa)); } for (Student s : list) { System.out.println(s.toString()); } Student s = searchStudent(1); System.out.println(s.toString()); } public Student searchStudent(int id) { for (Student student : list) { if (student.getId() == id) { return student; } } return null; // not found } }