In: Computer Science
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects. Each line from the file to read will contain two strings for first and last name, and three floats for three test grades. After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average are listed on top.
The Student class should have instance data for first and last name, and a float array to hold three test grades. Create getters and setters for first and last name. Create one getter and one setter for inserting tests into the array. Don’t forget to verify that the quiz grades are within a range that makes sense before you insert them into the array. Also, verify that you do not go outside the index range of the array when inserting quizzes. Create a method to calculate and return test average. Also, override the toString method and the equals method to something that make sense. Also, implement the Comparable interface on the Student class so that when you compare Student objects by comparing the student's test average. If the average is the same when comparing, then compare the last names. If the last names are the same, then compare the first names.
Here is the file that the program should read:
Text File:
Frank Hanford 78.5 65.9 98.2 John Blake 93.1 85.9 89.1 Alex Sanders 87.1 56.9 67.2 Jane Hope 72.1 85.1 77.9 Donald Davidson 85.1 72.1 77.9 Alan Davidson 77.9 72.1 85.1 Perkins Jenkins 87.5 75.9 90.1 Barbara Thompson 90.1 89.9 99.7 Michael Jones 78.1 69.9 81.2
Given below is the code for the question. Please do rate the
answer if it helped. Thank you.
If you are using eclipse as your IDE, make sure you don't put the
input file inside the src folder. Just put it directly under the
project folder, otherwise you will get file not found error.
Student.java
-----
public class Student implements Comparable<Student>{
private String fname;
private String lname;
private float[] grades;
public Student() {
fname = "";
lname = "";
grades = new float[3];
}
public Student(String fname, String lname) {
this.fname = fname;
this.lname = lname;
grades = new float[3];
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public void setGrade(int index, float g) {
if(index >= 0 && index
< 3 && g >= 0 && g <= 100)
grades[index] =
g;
}
public float getGrade(int index) {
if(index >= 0 && index
< 3)
return
grades[index];
else
return 0;
}
public float calculate() {
float avg = (grades[0] + grades[1]
+ grades[2])/3;
return avg;
}
public boolean equals(Student s) {
if(fname.equalsIgnoreCase(s.fname)
&& lname.equalsIgnoreCase(s.lname)) {
for(int i = 0; i
< grades.length; i++) {
if(grades[i] != s.grades[i])
return false;
}
return
true;
}
return false;
}
@Override
public int compareTo(Student o) {
float avg1 = calculate();
float avg2 = o.calculate();
if(avg1 > avg2)
return 1;
else if(avg1 < avg2)
return -1;
else {
if(lname.equalsIgnoreCase(o.lname))
return fname.compareToIgnoreCase(o.fname);
else
return lname.compareToIgnoreCase(o.lname);
}
}
public String toString() {
return String.format("Name: %s %s,
Grades: %.2f %.2f %.2f, Average: %.2f", fname, lname, grades[0],
grades[1], grades[2], calculate());
}
}
SortStudents.java
-----------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class SortStudents {
public static void main(String[] args) {
String filename;
Scanner in = new
Scanner(System.in);
System.out.print("Enter input
filename: ");
filename = in.next();
ArrayList<Student> s =
readFile(filename);
System.out.println("Before
sorting");
printStudents(s);
sortStudents(s);
System.out.println("After sorting
on average");
printStudents(s);
}
private static ArrayList<Student>
readFile(String filename){
ArrayList<Student> students =
new ArrayList<Student>();
try {
Scanner infile =
new Scanner(new File(filename));
while(infile.hasNext()) {
Student s = new Student();
s.setFname(infile.next());
s.setLname(infile.next());
for(int i = 0; i < 3; i++)
s.setGrade(i,
infile.nextFloat());
students.add(s);
}
infile.close();
} catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
System.exit(1);
}
return students;
}
private static void
sortStudents(ArrayList<Student> s){
int maxIdx;
for(int i = 0; i < s.size();
i++) {
maxIdx =
i;
for(int j = i+1;
j < s.size(); j++) {
if(s.get(j).compareTo(s.get(maxIdx)) > 0)
{
maxIdx = j;
}
}
Student temp =
s.get(i);
s.set(i,
s.get(maxIdx));
s.set(maxIdx,
temp);
}
}
private static void
printStudents(ArrayList<Student> students) {
for(Student s: students)
System.out.println(s);
System.out.println("\n");
}
}
output
----
Enter input filename: grades.txt
Before sorting
Name: Frank Hanford, Grades: 78.50 65.90 98.20, Average:
80.87
Name: John Blake, Grades: 93.10 85.90 89.10, Average: 89.37
Name: Alex Sanders, Grades: 87.10 56.90 67.20, Average: 70.40
Name: Jane Hope, Grades: 72.10 85.10 77.90, Average: 78.37
Name: Donald Davidson, Grades: 85.10 72.10 77.90, Average:
78.37
Name: Alan Davidson, Grades: 77.90 72.10 85.10, Average:
78.37
Name: Perkins Jenkins, Grades: 87.50 75.90 90.10, Average:
84.50
Name: Barbara Thompson, Grades: 90.10 89.90 99.70, Average:
93.23
Name: Michael Jones, Grades: 78.10 69.90 81.20, Average: 76.40
After sorting on average
Name: Barbara Thompson, Grades: 90.10 89.90 99.70, Average:
93.23
Name: John Blake, Grades: 93.10 85.90 89.10, Average: 89.37
Name: Perkins Jenkins, Grades: 87.50 75.90 90.10, Average:
84.50
Name: Frank Hanford, Grades: 78.50 65.90 98.20, Average:
80.87
Name: Jane Hope, Grades: 72.10 85.10 77.90, Average: 78.37
Name: Donald Davidson, Grades: 85.10 72.10 77.90, Average:
78.37
Name: Alan Davidson, Grades: 77.90 72.10 85.10, Average:
78.37
Name: Michael Jones, Grades: 78.10 69.90 81.20, Average:
76.40
Name: Alex Sanders, Grades: 87.10 56.90 67.20, Average: 70.40