Question
[Java HELP] Need help to complete rest of code This assignment is built using the code from Assignment 5. In this assignment create a separate
[Java HELP] Need help to complete rest of code
This assignment is built using the code from Assignment 5.
In this assignment create a separate serialized file using ObjectOutputStream for each student.
File should contain student's score and statistics for entire class.
Using debug mode (with Debug flag) print the contents of the serialized file.
Create and implement an interface to:
Print student statistics.
Print scores for a given student id.
implement the Debug flag globally
Use an abstract class to implement all the methods declared in an interface.
points.txt
Stud Qu1 Qu2 Qu3 Qu4 Qu5 1234 052 007 100 078 034 2134 090 036 090 077 030 3124 100 045 020 090 070 4532 011 017 081 032 077 5678 020 012 045 078 034 6134 034 080 055 078 045 7874 060 100 056 078 078 8026 070 010 066 078 056 9893 034 009 077 078 020 1947 045 040 088 078 055 2877 055 050 099 078 080 3189 022 070 100 078 077 4602 089 050 091 078 060 5405 011 011 000 078 010 6999 000 098 089 078 020
package driver;
import java.io.FileNotFoundException;
import db.*; import util.Util;
public class Driver { public static final boolean DEBUG_MODE = true; public static void main(String[] args) throws FileNotFoundException { Student lab2[] = new Student[40]; // Populate the student array for (int i = 0; i < 40; i++) { lab2[i] = new Student(); } lab2 = Util.readFile("C:\\Users\\Jordan\\workspace\\Assignment 5\\src\\db\\points.txt", lab2); System.out.printf("Stud Qu1 Qu2 Qu3 Qu4 Qu5 "); for (int row = 0; row < Util.numOfStudent; row++) { Student.printInfo(lab2[row]); } Statistics s = new Statistics(); // // s.findlow(lab6); // s.printlow(); // s.findhigh(lab6); // s.printhigh(); // s.findavg(lab6); // s.printavg(); s.printStatistics(lab2); }
}
package util;
import java.io.*; import java.util.StringTokenizer;
import db.Student;
public class Util {
public static int numOfStudent = 0; public void Write(Student stud, String filename) { FileOutputStream fileOut; ObjectOutputStream out; try { fileOut = new FileOutputStream(filename + ".ser"); out = new ObjectOutputStream(fileOut); out.writeObject(stud); out.flush(); out.close(); fileOut.close(); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } public Student Read(String filename) { Student std = null; try { FileInputStream filein = new FileInputStream(filename + ".ser"); ObjectInputStream in = new ObjectInputStream(filein); std = (Student)in.readObject(); in.close(); filein.close(); }catch (Exception e) { e.printStackTrace(); } return std; } public static Student[] readFile(String filename, Student[] stu) { try { // Reads the file and builds student array. // Open the file using FileReader Object. FileReader file = new FileReader(filename); BufferedReader buffer = new BufferedReader(file);
int count = 0, temp = 0; String line; int[] scores = new int[5];
while ((line = buffer.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) { if (temp == 0) stu[count].setSID(Integer.parseInt(st.nextToken())); else scores[temp - 1] = Integer.parseInt(st.nextToken());
temp++; stu[count].recordScores(scores); } temp = 0; count++;
} countStudents(count); buffer.close(); } catch (IOException e) { System.out.println("Error: " + e); } return stu; }
public static void countStudents(int numOfStudents) { numOfStudent = numOfStudents; }
}
package db;
import java.io.Serializable;
import adapter.GCInterface; import util.Util;
public class StudentGrade implements Serializable, GCInterface { private Student stu; private Statistics stat; Util fileio = new Util(); public void writeToDisk(StudentGrade a1){ } public StudentGrade(Student stu, Statistics stat){ super(); this.stu=stu; this.stat = stat; } public void print(boolean DEBUG){ //using debug mode with debug flag print the contentds of the the serialized file if(DEBUG = true){ //call the print methods of classes used for instance variables } } //add any other methods as ytouse see fit }
package db;
import java.io.Serializable;
public class Student implements Serializable { private int SID; protected int scores[] = new int[5];
// write public get and set methods for // SID and scores public Student(){ } public int getSID() { return SID; }
public void setSID(int SID) { this.SID = SID; }
public int getScores(int numOfQuiz) { return scores[numOfQuiz]; }
public void setScores(int[] scores) { this.scores = scores; }
public void recordScores(int[] scores) { for (int i = 0; i < 5; i++) { this.scores[i] = scores[i]; } }
public static void printInfo(Student stu) { System.out.printf("%d ", stu.getSID()); for (int i = 0; i < 5; i++) { System.out.printf("%3d ", stu.getScores(i)); } System.out.printf(" "); }
} // end class Stud
package db;
import db.Student; import util.Util;
public class Statistics { float[] lowscores = new float[5]; float[] highscores = new float[5]; float[] avgscores = new float[5];
int numStudent = Util.numOfStudent;
void findlow(Student[] a) {
int low = 100;
for (int col = 0; col < 5; col++) { for (int row = 0; row < numStudent; row++) { if (low > a[row].getScores(col)) { low = a[row].getScores(col); } } lowscores[col] = low; low = 100; } }
void findhigh(Student[] a) {
int high = 0;
for (int col = 0; col < 5; col++) { for (int row = 0; row < numStudent; row++) { if (high < a[row].getScores(col)) { high = a[row].getScores(col); } } highscores[col] = high; high = 0; }
}
void findavg(Student[] a) {
int sum = 0;
for (int col = 0; col < 5; col++) { for (int row = 0; row < numStudent; row++) { sum += a[row].getScores(col); } // end inner for if (numStudent == 0) avgscores[col] = 0; else avgscores[col] = (float) sum / numStudent;
sum = 0; } }
// public void printlow() { // System.out.printf("Low Score\t"); // for (int i = 0; i < 5; i++) { // System.out.printf("%.2f ",lowscores[i]); // } // System.out.printf(" "); // } // // public void printhigh() { // System.out.printf("High Score\t"); // for (int i = 0; i < 5; i++) { // System.out.printf("%.2f ", highscores[i]); // } // System.out.printf(" "); // } // // public void printavg() { // System.out.printf("Average\t\t"); // for (int i = 0; i < 5; i++) { // System.out.printf("%4.1f ", avgscores[i]); // } public void printStatistics(Student[] a) { System.out.printf("Low Score\t"); for (int i = 0; i < 5; i++) { findlow(a); System.out.printf("%.2f ", lowscores[i]); } System.out.printf(" "); System.out.printf("High Score\t"); for (int j = 0; j < 5; j++) { findhigh(a); System.out.printf("%.2f ", highscores[j]); } System.out.printf(" "); System.out.printf("Average\t\t"); for (int l = 0; l < 5; l++) { findavg(a); System.out.printf("%4.1f ", avgscores[l]); } }
}
package db;
import util.Util;
public class courses { private Student students[]; private Statistics stats;
public courses(String filename) { students = Util.readFile(filename, students); stats = new Statistics();
}
public Student[] getStudents() { return students; }
public void setStudents(Student[] students) { this.students = students; }
public Statistics getStats() { return stats; }
public void setStats(Statistics stats) { this.stats = stats; } }
package adapter;
public class StudentIntIImpl implements ReportCard{
public void buildStudentGrade(String a1){ } @Override public void printStudentStatistics() { // TODO Auto-generated method stub }
@Override public void printStudentScores(int id) { // TODO Auto-generated method stub }
}
package adapter;
public interface ReportCard { public void printStudentStatistics(); public void printStudentScores(int id); }
package adapter;
public class publicAPI extends StudentIntIImpl{
}
package adapter;
public interface GCInterface { public boolean DEBUG = true; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started