Question: import java.io.FileNotFoundException; import java.io.File; import java.io.PrintWriter; import java.util.Scanner; public class Main { static final int MAX_STUDENTS = 45; static final int FIELD_SID = 0; static
import java.io.FileNotFoundException; import java.io.File; import java.io.PrintWriter; import java.util.Scanner;
public class Main { static final int MAX_STUDENTS = 45; static final int FIELD_SID = 0; static final int FIELD_FIRST = 1; static final int FIELD_LAST = 2; static Student[] roster = new Student[MAX_STUDENTS]; static int numberOfStudents = 0; public static void main(String[] args) { try { loadStudentsFromFile("students.txt"); loadLabScoresFromFile("labscores.txt"); loadLabGradesFromFile("labgrades.txt"); writeStudentRecordsToFile("studentrecords.txt"); displayStudentRecords(); } catch ( FileNotFoundException e) { System.err.println( e.getMessage() ); } }// end main() /* TODO (1): * Implement the method. * * The method reads from the specified file a number of student records. The * student records are stored in the array 'roster'. * Method declares the exception FileNotFoundException. */ public static void loadStudentsFromFile(String filename) throws FileNotFooundException{ try (Scanner fin = new Scanner (new File(filename));) { int sid; String first; String last; while ( fin.hasNextLine() ) { studentRecord = fin.nextLine(); fields = studentRecord.split(","); sid = Integer.parseInt(fields[FIELD_SID]); first = fields[FIELD_FIRST]; last = fields[FIELD_LAST]; coursesTaken = fin.nextLine(); fields = coursesTaken.split(","); roster[numberOfStudents++] = new Student(sid, first, last, fields); } } }// end loadStudentsFromFile() /* TODO (2): * Implement the method. * * The method reads from the specified file a number of student lab scores. * The scores are stored in the 'labScore' array of each student record. The * lab score records correspond to each student record from the 'students.txt' * file. * Method declares the exception FileNotFoundException. */ public static void loadLabScoresFromFile(String filename) throws FileNotFoundException{ try ( Scanner fin = new Scanner( new File( filename ) ); ) { int index = 0; while ( fin.hasNextInt() ) { numbers[index++] = fin.nextInt(); } } }// end loadLabScoresFromFile() /* TODO (3): * Implement the method. * * The method reads from the specified file a number of student lab grades. * The grades are stored in the 'labGrade' array of each student record. The * lab grade records correspond to each student record from the 'students.txt' * file. * Method declares the exception FileNotFoundException. */ public static void loadLabGradesFromFile(String filename) { }// end loadLabGradesFromFile() /* TODO (4): * Implement the method. * * The method writes the student records to the specified file. Each record is placed * on a separate line and includes the student information, lab scores and lab grades. * * E.g. 100,f1,F1,25,25,25,A,A,A * * Method declares the exception FileNotFoundException. */ public static void writeStudentRecordsToFile(String filename) { }// end displayStudentRecords() /* TODO (5): * Implement the method. * * The method displays each student record to the screen, one record per line. * The output is the result of the the student's toString method. * * E.g. [100][F1, f1][25, 25, 25][A, A, A] */ public static void displayStudentRecords() { }// end displayStudentRecords()
}// end Main
public class Student { int sid = 0; String first = null; String last = null; /* There will always be 3 labs. */ int[] labScore = new int[] {0, 0, 0}; String[] labGrade = new String[] {"F", "F", "F"}; public Student(int sid, String first, String last) { this.sid = sid; this.first = first; this.last = last; }// end Student()
@Override public String toString() { String str = ""; int length = labScore.length; str += String.format("[%d][%s, %s]", sid, last, first); str += String.format("[%d", labScore[0]); for (int i = 1; i <= length - 1; i++) { str += String.format(", %d", labScore[i]); } str += "]"; str += String.format("[%s", labGrade[0]); for (int i = 1; i <= length - 1; i++) { str += String.format(", %s", labGrade[i]); } str += "]"; return str; }// end toString()
}// end Student
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
