Question
-------------------------- Assignment 7
--------------------------
Assignment 7 <----- Need help with.
--------------------------
This assignment is built on top of Assignment 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 class.
I need help with Assignment 7 which is built upon Assignment 5 and 6 which I have already done, can anyone help me with adding the gradebook and other subjects to my code? I have already the code for Assignment 5 and 6 below, I do not need help with those as I have already done them but can someone help add on to those codes with Assignment 7? The reason why I posted both 5 and 6 is because Assignment 7 is built around those programs.
Assignment 7 help please. Down below are just references.
-------------------------
Assignment 6 <------- Already done
------------------------
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.
Here is the code for Assignment 5:
Student class :
package driverstudentquiz;
public class Student { private int SID; private int scores[] = new int[5]; //get and set functions for scores and ID public int getSID(){ return this.SID; } public void setSID(int id){ this.SID= id; } public int[] getScores(){ return this.scores; } public void setScores(int[] s){ this.scores= s; } public void printData(){ //print all data of a student System.out.println("\t\t\t\t"+ SID +"\t"+ scores[0] +"\t"+ scores[1] +"\t"+ scores[2] + "\t"+ scores[3] +"\t"+ scores[4] +" "); } }
Statistics class :
package driverstudentquiz;
public class Statistics { private int[] lowscores= new int[5]; private int[] highscores= new int[5]; private float[] avgscores= new float[5]; void findlow(Student[] a, int c){ // finds low score. int i=0; for(i=0;i<5; i++){ int[] temp= a[0].getScores(); int min = temp[i]; for(int j=0; j< c; j++){ int[] s= a[j].getScores(); if(min > s[i]){ min = s[i]; } } lowscores[i]= min; } } void findhigh(Student[] a, int c){ // find high score int i=0; for(i=0;i<5; i++){ int[] temp= a[0].getScores(); int max = temp[i]; for(int j=0; j< c; j++){ int[] s= a[j].getScores(); if(max < s[i]){ max = s[i]; } } highscores[i]= max; } }
void findavg(Student[] a, int c){ // find average score. int i=0; for(i=0;i<5; i++){ int sum =0; for(int j=0; j int[] s= a[j].getScores(); sum= sum + s[i]; } avgscores[i]= sum/c; } } public void printStatistics(Student[] a, int c){ // print statistics for all students and all quizes findlow(a,c); findhigh(a,c); findavg(a,c); System.out.println("Lowest score :\t\t"+ lowscores[0] +"\t"+ lowscores[1] +"\t"+ lowscores[2] +"\t"+ lowscores[3] +"\t"+ lowscores[4] ); System.out.println("High score :\t\t"+ highscores[0] +"\t"+ highscores[1] +"\t"+ highscores[2] +"\t"+ highscores[3] +"\t"+ highscores[4] ); System.out.println("Average score :\t\t"+ avgscores[0] +"\t"+ avgscores[1] +"\t"+ avgscores[2] +"\t"+ avgscores[3] +"\t"+ avgscores[4] );
} }
Util class :
package driverstudentquiz; import java.io.*; import java.util.StringTokenizer;
public class Util { static int studentCount; static Student [] readFile(String filename, Student [] stu) { // Reads the file and builds student array. int i=0;
try { FileReader file = new FileReader(filename); //Open the file using FileReader Object. BufferedReader buff = new BufferedReader(file); boolean eof = false; while (!eof) { String line = buff.readLine(); //In a loop read a line using readLine method.
if (line == null) eof = true; else{ // System.out.println(line); stu[i]= new Student(); StringTokenizer st = new StringTokenizer(line); //Tokenize each line using StringTokenizer Object while(st.hasMoreTokens()){ stu[i].setSID(Integer.parseInt(st.nextToken())); // Value is then saved in the right property of Student Object. int[] arr= new int[5]; for(int j=0;j<5;j++){ arr[j]= Integer.parseInt(st.nextToken()); //Each token is converted from String to Integer using parseInt method } stu[i].setScores(arr); // Value is then saved in the right property of Student Object. } } i++; } buff.close(); } catch (IOException e) { System.out.println("Error -- " + e.toString()); } studentCount = i-1; return stu; } }
Driver class :
package driverstudentquiz;
public class DriverStudentQuiz { public static void main(String[] args) { Student arrStudent[] = new Student[40]; int studentCount=0; arrStudent = Util.readFile("Data.txt", arrStudent); studentCount= Util.studentCount; // find number of lines from file, which shows no. of students in array. Statistics s = new Statistics(); for(int i = 0; i // print student data fetched from file arrStudent[i].printData(); } s.printStatistics(arrStudent,studentCount); // print statistics of students } }
Assignment 6 Code (Different Rebuilt Code):
Complete Program: // Student.java import java.io.Serializable; public class Student implements Serializable { private int Stud; private int Qu1; private int Qu2; private int Qu3; private int Qu4; private int Qu5; public Student(int stud, int qu1, int qu2, int qu3, int qu4, int qu5) { Stud = stud; Qu1 = qu1; Qu2 = qu2; Qu3 = qu3; Qu4 = qu4; Qu5 = qu5; } public int getStud() { return Stud; } public void setStud(int stud) { Stud = stud; } public int getQu1() { return Qu1; } public void setQu1(int qu1) { Qu1 = qu1; } public int getQu2() { return Qu2; } public void setQu2(int qu2) { Qu2 = qu2; } public int getQu3() { return Qu3; } public void setQu3(int qu3) { Qu3 = qu3; } public int getQu4() { return Qu4; } public void setQu4(int qu4) { Qu4 = qu4; } public int getQu5() { return Qu5; } public void setQu5(int qu5) { Qu5 = qu5; } public String toString() { return Stud + " " + Qu1 + " " + Qu2 + " " + Qu3 + " " + Qu4 + " " + Qu5; } }
// StudentTest.java import java.io.*; public class StudentTest { public static final long serialVersionUID = 42L; public static void main(String[] args) throws IOException, ClassNotFoundException { Student[] students = new Student[15]; students[0] = new Student(1234, 52, 7, 100, 78, 34); students[1] = new Student(2134, 90, 36, 90, 77, 30); students[2] = new Student(3124, 100, 45, 20, 90, 70); students[3] = new Student(4532, 11, 17, 81, 32, 77); students[4] = new Student(5678, 20, 12, 45, 78, 34); students[5] = new Student(6134, 34, 80, 55, 78, 45); students[6] = new Student(7874, 60, 100, 56, 78, 78); students[7] = new Student(8026, 70, 10, 66, 78, 56); students[8] = new Student(9893, 34, 9, 77, 78, 20); students[9] = new Student(1947, 45, 40, 88, 78, 55); students[10] = new Student(2877, 55, 50, 99, 78, 80); students[11] = new Student(3189, 22, 70, 100, 78, 77); students[12] = new Student(4602, 89, 50, 91, 78, 60); students[13] = new Student(5405, 11, 11, 0, 78, 10); students[14] = new Student(6999, 0, 98, 89, 78, 20); FileOutputStream fos = new FileOutputStream("scores.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); for(int i = 0; i < students.length; i++) { oos.writeObject(students[i]); } oos.close(); fos.close(); int count = 0; int max1 = 0, min1 = 0; double sum1 = 0; int max2 = 0, min2 = 0; double sum2 = 0; int max3 = 0, min3 = 0; double sum3 = 0; int max4 = 0, min4 = 0; double sum4 = 0; int max5 = 0, min5 = 0; double sum5 = 0; FileInputStream fis = new FileInputStream("scores.txt"); ObjectInputStream ois = new ObjectInputStream(fis); while(true) { try { Student st = (Student)ois.readObject(); count++; if(count == 1) { max1 = st.getQu1(); min1 = st.getQu1(); max2 = st.getQu2(); min2 = st.getQu2(); max3 = st.getQu3(); min3 = st.getQu3(); max4 = st.getQu4(); min4 = st.getQu4(); max5 = st.getQu5(); min5 = st.getQu5(); } else { if(st.getQu1() > max1) max1 = st.getQu1(); if(st.getQu2() > max2) max2 = st.getQu2(); if(st.getQu3() > max3) max3 = st.getQu3(); if(st.getQu4() > max4) max4 = st.getQu4(); if(st.getQu5() > max5) max5 = st.getQu5(); if(st.getQu1() < min1) min1 = st.getQu1(); if(st.getQu2() < min2) min2 = st.getQu2(); if(st.getQu3() < min3) min3 = st.getQu3(); if(st.getQu4() < min4) min4 = st.getQu4(); if(st.getQu5() < min5) min5 = st.getQu5(); } sum1 += st.getQu1(); sum2 += st.getQu2(); sum3 += st.getQu3(); sum4 += st.getQu4(); sum5 += st.getQu5(); } catch(EOFException e) { break; } } ois.close(); fis.close(); System.out.printf("%-12s%6d%6d%6d%6d%6d ", "High Score", max1, max2, max3, max4, max5); System.out.printf("%-12s%6d%6d%6d%6d%6d ", "Low Score", min1, min2, min3, min5, min5); System.out.printf("%-12s%6.1f%6.1f%6.1f%6.1f%6.1f ", "Average", sum1/count, sum2/count, sum3/count, sum4/count, sum5/count); } }
(Does not have abstract classes but requires it)
--------------------------
Assignment 5 <--- Already done
--------------------------
Object Relationship and File IO
Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number.
The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is to be read from a text file. The output from the program should be similar to the following:
Here is some sample data (not to be used) for calculations:
Stud Q1 Q2 Q3 Q4 Q5
1234 78 83 87 91 86
2134 67 77 84 82 79
1852 77 89 93 87 71
High Score 78 89 93 91 86
Low Score 67 77 84 82 71
Average 73.4 83.0 88.2 86.6 78.6
The program should print the lowest and highest scores for each quiz.
Plan of Attack
Learning Objectives
You will apply the following topics in this assignment:
File Input operations.
Working and populating an array of objects.
Wrapper Classes.
Object Oriented Design and Programming.
Understanding Requirements
Here is a copy of actual data to be used for input.
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
Essentially, you have to do the following:
Read Student data from a text file.
Compute High, Low and Average for each quiz.
Print the Student data and display statistical information like High/Low/Average..
Design
This program can be written in one class. But dividing the code into simple and modular classes based on functionality, is at the heart of Object Oriented Design.
You must learn the concepts covered in the class and find a way to apply.
Please make sure that you put each class in its own .java file.
package lab2;
class Student {
private int SID;
private int scores[] = new int[5];
//write public get and set methods for
//SID and scores
//add methods to print values of instance variables.
}
/************************************************************************************/
package lab2;
class Statistics
{
int [] lowscores = new int [5];
int [] highscores = new int [5];
float [] avgscores = new float [5];
void findlow(Student [] a) {
/* This method will find the lowest score and store it in an array names lowscores. */
}
void findhigh(Student [] a) {
/* This method will find the highest score and store it in an array names highscores. */
}
void findavg(Student [] a) {
/* This method will find avg score for each quiz and store it in an array names avgscores. */
}
//add methods to print values of instance variables.
}
************************************************************************************/
package lab2;
class Util {
Student [] readFile(String filename, Student [] stu) {
//Reads the file and builds student array.
//Open the file using FileReader Object.
//In a loop read a line using readLine method.
//Tokenize each line using StringTokenizer Object
//Each token is converted from String to Integer using parseInt method
//Value is then saved in the right property of Student Object.
}
}
************************************************************************************/
//Putting it together in driver class:
public static void main(String [] args) {
Student lab2 [] = new Student[40];
//Populate the student array
lab2 = Util.readFile("filename.txt", lab2);
Statistics statlab2 = new Statistics();
statlab2.findlow(lab2);
//add calls to findhigh and find average
//Print the data and statistics
}
Topics to Learn
Working with Text Files
//ReadSource.java -- shows how to work with readLine and FileReader
public class ReadSource {
public static void main(String[] arguments) {
try {
FileReader file = new FileReader("ReadSource.java");
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else
System.out.println(line);
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
}
//How do you tokenize a String? You can use other ways of doing this, if you like.
StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
//How to convert a String to an Integer
int x = Integer.parseInt(String) ;
PreviousNext
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