Design a Java application that reads a Class roster in a for
Solution
Roster.java
import java.io.FileReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Roster {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(
new FileReader(\"data.txt\"));
System.out.println(\"Student Scores Application.\ \");
int nStud = 100;
Student[] studArr = new Student[nStud];
Scanner lnScan = null;
int ctr = 0;
while (scan.hasNext()) {
lnScan = new Scanner(scan.nextLine());
String lName = lnScan.next();
String fName = lnScan.next();
int examScr = lnScan.nextInt();
System.out.println(\"Student \" + ctr + \" \" + fName + \" \"
+ lName + \" \" + +examScr);
studArr[ctr++] = new Student(lName, fName, examScr);
lnScan.close();
}
for (int j = 0; j < ctr; j++) {
System.out.println(studArr[j]);
}
Arrays.sort(studArr, 0, ctr, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getFirstName().compareTo(s2.getFirstName());
}
});
System.out.println(\"Students sorted in ascending order by first name\");
for (int j = 0; j < ctr; j++) {
System.out.println(studArr[j]);
}
Arrays.sort(studArr, 0, ctr, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return Integer.valueOf(s1.getScore()).
compareTo(Integer.valueOf(s2.getScore()));
}
});
System.out.println(\"Students sorted in ascending order by score\");
for (int j = 0; j < ctr; j++) {
System.out.println(studArr[j]);
}
double sum = 0.0;
for (int j = 0; j < ctr; j++) {
sum += studArr[j].getScore();
}
double average = (sum * 1.0) / ctr;
System.out.println(\"Average_Score = \" + average);
scan.close();
}
}
Student.java
class Student implements Comparable<Student> {
private String fName;
private String lName;
private int score;
public Student(String fName, String lName, int score) {
this.fName = fName;
this.score = score;
this.lName = lName;
}
public int getScore() {
return score;
}
public String getFirstName() {
return fName;
}
public String getLastName() {
return lName;
}
@Override
public int compareTo(Student s) {
if (!fName.equalsIgnoreCase(s.fName)) {
return fName.compareToIgnoreCase(s.fName);
}
if (!lName.equalsIgnoreCase(s.lName)) {
return lName.compareToIgnoreCase(s.lName);
}
return (new Integer(score)).compareTo((new Integer(s.score)));
}
@Override
public String toString() {
return lName + \", \" + fName + \" : \" + score;
}
}



