Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Class ClassRoll: This class maintains student information (arbitrary number of s

ID: 2300323 • Letter: C

Question

Class ClassRoll: This class maintains student information (arbitrary number of students) for one course:

Private Static Variables

ArrayList of students

Students in the class

String title

The course title

String filename

The name of the input/output file

Constructor

ClassRoll(String f)

Read the class roll data from the input file f, creates Student objects for each of the students and adds them to the Array of students.

The input file contains the course title on the first line. The data for each student appears on a separate line consisting of last name, first name, score1, score 2, and score3 separated by at least one space.

Public Methods:

void remove()

Prompts the user for the first name and last name of a student and removes the student from the list of students. In case the student is not found in the list of students, an appropriate message is displayed.

void display()

Displays the class roll. The course title on the first line and each student record (first name, last name, score1, score 2, score3 and average) on a separate line. The class average must also be displayed. The class roll must be properly formatted

void add()

Prompts the user for the first name, last name and three scores of a student, creates a student object with the input data and adds the student to the end of the list of students. In case the student is already in the list of students, an appropriate message is displayed.

void changeScore1()

Prompts the user for user for the first name, last name and score 1 of a student and change the first score of the student. In case the student is not found in the list of students, an appropriate message is displayed.

void changeScore2()

Prompts the user for user for the first name, last name and score 2 of a student and change the first score of the student. In case the student is not found in the list of students, an appropriate message is displayed.

void changeScore3()

Prompts the user for user for the first name, last name and score 3 of a student and change the first score of the student. In case the student is not found in the list of students, an appropriate message is displayed.

void find()

Prompts the user for the first name and last name of a student and displays the student

Private Static Variables

ArrayList of students

Students in the class

String title

The course title

String filename

The name of the input/output file

Constructor

ClassRoll(String f)

Read the class roll data from the input file f, creates Student objects for each of the students and adds them to the Array of students.

The input file contains the course title on the first line. The data for each student appears on a separate line consisting of last name, first name, score1, score 2, and score3 separated by at least one space.

Public Methods:

void remove()

Prompts the user for the first name and last name of a student and removes the student from the list of students. In case the student is not found in the list of students, an appropriate message is displayed.

void display()

Displays the class roll. The course title on the first line and each student record (first name, last name, score1, score 2, score3 and average) on a separate line. The class average must also be displayed. The class roll must be properly formatted

void add()

Prompts the user for the first name, last name and three scores of a student, creates a student object with the input data and adds the student to the end of the list of students. In case the student is already in the list of students, an appropriate message is displayed.

void changeScore1()

Prompts the user for user for the first name, last name and score 1 of a student and change the first score of the student. In case the student is not found in the list of students, an appropriate message is displayed.

void changeScore2()

Prompts the user for user for the first name, last name and score 2 of a student and change the first score of the student. In case the student is not found in the list of students, an appropriate message is displayed.

void changeScore3()

Prompts the user for user for the first name, last name and score 3 of a student and change the first score of the student. In case the student is not found in the list of students, an appropriate message is displayed.

void find()

Prompts the user for the first name and last name of a student and displays the student

Explanation / Answer

File: ClassRoll.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class ClassRoll
{
   private static ArrayList<Student> students;
   private static String title;
   private static String filename;
   private static Scanner input;
      
   public ClassRoll()
   {
       students = new ArrayList<Student>();
       title = "";
       filename = "";
       input = new Scanner(System.in);
   }
  
   public ClassRoll(String f)
   {
       students = new ArrayList<Student>();
       filename = f;
      
       try
       {
           Scanner infile = new Scanner(new File(filename));
          
           if(infile.hasNextLine())
               title = infile.next();
          
           while(infile.hasNext())
           {              
               String fn = infile.next();
               String ln = infile.next();
               int s1 = infile.nextInt();
               int s2 = infile.nextInt();
               int s3 = infile.nextInt();
              
               Student student = new Student(fn, ln);
               student.setScore1(s1);
               student.setScore2(s2);
               student.setScore3(s3);
              
               students.add(student);
           }
          
           infile.close();
       }
       catch(FileNotFoundException e)
       {
           System.out.println(e.getMessage() + " file is not found.");
           System.exit(1);
       }
   }
  
   public void remove()
   {
       System.out.print(" Enter the first name: ");
       String fn = input.next();
      
       System.out.print("Enter the last name: ");
       String ln = input.next();
      
       Student student = new Student(fn, ln);
      
       boolean isFound = false;
       int i = 0;
      
       while(!isFound && i < students.size())
       {
           if(students.get(i).compareTo(student) == 0)
           {
               isFound = true;
              
               students.remove(i);
           }
           else
               i++;
       }
      
       if(!isFound) // if not found
       {
           System.out.println(fn + " " + ln + " is not found in the class.");
       }
   }
  
   public void display()
   {
       System.out.println();
       System.out.println(title);
      
       for(int i = 0; i < students.size(); i++)
       {
           System.out.print(students.get(i).toString());
          
           double avg = students.get(i).getAverage();
          
           avg = Math.round(avg * 100.0) / 100.0;
          
           System.out.println(" " + avg);
       }
   }
  
   public void add()
   {
       System.out.print(" Enter the first name: ");
       String fn = input.next();
      
       System.out.print("Enter the last name: ");
       String ln = input.next();
      
       System.out.print("Enter the score1: ");
       int s1 = input.nextInt();
      
       System.out.print("Enter the score2: ");
       int s2 = input.nextInt();
      
       System.out.print("Enter the score3: ");
       int s3 = input.nextInt();
      
       Student student = new Student(fn, ln);
       student.setScore1(s1);
       student.setScore2(s2);
       student.setScore3(s3);
      
       boolean isFound = false;
       int i = 0;
      
       while(!isFound && i < students.size())
       {
           if(students.get(i).compareTo(student) == 0)
               isFound = true;
           else
               i++;
       }
      
       if(isFound) // if found
           System.out.println(fn + " " + ln + " is already in the class.");
       else
           students.add(student);
   }
  
   public void changeScore1()
   {
       System.out.print(" Enter the first name: ");
       String fn = input.next();
      
       System.out.print("Enter the last name: ");
       String ln = input.next();
      
       System.out.print("Enter the score1: ");
       int s1 = input.nextInt();
      
       Student student = new Student(fn, ln);
       boolean isFound = false;
       int i = 0;
      
       while(!isFound && i < students.size())
       {
           if(students.get(i).compareTo(student) == 0)
               isFound = true;
           else
               i++;
       }
      
       if(!isFound) // if not found
           System.out.println(fn + " " + ln + " is not found in the class.");
       else
       {
           students.get(i).setScore1(s1);
       }
   }  
  
   public void changeScore2()
   {
       System.out.print(" Enter the first name: ");
       String fn = input.next();
      
       System.out.print("Enter the last name: ");
       String ln = input.next();
      
       System.out.print("Enter the score2: ");
       int s2 = input.nextInt();
      
       Student student = new Student(fn, ln);
       boolean isFound = false;
       int i = 0;
      
       while(!isFound && i < students.size())
       {
           if(students.get(i).compareTo(student) == 0)
               isFound = true;
           else
               i++;
       }
      
       if(!isFound) // if not found
           System.out.println(fn + " " + ln + " is not found in the class.");
       else
       {
           students.get(i).setScore2(s2);
       }
   }
  
   public void changeScore3()
   {
       System.out.print(" Enter the first name: ");
       String fn = input.next();
      
       System.out.print("Enter the last name: ");
       String ln = input.next();
      
       System.out.print("Enter the score1: ");
       int s3 = input.nextInt();
      
       Student student = new Student(fn, ln);
       boolean isFound = false;
       int i = 0;
      
       while(!isFound && i < students.size())
       {
           if(students.get(i).compareTo(student) == 0)
               isFound = true;
           else
               i++;
       }
      
       if(!isFound) // if not found
           System.out.println(fn + " " + ln + " is not found in the class.");
       else
       {
           students.get(i).setScore3(s3);
       }
   }
  
   public void find()
   {
       System.out.print(" Enter the first name: ");
       String fn = input.next();
      
       System.out.print("Enter the last name: ");
       String ln = input.next();
      
       Student student = new Student(fn, ln);
       boolean isFound = false;
       int i = 0;
      
       while(!isFound && i < students.size())
       {
           if(students.get(i).compareTo(student) == 0)
               isFound = true;
           else
               i++;
       }
      
       if(!isFound) // if not found
           System.out.println(fn + " " + ln + " is not found in the class.");
       else
       {
           System.out.println("Scores and average of " + fn + " " + ln + ":");
           System.out.println(students.get(i));
       }
   }
  
   public void sortAverage()
   {      
       ArrayList<Student> temp = students;
      
       // sort the temp list
       for(int i = temp.size() - 1; i >= 0; i--)
       {
           for(int j = 0; j <= i - 1; j++)
           {
               // compare the elements
               if(temp.get(j).getAverage() < temp.get(j + 1).getAverage())
               {
                   // swap the elements
                   Student curr = temp.get(j);
                   temp.set(j, temp.get(j + 1));
                   temp.set(j + 1, curr);
               }
           }
       }
    
       // display the students after sorting
       System.out.println();
       for(int i = 0; i < temp.size(); i++)
       {
           System.out.print(temp.get(i).toString());
          
           double avg = students.get(i).getAverage();
          
           avg = Math.round(avg * 100.0) / 100.0;
          
           System.out.println(" " + avg);
       }
   }
  
   public void sortNames()
   {
       ArrayList<Student> temp = students;
      
       // sort the temp list
       for(int i = temp.size() - 1; i >= 0; i--)
       {
           for(int j = 0; j <= i - 1; j++)
           {
               // compare the elements
               if(temp.get(j).compareTo(temp.get(j + 1)) > 0)
               {
                   // swap the elements
                   Student curr = temp.get(j);
                   temp.set(j, temp.get(j + 1));
                   temp.set(j + 1, curr);
               }
           }
       }
    
       // display the students after sorting
       System.out.println();
       for(int i = 0; i < temp.size(); i++)
       {
           System.out.print(temp.get(i).toString());
          
           double avg = students.get(i).getAverage();
          
           avg = Math.round(avg * 100.0) / 100.0;
          
           System.out.println(" " + avg);
       }
   }
  
   public void save()
   {
       try
       {
           PrintWriter outfile = new PrintWriter(filename);
          
           outfile.println(title);
          
           for(int i = 0; i < students.size(); i++)
           {
               outfile.print(students.get(i).toString());
              
               double avg = students.get(i).getAverage();
              
               avg = Math.round(avg * 100.0) / 100.0;
              
               outfile.println(" " + avg);
           }
          
           outfile.close();
       }
       catch(FileNotFoundException e)
       {
           System.out.println(e.getMessage() + " file is not found.");
           System.exit(1);
       }
   }
}

-------------------------------------------------------------------------------------------

Supporting file: File: Student.java
public class Student
{
    private String fName;
    private String lName;
    private Exam scores;

    public Student(String fn, String ln)
    {
       fName = fn;
       lName = ln;
       scores = new Exam();
    }
  
    public void setScore1(int sc)
   {
       scores.setScore1(sc);
   }
  
   public void setScore2(int sc)
   {
       scores.setScore2(sc);
   }
  
   public void setScore3(int sc)
   {
       scores.setScore3(sc);
   }
  
   public String toString()
   {
       return fName + " " + lName + " " + scores.toString();
   }
  
   double getAverage()
   {
       return (scores.getScore1() + scores.getScore2() + scores.getScore3()) / 3.0;
   }
  
   int compareTo(Student s)
   {
       String name1 = fName + "," + lName;
       String name2 = s.fName + "," + s.lName;
      
       if(((Comparable)name1).compareTo(name2) < 0)
           return -1;
       else if(((Comparable)name1).compareTo(name2) > 0)
           return 1;
       else
           return 0;      
   }
}
----------------------------------------------------------------------

Supporting File: Exam.java
public class Exam
{
   private int score1;
   private int score2;
   private int score3;
  
   public Exam()
   {
       score1 = 0;
       score2 = 0;
       score3 = 0;
   }

   public void setScore1(int sc)
   {
       score1 = sc;
   }
  
   public void setScore2(int sc)
   {
       score2 = sc;
   }
  
   public void setScore3(int sc)
   {
       score3 = sc;
   }

   public int getScore1()
   {
       return score1;
   }
  
   public int getScore2()
   {
       return score2;
   }

   public int getScore3()
   {
       return score3;
   }  
  
   public String toString()
   {
       return score1 + " " + score2 + " " + score3;
   }
}