Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. given the following class: public class Student implements Serializable { pri

ID: 3705466 • Letter: 1

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' 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 test 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. Don’t 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)
pls solve number 2 also

Explanation / Answer

Hi,

As per the problem statement, Please find the solution below:

Question was too lengthy to answer , so I answered two questions.

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.List;

public class test {

  

   public static void main(String[] args) throws IOException {

      

       List<Student> studentList = new ArrayList<>();

       Student student1= new Student(1, "chegg1", 2001, 'm');

       Student student2 = new Student(2, "chegg2", 2002, 'f');

       Student student3= new Student(1, "chegg3", 2003, 'm');

       Student student4 = new Student(2, "chegg4", 2004, 'f');

       Student student5= new Student(1, "chegg5", 2001, 'm');

       Student student6 = new Student(2, "chegg6", 2002, 'f');

       Student student7= new Student(1, "chegg7", 2001, 'f');

      

       studentList.add(student1);

       studentList.add(student2);

       studentList.add(student3);

       studentList.add(student4);

       studentList.add(student5);

       studentList.add(student6);

       studentList.add(student7);

      

      

      

       ObjectOutputStream oos = null;

       FileOutputStream fout = null;

       try{

          fout = new FileOutputStream("student.ser", true);

          oos = new ObjectOutputStream(fout);

          oos.writeObject(studentList);

         

       } catch (Exception ex) {

          ex.printStackTrace();

       } finally {

          if(oos != null){

          oos.close();

          }

       }

      

      

      

       //1) printFile used to print all object inside the file student.ser

       printFile("student.ser");

       countRecords("student.ser");

   }

   private static void countRecords(String filename) throws FileNotFoundException {

      

       FileInputStream fis = new FileInputStream(filename);

       ArrayList<Object> objectsList = new ArrayList<Object>();

       boolean cont = true;

       try {

           @SuppressWarnings("resource")

           ObjectInputStream input = new ObjectInputStream(fis);

           while (cont) {

               Object obj = input.readObject();

               if (obj != null)

                   objectsList.add(obj);

               else

                   cont = false;

           }

       } catch (Exception e) {

       }

       System.out.println("No of object in "+filename + "is "+ objectsList.size());

  

      

   }

   private static void printFile(String filename) throws IOException {

       FileInputStream fis = new FileInputStream(filename);

       ArrayList<Object> objectsList = new ArrayList<Object>();

       boolean cont = true;

       try {

           @SuppressWarnings("resource")

           ObjectInputStream input = new ObjectInputStream(fis);

           while (cont) {

               Object obj = input.readObject();

               if (obj != null)

                   objectsList.add(obj);

               else

                   cont = false;

           }

       } catch (Exception e) {

       }

       for (Object obj : objectsList) {

           System.out.println("Object name: " + obj);

       }

   }

}

Please find the output below:

Object name: com.studentSerializa.impl.Student@67b64c45

Object name: com.studentSerializa.impl.Student@67b64c46

Object name: com.studentSerializa.impl.Student@67b64c47

Object name: com.studentSerializa.impl.Student@67b64c48

Object name: com.studentSerializa.impl.Student@67b64c49

Object name: com.studentSerializa.impl.Student@67b64c50

Object name: com.studentSerializa.impl.Student@67b64c51

No of object in student.ser is 7

Thanks

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.List;

public class test {

  

   public static void main(String[] args) throws IOException {

      

       List<Student> studentList = new ArrayList<>();

       Student student1= new Student(1, "chegg1", 2001, 'm');

       Student student2 = new Student(2, "chegg2", 2002, 'f');

       Student student3= new Student(1, "chegg3", 2003, 'm');

       Student student4 = new Student(2, "chegg4", 2004, 'f');

       Student student5= new Student(1, "chegg5", 2001, 'm');

       Student student6 = new Student(2, "chegg6", 2002, 'f');

       Student student7= new Student(1, "chegg7", 2001, 'f');

      

       studentList.add(student1);

       studentList.add(student2);

       studentList.add(student3);

       studentList.add(student4);

       studentList.add(student5);

       studentList.add(student6);

       studentList.add(student7);

      

      

      

       ObjectOutputStream oos = null;

       FileOutputStream fout = null;

       try{

          fout = new FileOutputStream("student.ser", true);

          oos = new ObjectOutputStream(fout);

          oos.writeObject(studentList);

         

       } catch (Exception ex) {

          ex.printStackTrace();

       } finally {

          if(oos != null){

          oos.close();

          }

       }

      

      

      

       //1) printFile used to print all object inside the file student.ser

       printFile("student.ser");

       countRecords("student.ser");

   }

   private static void countRecords(String filename) throws FileNotFoundException {

      

       FileInputStream fis = new FileInputStream(filename);

       ArrayList<Object> objectsList = new ArrayList<Object>();

       boolean cont = true;

       try {

           @SuppressWarnings("resource")

           ObjectInputStream input = new ObjectInputStream(fis);

           while (cont) {

               Object obj = input.readObject();

               if (obj != null)

                   objectsList.add(obj);

               else

                   cont = false;

           }

       } catch (Exception e) {

       }

       System.out.println("No of object in "+filename + "is "+ objectsList.size());

  

      

   }

   private static void printFile(String filename) throws IOException {

       FileInputStream fis = new FileInputStream(filename);

       ArrayList<Object> objectsList = new ArrayList<Object>();

       boolean cont = true;

       try {

           @SuppressWarnings("resource")

           ObjectInputStream input = new ObjectInputStream(fis);

           while (cont) {

               Object obj = input.readObject();

               if (obj != null)

                   objectsList.add(obj);

               else

                   cont = false;

           }

       } catch (Exception e) {

       }

       for (Object obj : objectsList) {

           System.out.println("Object name: " + obj);

       }

   }

}