This assignment is built on top of Assignment 2.
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.
This is my code from assignment 2:
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%6dn”, “High Score”, max1, max2, max3, max4, max5);
System.out.printf(“%-12s%6d%6d%6d%6d%6dn”, “Low Score”, min1, min2, min3, min5, min5);
System.out.printf(“%-12s%6.1f%6.1f%6.1f%6.1f%6.1fn”, “Average”, sum1/count, sum2/count, sum3/count, sum4/count, sum5/count);
}
}
Expert Answer
Code :
//Student.java
import java.io.Serializable;
public class Student implements Serializable
{
private int Stud;
//for chemistry
private int chQu1;
private int chQu2;
private int chQu3;
private int chQu4;
private int chQu5;
//for computer science
private int compQu1;
private int compQu2;
private int compQu3;
private int compQu4;
private int compQu5;
public Student(int stud, int chqu1, int chqu2, int chqu3, int chqu4, int chqu5, int compqu1, int compqu2, int compqu3, int compqu4, int compqu5)
{
Stud = stud;
//for chemistry
chQu1 = chqu1;
chQu2 = chqu2;
chQu3 = chqu3;
chQu4 = chqu4;
chQu5 = chqu5;
//for computer science
compQu1 = compqu1;
compQu2 = compqu2;
compQu3 = compqu3;
compQu4 = compqu4;
compQu5 = compqu5;
}
public int getStud()
{
return Stud;
}
public void setStud(int stud)
{
Stud = stud;
}
//for chemistry
public int getChQu1()
{
return chQu1;
}
public void setChQu1(int chqu1)
{
chQu1 = chqu1;
}
public int getChQu2()
{
return chQu2;
}
public void setChQu2(int chqu2)
{
chQu2 = chqu2;
}
public int getChQu3()
{
return chQu3;
}
public void setChQu3(int chqu3)
{
chQu3 = chqu3;
}
public int getChQu4()
{
return chQu4;
}
public void setChQu4(int chqu4)
{
chQu4 = chqu4;
}
public int getChQu5()
{
return chQu5;
}
public void setChQu5(int chqu5)
{
chQu5 = chqu5;
}
//for computer science
public int getCompQu1()
{
return compQu1;
}
public void setCompQu1(int compqu1)
{
compQu1 = compqu1;
}
public int getCompQu2()
{
return compQu2;
}
public void setCompQu2(int compqu2)
{
compQu2 = compqu2;
}
public int getCompQu3()
{
return compQu3;
}
public void setCompQu3(int compqu3)
{
compQu3 = compqu3;
}
public int getCompQu4()
{
return compQu4;
}
public void setCompQu4(int compqu4)
{
compQu4 = compqu4;
}
public int getCompQu5()
{
return compQu5;
}
public void setCompQu5(int compqu5)
{
compQu5 = compqu5;
}
public String toString()
{
return Stud + ” ” + chQu1 + ” ” + chQu2 + ” ” + chQu3 + ” ” + chQu4 + ” ” + chQu5 + ” ” + compQu1 + ” ” + compQu2 + ” ” + compQu3 + ” ” + compQu4 + ” ” + compQu5;
}
}
// 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, 90, 36, 90, 77, 30);
students[1] = new Student(2134, 90, 36, 90, 77, 30, 52, 7, 100, 78, 34);
students[2] = new Student(3124, 100, 45, 20, 90, 70, 11, 17, 81, 32, 77);
students[3] = new Student(4532, 11, 17, 81, 32, 77, 100, 45, 20, 90, 70);
students[4] = new Student(5678, 20, 12, 45, 78, 34, 34, 80, 55, 78, 45);
students[5] = new Student(6134, 34, 80, 55, 78, 45, 20, 12, 45, 78, 34);
students[6] = new Student(7874, 60, 100, 56, 78, 78, 70, 10, 66, 78, 56);
students[7] = new Student(8026, 70, 10, 66, 78, 56, 60, 100, 56, 78, 78);
students[8] = new Student(9893, 34, 9, 77, 78, 20, 45, 40, 88, 78, 55);
students[9] = new Student(1947, 45, 40, 88, 78, 55, 34, 9, 77, 78, 20);
students[10] = new Student(2877, 55, 50, 99, 78, 80, 22, 70, 100, 78, 77);
students[11] = new Student(3189, 22, 70, 100, 78, 77, 55, 50, 99, 78, 80);
students[12] = new Student(4602, 89, 50, 91, 78, 60, 0, 98, 89, 78, 20);
students[13] = new Student(5405, 11, 11, 0, 78, 10, 89, 50, 91, 78, 60);
students[14] = new Student(6999, 0, 98, 89, 78, 20, 11, 11, 0, 78, 10);
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;
//for chemistry
int chmax1 = 0, chmin1 = 0; double chsum1 = 0;
int chmax2 = 0, chmin2 = 0; double chsum2 = 0;
int chmax3 = 0, chmin3 = 0; double chsum3 = 0;
int chmax4 = 0, chmin4 = 0; double chsum4 = 0;
int chmax5 = 0, chmin5 = 0; double chsum5 = 0;
//for computer science
int compmax1 = 0, compmin1 = 0; double compsum1 = 0;
int compmax2 = 0, compmin2 = 0; double compsum2 = 0;
int compmax3 = 0, compmin3 = 0; double compsum3 = 0;
int compmax4 = 0, compmin4 = 0; double compsum4 = 0;
int compmax5 = 0, compmin5 = 0; double compsum5 = 0;
FileInputStream fis = new FileInputStream(“scores.txt”);
ObjectInputStream ois = new ObjectInputStream(fis);
while(true)
{
try
{
Student st = (Student)ois.readObject();
count++;
if(count == 1)
{
//for chemistry
chmax1 = st.getChQu1();
chmin1 = st.getChQu1();
chmax2 = st.getChQu2();
chmin2 = st.getChQu2();
chmax3 = st.getChQu3();
chmin3 = st.getChQu3();
chmax4 = st.getChQu4();
chmin4 = st.getChQu4();
chmax5 = st.getChQu5();
chmin5 = st.getChQu5();
//for computer science
compmax1 = st.getCompQu1();
compmin1 = st.getCompQu1();
compmax2 = st.getCompQu2();
compmin2 = st.getCompQu2();
compmax3 = st.getCompQu3();
compmin3 = st.getCompQu3();
compmax4 = st.getCompQu4();
compmin4 = st.getCompQu4();
compmax5 = st.getCompQu5();
compmin5 = st.getCompQu5();
}
else
{
//for chemistry
if(st.getChQu1() > chmax1)
chmax1 = st.getChQu1();
if(st.getChQu2() > chmax2)
chmax2 = st.getChQu2();
if(st.getChQu3() > chmax3)
chmax3 = st.getChQu3();
if(st.getChQu4() > chmax4)
chmax4 = st.getChQu4();
if(st.getChQu5() > chmax5)
chmax5 = st.getChQu5();
if(st.getChQu1() < chmin1)
chmin1 = st.getChQu1();
if(st.getChQu2() < chmin2)
chmin2 = st.getChQu2();
if(st.getChQu3() < chmin3)
chmin3 = st.getChQu3();
if(st.getChQu4() < chmin4)
chmin4 = st.getChQu4();
if(st.getChQu5() < chmin5)
chmin5 = st.getChQu5();
//for computer science
if(st.getCompQu1() > compmax1)
compmax1 = st.getCompQu1();
if(st.getCompQu2() > compmax2)
compmax2 = st.getCompQu2();
if(st.getCompQu3() > compmax3)
compmax3 = st.getCompQu3();
if(st.getCompQu4() > compmax4)
compmax4 = st.getCompQu4();
if(st.getCompQu5() > compmax5)
compmax5 = st.getCompQu5();
if(st.getCompQu1() < compmin1)
compmin1 = st.getCompQu1();
if(st.getCompQu2() < compmin2)
compmin2 = st.getCompQu2();
if(st.getCompQu3() < compmin3)
compmin3 = st.getCompQu3();
if(st.getCompQu4() < compmin4)
compmin4 = st.getCompQu4();
if(st.getCompQu5() < compmin5)
compmin5 = st.getCompQu5();
}
//for chemistry
chsum1 += st.getChQu1();
chsum2 += st.getChQu2();
chsum3 += st.getChQu3();
chsum4 += st.getChQu4();
chsum5 += st.getChQu5();
//for computer science
compsum1 += st.getCompQu1();
compsum2 += st.getCompQu2();
compsum3 += st.getCompQu3();
compsum4 += st.getCompQu4();
compsum5 += st.getCompQu5();
}
catch(EOFException e)
{
break;
}
}
ois.close();
fis.close();
//for chemistry
System.out.println(“Chemistry Details : “);
System.out.printf(“%-12s%6d%6d%6d%6d%6dn”, “High Score”, chmax1, chmax2, chmax3, chmax4, chmax5);
System.out.printf(“%-12s%6d%6d%6d%6d%6dn”, “Low Score”, chmin1, chmin2, chmin3, chmin5, chmin5);
System.out.printf(“%-12s%6.1f%6.1f%6.1f%6.1f%6.1fn”, “Average”, chsum1/count, chsum2/count, chsum3/count, chsum4/count, chsum5/count);
//for computer science
System.out.println(“Computer Science Details : “);
System.out.printf(“%-12s%6d%6d%6d%6d%6dn”, “High Score”, compmax1, compmax2, compmax3, compmax4, compmax5);
System.out.printf(“%-12s%6d%6d%6d%6d%6dn”, “Low Score”, compmin1, compmin2, compmin3, compmin5, compmin5);
System.out.printf(“%-12s%6.1f%6.1f%6.1f%6.1f%6.1fn”, “Average”, compsum1/count, compsum2/count, compsum3/count, compsum4/count, compsum5/count);
}
}
Output: