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

Write a program in Java according to the following specifications: The program r

ID: 671454 • Letter: W

Question

Write a program in Java according to the following specifications:

The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following:

"print" - prints the student records (first name, last name, grade).

"sortfirst" - sorts the student records by first name.

"sortlast" - sorts the student records by last name.

"sortgrade" - sorts the student records by grade.

"end" - exits the loop the terminates the program.

For example, if the input file includes the following data:

John Smith   90
Sarah Barnes 75
Mark Riley   80
Laura Getz   72
Larry Smith 95
Frank Phelps 75
Mario Guzman 60
Marsha Grant 85

the program may proceeds as follows (user input is shown in bold face):

Enter command: print

John Smith   90
Sarah Barnes 75
Mark Riley   80
Laura Getz   72
Larry Smith 95
Frank Phelps 75
Mario Guzman 60
Marsha Grant 85

Enter command: sortlast

Students sorted by last name

Enter command: print

Sarah Barnes 75
Laura Getz   72
Marsha Grant 85
Mario Guzman 60
Frank Phelps 75
Mark Riley   80
John Smith   90
Larry Smith 95

Restrictions and implementation: Use an array of objects to store the student records. Define a class Student that represents a student record (student's first name, last name and grade) and implements theComparable interface. Use the bubble sort algorithm for sorting Student objects. Compile and run the program using BlueJ. Hint: Define a static instance variable in class Student to specify the type of sorting and use it in the compareTo method to compare first names, last names, or grades respectively.

Explanation / Answer

/**
* The Student class that implements the Comparable
* interface that overrides the methods compareTo
* method that returns the integer value
* */
//Student.java
public class Student implements Comparable<Student>
{
   //members of class
   private String fName;
   private String lName;
   private int grade;

  
   //parameterized consructor of class
   public Student(String fName, String lName, int grade)
   {
       this.fName=fName;
       this.lName=lName;
       this.grade=grade;
   }
  
   //Returns the first name
   public String getFName()
   {
       return fName;
   }
  
   //Returns the last name
   public String getLName()
   {
       return lName;
   }
  
   //Returns the grade
   public int getGrade()
   {
       return grade;
   }
  

  
   /**Override the compareTo method that compares
   * the three members of the class */
   @Override
   public int compareTo( Student s )
   {
       if( !lName.equals( s.lName ) )
       {
           return lName.compareTo(lName);
       }
       else if( !fName.equals( s.fName ) )
       {
           return fName.compareTo( s.fName );
       }
       else
           return grade - s.grade;
   }
   /*Returns the string representation of class object*/
   @Override
   public String toString()
   {      
       return fName+" "+lName+" "+grade;
   }
}


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


//StudentTest .java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentTest
{
   public static void main(String[] args)
   {
      
       //set size
       int SIZE=8;
       int index=0;
       //Create an array of type Student
       Student[] students=new Student[SIZE];      
       Scanner scanner=null;
      
      
       try
       {
           //Read a text file "scores.txt"
           scanner=new Scanner(new File("scores.txt"));
          
           //read scores of the student from file
           while(scanner.hasNextLine())
           {
               String firstName=scanner.next();
               String lastName=scanner.next();
               int grade=scanner.nextInt();
              
               students[index]=new Student(firstName, lastName, grade);
               index++;              
           }
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e);
       }
      
       scanner.close();
      
      
       Scanner keyboard=new Scanner(System.in);
       String command="";
      
       while(true)
       {
           System.out.println("Enter command:");
           //read command from keyboard
           command=keyboard.nextLine();
           switch(command)
           {
           case "print":
               //call print method
               print(students);
               break;
           case "sortfirst":
               //call sortByFname
               sortByFname(students);
               System.out.println("Students sorted by first name");
               break;
           case "sortlast":
               //call sortByLname
               sortByLname(students);
               System.out.println("Students sorted by last name");
               break;
           case "sortgrade":
               //call sortByGrade
               sortByGrade(students);
               System.out.println("Students sorted by grade");
               break;
           case "end":
               System.exit(0);
           }
          
       }
      
   }
  
   //prints the student object
   public static void print(Student students[])
   {
       for (Student student : students)
       {
           System.out.println(student);
       }
   }
  
   /**Bubble sort method that sorts the students by first name*/
   public static void sortByFname(Student students[])
   {
       for (int i = 0; i < students.length; i++)
       {
           for (int j = 0; j < students.length-i-1; j++)
           {
               //compares the first name of the students
               if(students[j].getFName()
                       .compareTo(students[j+1].getFName())>0)
               {
                   Student student=students[j];
                   students[j]=students[j+1];
                   students[j+1]=student;
                  
               }
           }
       }
   }
   /**Bubble sort method that sorts the students by last name*/
   public static void sortByLname(Student students[])
   {
       for (int i = 0; i < students.length; i++)
       {
           for (int j = 0; j < students.length-i-1; j++)
           {
               //compares the last name of the students
               if(students[j].getLName()
                       .compareTo(students[j+1].getLName())>0)
               {
                   Student student=students[j];
                   students[j]=students[j+1];
                   students[j+1]=student;
                  
               }
           }
       }
   }

   /**Bubble sort method that sorts the students by grade */
   public static void sortByGrade(Student students[])
   {
       for (int i = 0; i < students.length; i++)
       {
           for (int j = 0; j < students.length-i-1; j++)
           {
               //compares grades
               if(students[j].getGrade()
                       >students[j+1].getGrade())
               {
                   Student student=students[j];
                   students[j]=students[j+1];
                   students[j+1]=student;
                  
               }
           }
       }
   }
}


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

Input file name

scores.txt

John Smith   90
Sarah Barnes 75
Mark Riley   80
Laura Getz   72
Larry Smith 95
Frank Phelps 75
Mario Guzman 60
Marsha Grant 85

Sample Output:

Enter command:
print
John Smith 90
Sarah Barnes 75
Mark Riley 80
Laura Getz 72
Larry Smith 95
Frank Phelps 75
Mario Guzman 60
Marsha Grant 85
Enter command:
sortlast
Students sorted by last name
Enter command:
print
Sarah Barnes 75
Laura Getz 72
Marsha Grant 85
Mario Guzman 60
Frank Phelps 75
Mark Riley 80
John Smith 90
Larry Smith 95
Enter command:
sortfirst
Students sorted by first name
Enter command:
print
Frank Phelps 75
John Smith 90
Larry Smith 95
Laura Getz 72
Mario Guzman 60
Mark Riley 80
Marsha Grant 85
Sarah Barnes 75
Enter command:
sortgrade
Students sorted by grade
Enter command:
print
Mario Guzman 60
Laura Getz 72
Frank Phelps 75
Sarah Barnes 75
Mark Riley 80
Marsha Grant 85
John Smith 90
Larry Smith 95
Enter command:

exit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote