Design a Java application that reads a Class roster in a for

Design a Java application that reads a Class roster in a format Doe Joe 8 91 89 90 Brown Jane 89 91 79 95 Hopkins Michael 56 78 94 76 Smith Erin 95 85 84 79 Brown Jerry 91 87 79 100 Green Erin 88 92 67 89 Griggs Mary 78 0 97 82 Hopkins John 98 45 89 99 Peters Brian 64 90 100 85 Smith Molly 99 100 100 92 consisting of student names and 4 scores, given by integer numbers. The program must sort the roster either with respect to the last name in the alphabetic order, or according to the total scores in decreasing order and print the sorted roster. Students with the same last name or the same total score can be printed In the sorted list in arbitrary order. The total scores are not present in the roster and should be computed by averaging the four scores in every line of the roster. The averages must be rounded off to the nearest integer. Your program should work for any roster and should read it from file roster.txt For every student m the roster your program must instantiate custom class Record with the following class members, corresponding to the last and first student name, his/her scores (array of length 4), and the total score. The class must include method grade() for computing the total score and saving it in class variabie totalScore. Use Java API class ArrayListto store the student records and handle the roster. public class Record {public String lastName; public String firstName; public int[] scores; public int totalScore;} Ask the user for the desired way of sorting the ArravList in the form \"n\" (sort for names) and \"s\" (sort for scores).|

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;

    }

}

 Design a Java application that reads a Class roster in a format Doe Joe 8 91 89 90 Brown Jane 89 91 79 95 Hopkins Michael 56 78 94 76 Smith Erin 95 85 84 79 Br
 Design a Java application that reads a Class roster in a format Doe Joe 8 91 89 90 Brown Jane 89 91 79 95 Hopkins Michael 56 78 94 76 Smith Erin 95 85 84 79 Br
 Design a Java application that reads a Class roster in a format Doe Joe 8 91 89 90 Brown Jane 89 91 79 95 Hopkins Michael 56 78 94 76 Smith Erin 95 85 84 79 Br
 Design a Java application that reads a Class roster in a format Doe Joe 8 91 89 90 Brown Jane 89 91 79 95 Hopkins Michael 56 78 94 76 Smith Erin 95 85 84 79 Br

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site