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

Design a Java application that reads a class roster in a format consisting of student names and 4 scores, given by integer numbers. The program must sort the roster either with respect to the last names 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 in 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 variable totalScore. Use Java API class ArrayList to store the student records and handle the roster. Ask the user for the desired way of sorting the ArrayList in the form \"n\" (sort for names) and \"s\" (sort for scores)|

Solution

package com.amdocs.project;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Roaster implements Comparator<Record>{
   public static ArrayList<Record> al;
  
   public int compare(Record one,Record two){
       if (one.getTotalScores() >two.getTotalScores())
           return -1;
       else if (one.getTotalScores()==two.getTotalScores())
           return 0;
       else
           return 0;
              
   }
  
   public static void main(String[] args) throws Exception {
       // TODO Auto-generated method stub
      
      
       //FileReader r=new FileReader(Syst);
      
       File myfile=new File(\"roster.txt\");
       FileReader fr=new FileReader(myfile);
       BufferedReader br=new BufferedReader(fr);
       String line=null;
       while((line=br.readLine())!=\"end\"){
           line=line.replaceAll(\"\\\\s+\",\" \");
           System.out.println(line);
           String b[]=line.split(\" \");
           System.out.println(b[0]);
           System.out.println(b[1]);
           String firstName=b[0];
           String lastName=b[1];
           int[] scores=new int[4];
           scores[0]=Integer.parseInt(b[2]);
           scores[1]=Integer.parseInt(b[3]);
           scores[2]=Integer.parseInt(b[4]);
           scores[3]=Integer.parseInt(b[5]);
           System.out.println(scores[0]);
           Record re=new Record(lastName,firstName, scores);
           al.add(re);
       }
       Scanner sc=new Scanner(System.in);
       String a=sc.nextLine();
       if (a==\"n\")
       {
           Collections.sort(al);
       }
       else if(a==\"s\"){
           Collections.sort(al,new Roaster());
       }
  
          
       }
}

package com.amdocs.project;

public class Record implements Comparable<Record>{
public String lastName;
public String firstName;
public int[] scores;
public int totalScores;

public Record(String lastName,String firstName, int[] scores ){
   this.lastName=lastName;
   this.firstName=firstName;
   this.scores=scores;
   }


public String getLastName() {
   return lastName;
}


public void setLastName(String lastName) {
   this.lastName = lastName;
}


public int getTotalScores() {
   return totalScores;
}


public void setTotalScores(int totalScores) {
   this.totalScores = totalScores;
}


public void grade(){
   int sum=0;
   for (int i:scores){
       sum+=i;
   }
   totalScores=sum/4;
}
public int compareTo(Record r){
   return lastName.compareTo(r.getLastName());
}
}

 Design a Java application that reads a class roster in a format consisting of student names and 4 scores, given by integer numbers. The program must sort the r
 Design a Java application that reads a class roster in a format consisting of student names and 4 scores, given by integer numbers. The program must sort the r
 Design a Java application that reads a class roster in a format consisting of student names and 4 scores, given by integer numbers. The program must sort the r

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site