Question: Java Programming This assignment is built on top of lab 6 Add capability to handle Grade Book for different subjects (Chemistry and Computer Science). We
Java Programming
This assignment is built on top of lab 6
Add capability to handle Grade Book for different subjects (Chemistry and Computer Science).
We will assume a maximum of five assignments and up to 40 students.
Details will be provided in the classFrom
Lab 6 - You have a Printable interface - used by Student.
Steps for completing Lab 7 -
1. Create a new interface for Faculty to use:
package adapter;
public interface Createable {
public void createGradeBook(String fname);
public void printGradeBook();
}
2. Create a class called InstructorReport
package model;
public class InstructorReport {
private Student s[] = null;
private Statistics st = null;
public InstructorReport(Student [] s, Statistics st){
this.s = s;
this.st = st
}
public void print() {
//call a method that prints info for all students - prints student []
//st.print();
}
}
3. Add methods in Util or FileIO class (Util package) to serialize and deserialize InstructorReport.
public void writetodisk(InstructorReport a1, String fname)
{
try {
FileOutputStream p = new FileOutputStream(fname);
ObjectOutputStream p1 = new ObjectOutputStream(p);
p1.writeObject(a1);
}
catch(Exception e)
{
//exception message
}
}
public InstructorReport readfromdiskforInstructor(String fname)
{
InstructorReport a= null;
try {
FileInputStream p = new FileInputStream(fname);
ObjectInputStream p1 = new ObjectInputStream(p);
a = (InstructorReport)p1.readObject();
}
catch(Exception e)
{
//exception message
}
return a;
}
4. Rename your Print class to CreatePrint.
package adapter;
public abstract class CreatePrint {
private Util u = new Util();
private Student arr[] = new Student[40];
private Statistics s = new Statistics();
private StudentReport arr2[] = new StudentReport[40];
private InstructorReport arr3 = null;
public void createGradeBook(String fname) {
//This method must be called first - before printGradeBook() or getStats() or printStudentScores()
//we will call existing methods to:
//a. read the file and build a student array - call readFile in Util
//b. compute statistics. - call methods in Statistics
//c. build StudentReport array [done in lab 6]
//d. serialize studentreport - upto 40 files. [done in lab 6]
//e. For instructor - write one file (serialized) with Student [] and Statistics[]
}
public void printGradeBook() { }
//use debug flag for printing. if debug = true then print other no printing.
public void getStats() {
//print stats from any object - read one object from disk and print stats.
}
public void printstudentscores(int id) {
//use the serialized life so your life is easy.
//pl. don't use search in studentreport array. long way. no good.
}
}
5. Create a class called GradeBook that extends the abstract class called CreatePrint
package adapter;
public class GradeBook extends CreatePrint implements Creatable, Printable {
}
//default package.
6. Testing your code
public class Driver7 {
public static void main(String [] args)
{
//Test Instructor interface
Creatable p = new GradeBook();
String fname = "c:\path to filename";
p.createGradeBook(fname);
p.printGradeBook();
//next three lines should give a compiler error - can you say why?
//p.getStats();
//p.printstudentscores(1234);
//p.printstudentscores(9111); //invalid student id shld print a friendly message - no such student.
//Test Student Interface
Printable s = p;
//s.printGradebook(); //Error
s.getStats();
s.printstudentscores(1234);
s.printstudentscores(9111); //invalid student id shld print a f
}
}
______________________________________________________________________________________________________
import model.StudentReport;
public class Driver
{
//create student report and process
public static void main(String[] args)
{
StudentReport report = new StudentReport();
report.process();
}
}
______________________________________________________________________________________________________
package model;
public class Statistics
{
// store lowest scores of students
private int[] lowScores;
// store highest scores of students
private int[] highScores;
// store averages of students
private double[] avgScores;
// store grades of students
private String[] grades;
//determines the lowest score and stores in an array
private void calcLowest(Student[] students)
{
// allocate memory
lowScores = new int[students.length];
// for each student
for (int i = 0; i < students.length; i++)
{
lowScores[i] = students[i].getLowest();
}
}
//determines the highest score and stores in an array
private void calcHighest(Student[] students)
{
// allocate memory
highScores = new int[students.length];
// for each student
for (int i = 0; i < students.length; i++)
{
highScores[i] = students[i].getHighest();
}
}
//computes average and grade of the students
private void calculateAverages(Student[] students)
{
// allocate memory
avgScores = new double[students.length];
grades = new String[students.length];
// for each student
for (int i = 0; i < students.length; i++)
{
// compute average
avgScores[i] = students[i].calcAverage();
// compute average
grades[i] = students[i].getGrade();
}
}
// print statistics for all students and all quizzes
public void printStatistics(Student[] students)
{
calcLowest(students);
calcHighest(students);
calculateAverages(students);
// Build header row
String output = "";
System.out.printf("******************************************************************** ");
output += String.format("*%-12s %-15s %-15s %-15s %s%n", "Student ID", "Highest Score", "Lowest Score",
"Average Score", "Grade* " + "********************************************************************");
for (int i = 0; i < students.length; i++)
{
output += String.format("%-12d %-15d %-15d %-15.2f %s%n", students[i].getSID(), highScores[i], lowScores[i],
avgScores[i], grades[i]);
}
System.out.printf(output);
}
}
______________________________________________________________________________________________________
package model;
import java.io.Serializable;
public class Student implements Serializable
{
private static final long serialVersionUID = -123433434343L;
private int SID;
private int scores[] = new int[5];
// getter and setter functions
public int getSID()
{
return this.SID;
}
// setter function
public void setSID(int id)
{
this.SID = id;
}
// Get scores
public int[] getScores()
{
return this.scores;
}
// set scores
public void setScores(int[] s)
{
// Make deep copy
for (int i = 0; i < s.length; i++)
{
this.scores[i] = s[i];
}
}
//determines highest score of a student
public int getHighest()
{
int highest = scores[0];
for (Integer i : scores)
{
if (i > highest)
{
highest = i;
}
}
return highest;
}
//determines lowest score of a student
public int getLowest()
{
int lowest = scores[0];
for (Integer i : scores)
{
if (i < lowest)
{
lowest = i;
}
}
return lowest;
}
//Determines the average score of a student
public double calcAverage()
{
double sum = 0;
for (Integer i : scores)
{
sum += i;
}
return sum / scores.length;
}
//determines the letter grade of the student by their average score
public String getGrade()
{
double average = calcAverage();
if (average >= 97)
return "A+";
else if (average >= 93)
return "A";
else if (average >= 90)
return "A-";
else if (average >= 87)
return "B+";
else if (average >= 83)
return "B";
else if (average >= 80)
return "B-";
else if (average >= 77)
return "C+";
else if (average >= 73)
return "C";
else if (average >= 70)
return "D+";
else if (average >= 63)
return "D";
else if (average >= 60)
return "D-";
else
return "F";
}
// prints all data of a student
public void printData()
{
System.out.printf("Student ID: " + SID + " ");
String str = "Scores: ";
for (int i = 0; i < scores.length; i++)
{
str += String.valueOf(scores[i]) + " ";
}
System.out.printf(str+" ");
System.out.printf("Lowest Score: " + getLowest()+" ");
System.out.printf("Highest Score: " + getHighest()+" ");
System.out.printf("Average Score: " + calcAverage()+" ");
System.out.printf("Letter Grade: " + getGrade()+" ");
}
}
_______________________________________________________________________________________________________
package model;
import adapter.Print;
import fileIO.Util;
public class StudentReport
{
//reads data, loads in an array, prints report
public void process()
{
// Load File
Student students[] = Util.readFile("Scores.txt");
// Serialize
Util.serializeStudents(students);
// Print Statistics
Print printer = new Print();
printer.printStats();
System.out.printf(" ******************************************* ");
System.out.printf("*Student Individual Reports by Student ID:* ");
System.out.printf("******************************************* ");
for(int i=0; i { System.out.printf("STUDENT #" + (i+1)+" "); // print the student printer.printStudentScores(students[i].getSID()); System.out.printf(" "); } } } _______________________________________________________________________________________________________ package fileIO; import java.io.IOException; import java.io.File; import java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.FileInputStream; import java.util.Scanner; import model.Student; public class Util { // binary file name private static final String BINARY_FILE = "students.dat"; private static int numStudents; //loads students into an array from the file public static Student[] readFile(String fileName) { // Get student count numStudents = countStudents(fileName); // Create array to hold students, hold all students in file Student students[] = new Student[numStudents]; try { File file = new File(fileName); Scanner scanner = new Scanner(file); // ignore first line scanner.nextLine(); // Create Student records and store into array for (int i = 0; i < numStudents; i++) { students[i] = new Student(); students[i].setSID(scanner.nextInt()); // read scores int scores[] = new int[5]; for (int j = 0; j < 5; j++) { scores[j] = scanner.nextInt(); } // set score students[i].setScores(scores); } // close file scanner.close(); } catch (IOException e) { System.out.printf("Error -- " + e.toString()); } return students; } //reads number of students in the file private static int countStudents(String fileName) { int count = 0; try { File file = new File(fileName); Scanner scanner = new Scanner(file); // ignore first line scanner.nextLine(); // read line by line and count it while (scanner.hasNextLine()) { scanner.nextLine(); count++; } // close scanner scanner.close(); } catch (Exception e) { e.printStackTrace(); } return count; } //serialize student data into a binary file public static void serializeStudents(Student students[]) { try { // open object output stream File file = new File(BINARY_FILE); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); // write objects to file for (Student std : students) { oos.writeObject(std); } // close stream oos.close(); } catch (Exception e) { e.printStackTrace(); } } //find student by ID in the binary file public static Student getStudentByID(int id) { Student student = null; try { // open object output stream File file = new File(BINARY_FILE); FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); Student std; // read students while ((std = (Student) ois.readObject()) != null) { if (std.getSID() == id) { student = std; break; } } // close stream ois.close(); } catch (Exception e) { e.printStackTrace(); } return student; } //de-serialize students public static Student[] getStudents() { Student students[] = new Student[numStudents]; try { // open object output stream File file = new File(BINARY_FILE); FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); Student std; // read students int counter = 0; while (counter < numStudents && (std = (Student) ois.readObject()) != null) { students[counter++] = std; } // close stream ois.close(); } catch (Exception e) { e.printStackTrace(); } return students; } } _____________________________________________________________________________________________________ package adapter; import model.Student; import model.Statistics; import fileIO.Util; public class Print implements Printable { //Print Students statistics, read from the serialized file public void printStats() { // Get all the Scores. Student[] stds = Util.getStudents(); // Statistics object. Statistics stats = new Statistics(); stats.printStatistics(stds); } //print statistics function by ID public void printStudentScores(int id) { //Find Student by ID Student std = Util.getStudentByID(id); if(std == null) { System.out.printf("No Such Student exist with ID: " + id); } else { std.printData(); } } } _______________________________________________________________________________________________________ package adapter; public interface Printable { //Print Students statistics, read from the serialized file public void printStats(); //print statistics function by ID public void printStudentScores(int id); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
