Question: 1. given the following class: public class Student implements Serializable { private int id; private String name; private int birthYear; private char gender; //value 'M'
1. given the following class: public class Student implements Serializable { private int id; private String name; private int birthYear; private char gender; //value 'M' or 'F' private double gpa; Student(int id, String name, int birthYear, char gender) { this.id = id; this.name = name; this.birthYear = birthYear; this.gender = gender; } public int getBitrhYear() { return birthYear; } public double getGPA() { return gpa; } public char getGender() { return gender; } }
Create a separate testii class with name test having the following:
a) A static method printFile that takes a string f1 representing a serial file name of Student objects and print all objects inside that file.
b) A static int method countRecord that takes a string f1 representing a serial file name of Student objects and return number of objects inside that file.
c) A static method filterStudents that takes a string f1 representing a serial file name of Student objects and separate male and female students into separate binary files with names male.ser and female.ser.
d) A static double method sumGPA that takes a string f1 representing a serial file name of Student objects and return sum of GPA for all students inside that file.
e) A main method that creates a binary file with name student.ser of seven Student objects. Then
i. Print the file student.ser (using a call to printFile method).
ii. Call method filterStudents.
iii. Print files male.ser and female.ser (using two calls to printFile method).
iv. Calculate GPA average for males and female (using methods countRecord and sumGPA).
================================ ========================= 2. Using class Student on Q1,
a) Update class Student with name Student1 and do the following:
i. Implement inner private class Subject which has o Three instance variables: id, name, credit. o A constructor.
ii. Add to Student1 as an instance variable an array of subjects of size 5.
iii. Add a boolean method addSubject that takes an id, name and credit and add a subject to the array. Dont add if the array is full and return false.
iv. Add a toString method that return student info with all his/her subjects.
b) Create test class HW04Q2 then:
i. Create an object of student. Test methods addSubject and toString . Note: include both classes Student1 and HW04Q2 in one java file.
(in java program language)
==== I solve question one but I need help in question 2 pls pls I will be thankful (in java program language)
== the code:
Student.java
import java.io.Serializable;
public class Student implements Serializable { private int id; private String name; private int birthYear; private char gender; //value 'M' or 'F' private double gpa; Student(int id, String name, int birthYear, char gender, double gpa) { this.id = id; this.name = name; this.birthYear = birthYear; this.gender = gender; this.gpa=gpa; } public int getBirthYear() { return birthYear; } public double getGPA() { return gpa; } public char getGender() { return gender; } public String getName() {return name;}; public int getId() {return id;}; }
Testii.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
public class Testii{
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student stu1=new Student(1, "Abhishek Garg", 1995, 'M', 9.8);
Student stu2=new Student(2, "Rajat Goel", 1997, 'M', 9.5);
Student stu3=new Student(3, "Rachit Bansal", 1998, 'M', 9.2);
Student stu4=new Student(4, "Aishwarya Wasenakar", 1996, 'F', 9.7);
Student stu5=new Student(5, "Shivam Sharma", 1997, 'M', 7.8);
Student stu6=new Student(6, "Vardaan Popli", 1996, 'M', 9.0);
Student stu7=new Student(7, "Sonal Chauhan", 1996, 'F', 7.6);
String fileName="student.ser";
FileOutputStream fo=new FileOutputStream(fileName);
ObjectOutputStream oo=new ObjectOutputStream(fo);
oo.writeObject(stu1);
oo.writeObject(stu2);
oo.writeObject(stu3);
oo.writeObject(stu4);
oo.writeObject(stu5);
oo.writeObject(stu6);
oo.writeObject(stu7);
printFile(fileName);
filterStudents(fileName);
printFile("male.ser");
printFile("female.ser");
double gpa_sum=sumGPA(fileName);
int recordsCount=countRecord(fileName);
double avg=gpa_sum/recordsCount;
System.out.println(" GPA Average: "+avg);
}
public static int countRecord(String f1) throws IOException, ClassNotFoundException
{
FileInputStream fi=new FileInputStream(f1);
ObjectInputStream oi=new ObjectInputStream(fi);
int count=0;
try {
while(true)
{
oi.readObject();
count++;
}
}
catch(Exception e)
{
}
oi.close();
return count;
}
public static double sumGPA(String f1) throws IOException, ClassNotFoundException
{
FileInputStream fi=new FileInputStream(f1);
ObjectInputStream oi=new ObjectInputStream(fi);
double sum=0;
Student obj=null;
try {
while(true)
{
obj=(Student)oi.readObject();
sum+=obj.getGPA();
}
}
catch(Exception e)
{
}
oi.close();
return sum;
}
public static void filterStudents(String f1) throws IOException, ClassNotFoundException
{
FileInputStream fi=new FileInputStream(f1);
ObjectInputStream oi=new ObjectInputStream(fi);
String male="male.ser";
String female="female.ser";
FileOutputStream fo_m=new FileOutputStream(male);
ObjectOutputStream mo=new ObjectOutputStream(fo_m);
FileOutputStream fo_f=new FileOutputStream(female);
ObjectOutputStream fo=new ObjectOutputStream(fo_f);
Student obj=null;
try {
while(true)
{
obj=(Student)oi.readObject();
if(obj.getGender()=='M')
mo.writeObject(obj);
else
fo.writeObject(obj);
}
}
catch(Exception e)
{
}
oi.close();
mo.close();
fo.close();
}
public static void printFile(String f1) throws IOException, ClassNotFoundException
{
FileInputStream fi=new FileInputStream(f1);
ObjectInputStream oi=new ObjectInputStream(fi);
Student obj=null;
System.out.println(" Printing file: "+f1+" ");
try {
while(true)
{
obj=(Student)oi.readObject();
System.out.printf("%20d %20s %20d %20c %20f ", obj.getId(), obj.getName(), obj.getBirthYear(), obj.getGender(), obj.getGPA());
}
}
catch(Exception e)
{
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
