Question

Write a program in Java according to the following specifications: The program reads a text file...

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 a regular array (not ArrayList) of objects of class Student to store the student records. Define a class Student that represents a student record (student's first name, last name and grade) and implements the Comparable interface. Use the bubble sort algorithm for sorting objects implementing the Comparable interface. Hint: Define a static instance variable in class Student to specify the type of sorting and use it in the compareTo method to determine what to compare - first names, last names, or grades.

ExtraCredit (maximum 2 points): Implement "add" and "delete" commands for adding and deleting students given their names and grades.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// students.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

___________________

// Student.java

public class Student implements Comparable<Student> {
   private String firstname;
   private String lastname;
   private int grade;
public static String command;
   public Student(String firstname, String lastname, int grade) {
       this.firstname = firstname;
       this.lastname = lastname;
       this.grade = grade;
   }

   public String getFirstname() {
       return firstname;
   }

   public void setFirstname(String firstname) {
       this.firstname = firstname;
   }

   public String getLastname() {
       return lastname;
   }

   public void setLastname(String lastname) {
       this.lastname = lastname;
   }

   public int getGrade() {
       return grade;
   }

   public void setGrade(int grade) {
       this.grade = grade;
   }

   @Override
   public String toString() {
       return firstname + " " + lastname + " " + grade;
   }

   public static void setCommand(String command) {
       Student.command = command;
   }

   public int compareTo(Student other) {
       String temp;
       int val=0;
       if(command.equalsIgnoreCase("sortlast"))
       {
           return this.getLastname().compareTo(other.getLastname());
       }
       else if(command.equalsIgnoreCase("sortfirst"))
       {
           return this.getFirstname().compareTo(other.getFirstname());          
       }
       else if(command.equalsIgnoreCase("grade"))
       {
           int ograde = other.getGrade();

           if (getGrade() > ograde)
           val = 1;
           else if (getGrade() < ograde)
           val = -1;
           else if (getGrade() == ograde)
           val = 0;
           return val;
       }
       return val;
   }

}
_______________________

// ReadStudentsData.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class ReadStudentsData {

   public static void main(String[] args) {
       String command,fname,lname,str;
       int grade,cnt=0;
       Student sarr[]=new Student[100];
try {
       Scanner sc=new Scanner(new File("students.txt"));
       while(sc.hasNext())
       {
           fname=sc.next();
           lname=sc.next();
           grade=sc.nextInt();

           Student s=new Student(fname, lname, grade);
           sarr[cnt]=s;
           cnt++;
       }
       sc.close();
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       sc = new Scanner(System.in);

       while(true)
       {
           System.out.print("\nEnter command :");
           command=sc.next();
           if(command.equalsIgnoreCase("print"))
           {
               display(sarr,cnt);
           }
           else if(command.equalsIgnoreCase("sortlast"))
           {
               Student.setCommand(command);
       //performing sorting operation
           bubbleSort(sarr,cnt);

               display(sarr,cnt);
           }
           else if(command.equalsIgnoreCase("sortfirst"))
           {
               Student.setCommand(command);
               //performing sorting operation
           bubbleSort(sarr,cnt);
               display(sarr,cnt);
           }
           else if(command.equalsIgnoreCase("grade"))
           {
               Student.setCommand(command);
               //performing sorting operation
           bubbleSort(sarr,cnt);
               display(sarr,cnt);
           }
           else if(command.equals("end"))
           {
               break;
           }
       }
      
       sc.close();
      
   } catch (FileNotFoundException e) {
   System.out.println("** File Not Found **");
   }

   }

   private static void display(Student[] sarr, int cnt) {
       for(int i=0;i<cnt;i++)
       {
           System.out.println(sarr[i]);
       }
      
   }

private static void bubbleSort(Comparable[] list,int size) {
  
for(int i=0;i<size-1;i++)
{
if(list[i].compareTo(list[i + 1]) > 0)
{
swap(list,i,i+1);
}
}
if(size-1>1)
{
bubbleSort(list, size-1);
}
  
}
private static void swap(Comparable[] list, int i, int j) {
Comparable tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
/** Sort an array of comparable objects using the Bubble sort algorithm */
public static void sort(Comparable[] list) {
Comparable currentMin;
int currentMinIndex;
// select subarray (the range of the array) where we bubble up the
// greatest in the subarray
for (int i = 0; i < list.length - 1; i++) {
// bubble up the greast in the subarray (in the range of the array)
for (int j = 0; j < list.length - i - 1; j++) {
if (list[j].compareTo(list[j + 1]) > 0) {
Comparable tmp = list[j];
list[j] = list[j + 1];
list[j + 1] = tmp;
}
}
}
}


}
___________________________

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 :sortfirst
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 :sortlast
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 :grade
Mario Guzman 60
Laura Getz 72
Sarah Barnes 75
Frank Phelps 75
Mark Riley 80
Marsha Grant 85
John Smith 90
Larry Smith 95

Enter command :end

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Write a program in Java according to the following specifications: The program reads a text file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • Write a Java program that does the following: 1. Creates a string array called firstNames that...

    Write a Java program that does the following: 1. Creates a string array called firstNames that holds these values "John", "Frank", "Nick", "Amanda", "Brittany", "Amy", "Deborah", and "Stirling". 2. Creates another string array called lastNames that holds these values "Thompson", "Smith", "Jones", "Keribarian", "Lu", "Banafsheh", "Spielberg", "Jordan", "Castle" and "Simpson". 3. Now add a method that creates an array list called randomNames which picks a random first name from the array in step 1 and a random last name in...

  • This code should be written in php. Write a program that allows the user to input...

    This code should be written in php. Write a program that allows the user to input a student's last name, along with that student's grades in English, History, Math, Science, and Geography. When the user submits this information, store it in an associative array, like this: Smith=61 67 75 80 72 where the key is the student's last name, and the grades are combined into a single space-delimited string. Return the user back to the data entry screen, to allow...

  • C++ program that reads students' names followed by their test scores (5 scores) from the given...

    C++ program that reads students' names followed by their test scores (5 scores) from the given input file, students.txt. The program should output to a file, output.txt, each student's first name, last name, followed by the test scores and the relevant grade. All data should be separated by a single space. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, an array of testScores of type int...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • Write a C++ program to store and update students' academic records in a binary search tree....

    Write a C++ program to store and update students' academic records in a binary search tree. Each record (node) in the binary search tree should contain the following information fields:               1) Student name - the key field (string);               2) Credits attempted (integer);               3) Credits earned (integer);               4) Grade point average - GPA (real).                             All student information is to be read from a text file. Each record in the file represents the grade information on...

  • Write a program, which reads a list of student records from a file in batch mode....

    Write a program, which reads a list of student records from a file in batch mode. Each student record comprises a roll number and student name, and the student records are separated by commas. An example data in the file is given below. · SP18-BCS-050 (Ali Hussan Butt), SP19-BCS-154 (Huma Khalid), FA19-BSE-111 (Muhammad Asim Ali), SP20-BSE-090 (Muhammad Wajid), SP17-BCS-014 (Adil Jameel) The program should store students roll numbers and names separately in 2 parallel arrays named names and rolls, i.e....

  • In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, t...

    In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, then present a menu to the user, and at the end print a final report shown below. You may(should) use the structures you developed for the previous assignment to make it easier to complete this assignment, but it is not required. Required Menu Operations are: Read Students’ data from a file to update the list (refer to sample...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT