JAVA Create a simple sequential-access file-processing program. This program should open the file provided in Blackboard. Read and display the grade information for each student. The program should also display the class average.
Enter the name of the file containing the student records: students.txt
Student ID First Name Last Name Grade
1 Suzy Green 88.60
2 Jessica Purple 98.30
3 Paul Orange 75.90
Class average is: 87.60
-----------------------------------------------------------------------------------------------------------
// Coding Exercise 2: ReadTextFileTest.java// This program test class Read Text File.public class Read Text File Test{ public static void main( String args[] ) { Read Text File application = new Read Text File(); application. Open File(); application .read Records(); application. Close File(); } // end main} // end class Read Text File Test
-------------------------------------------------------------------------------------------
// Coding Exercise 2: ReadTextFile.java// This program reads a text file and displays each record and // displays the class average.import java .io .File;import java .io .File Not Found Exception;import java .lang. Illegal State Exception;import java .until .No Such Element Exception;import java. Until public class Read Text File{ private Scanner input;
// enable user to open file public void open File() { try { Scanner temp Scanner = new Scanner( System. in ); System. out. print ln( "Enter the name of the file containing the student records:" ); String file Name = temp Scanner .next Line(); input = new Scanner( new File( file Name ) ); } // end try catch ( File Not Found Exception file Not Found Exception ) { System. err. print ln( "Error opening file." ); System. exit( 1 ); } // end catch } // end method open File // read record from file public void read Records() { // object to be written to screen Student record = new Student();
double total = 0; // stores total of all grades int grade Counter = 0; // counts grades input System .out. print f( "\n%-12s%-12s%-12s%10s\n", "Student ID", "First Name", "Last Name", "Grade" ); try // read records from file using Scanner object { while ( input .has Next() ) { Record .set Student ID( input. Next Int() ); // read account number Record .set First Name( input .next() ); // read first name Record .set Last Name( input .next() ); // read last name Record .set Grade( input .next Double() ); // read balance // display record contents System. Out .print f( "%-12d%-12s%-12s%10.2f\n",
record. Get Student ID(), record .get First Name(), record .get Last Name(), record. Get Grade() );
++grade Counter; total += record. Get Grade(); } // end while System. out. print f( "\n Class average is: %.2f\n", ( total / grade Counter ) ); } // end try catch ( No Such Element Exception element Exception ) { System. err. Print ln( "File improperly formed." ); input. close(); System .exit( 1 ); } // end catch catch ( Illegal State Exception state Exception ) { System err. Print ln( "Error reading from file." ); System .exit( 1 ); } // end catch } // end method read Records // close file and terminate application public void close File() { if ( input != null ) input .close(); // close file } // end method close File} // end class Read Text File
-------------------------------------------------------------------------------------------------------
// Coding Exercise 1: Student.java// A class that represents one record of student information. public class Student{ private int student ID; private String first Name; private String last Name; private double grade; // no-argument constructor calls other constructor with default values public Student() { this( 0, "", "", 0.0 ); // call four-argument constructor } // end no-argument Student constructor // initialize a record public Student( int id, String first, String last, double grade ) { Set Student ID( id ); set First Name( first ); set Last Name( last ); set Grade( grade ); } // end four-argument Student constructor // set student ID number public void set Student ID( int id ) { Student ID = id; } // end method set Student ID // get student ID number public int get Student ID() { return student ID; } // end method get Student ID // set first name public void set First Name( String first ) { First Name = first; } // end method set First Name // get first name public String get First Name() { return first Name; } // end method get First Name // set last name public void set Last Name( String last ) { Last Name = last; } // end method set Last Name // get last name public String get Last Name() { return last Name; } // end method get Last Name // set grade public void set Grade( double grade Value ) { grade = grade Value; } // end method set Grade // get grade public double get Grade() { return grade; } // end method get Grade} // end class Student
-----------------------------------------------------------------------------------------------------------
1 Suzy Green 88.602 Jessica Purple 98.303 Paul Orange 75.90
How do I fix it to get that output?