Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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 ReadTextFile. public class ReadTextFileTest { public static void main( String args[] ) { ReadTextFile application = new ReadTextFile(); application.openFile(); application.readRecords(); application.closeFile(); } // end main } // end class ReadTextFileTest 

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

// 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.FileNotFoundException; import java.lang.IllegalStateException; import java.util.NoSuchElementException; import java.util.Scanner; public class ReadTextFile { private Scanner input; // enable user to open file public void openFile() { try { Scanner tempScanner = new Scanner( System.in ); System.out.println( "Enter the name of the file containing the student records:" ); String fileName = tempScanner.nextLine(); input = new Scanner( new File( fileName ) ); } // end try catch ( FileNotFoundException fileNotFoundException ) { System.err.println( "Error opening file." ); System.exit( 1 ); } // end catch } // end method openFile // read record from file public void readRecords() { // object to be written to screen Student record = new Student(); double total = 0; // stores total of all grades int gradeCounter = 0; // counts grades input System.out.printf( " %-12s%-12s%-12s%10s ", "Student ID", "First Name", "Last Name", "Grade" ); try // read records from file using Scanner object { while ( input.hasNext() ) { record.setStudentID( input.nextInt() ); // read account number record.setFirstName( input.next() ); // read first name record.setLastName( input.next() ); // read last name record.setGrade( input.nextDouble() ); // read balance // display record contents System.out.printf( "%-12d%-12s%-12s%10.2f ", record.getStudentID(), record.getFirstName(), record.getLastName(), record.getGrade() ); ++gradeCounter; total += record.getGrade(); } // end while System.out.printf( " Class average is: %.2f ", ( total / gradeCounter ) ); } // end try catch ( NoSuchElementException elementException ) { System.err.println( "File improperly formed." ); input.close(); System.exit( 1 ); } // end catch catch ( IllegalStateException stateException ) { System.err.println( "Error reading from file." ); System.exit( 1 ); } // end catch } // end method readRecords // close file and terminate application public void closeFile() { if ( input != null ) input.close(); // close file } // end method closeFile } // end class ReadTextFile 

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

// Coding Exercise 1: Student.java // A class that represents one record of student information. public class Student { private int studentID; private String firstName; private String lastName; 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 ) { setStudentID( id ); setFirstName( first ); setLastName( last ); setGrade( grade ); } // end four-argument Student constructor // set student ID number public void setStudentID( int id ) { studentID = id; } // end method setStudentID // get student ID number public int getStudentID() { return studentID; } // end method getStudentID // set first name public void setFirstName( String first ) { firstName = first; } // end method setFirstName // get first name public String getFirstName() { return firstName; } // end method getFirstName // set last name public void setLastName( String last ) { lastName = last; } // end method setLastName // get last name public String getLastName() { return lastName; } // end method getLastName // set grade public void setGrade( double gradeValue ) { grade = gradeValue; } // end method setGrade // get grade public double getGrade() { return grade; } // end method getGrade } // end class Student 

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

1 Suzy Green 88.60 2 Jessica Purple 98.30 3 Paul Orange 75.90 

How do I fix it to get that output?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions