Question: Java programming help Need help making a class diagram for the below program _______________________________________________________________________________________________________ import model.StudentReport; public class Driver { //create student report and process
Java programming help
Need help making a class diagram for the below program
_______________________________________________________________________________________________________
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
