Question

In: Computer Science

IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods...

IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1.

  • Student has a String name, a double GPA, and a reasonable equals() method.
  • Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student.
  • StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister does the same for Courses and Lists of Courses. The save methods will need to deal with NotSerializableExceptions. Make sure that the read methods do not cause crashes if you attempt to read files that do not contain the anticipated objects.
  • Write a JUnit test case for each of the Persisters. Test to make sure StudentPersister correctly saves and retrieves Students and lists of Students, and that CoursePersister correctly saves and retrieves Courses and lists of Courses. Make sure a Course's list of Students is correctly saved and retrieved. Test storage using one instance of each Persister to save a file and a different instance to retrieve it, just as in a real-world application you need to save data, shut the application down, start it again, and load the file. The tests will be easiest if you make use of the .equals() methods in your classes to make sure the data you saved is the same as the data you retrieve. You do not need to provide a GUI for data entry, but you will need to write public getters and setters for the JUnit tests to use.

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

Code to copy:

Student.java:

package persistence;

import java.io.Serializable;

public class Student implements Serializable

{

     // Declaration of variable name

     private String name;

     // Declaration of variable gpa

     private double gpa;

     // constuctor of Student class

     public Student(String name, double gpa)

     {

          this.name = name;

          this.gpa = gpa;

     }

     // getName method to return name of the student

     public String getName()

     {

          return name;

     }

     // getGpa method for return gpa

     public double getGpa()

     {

          return gpa;

     }

     // setName method to set name of the student

     public void setName(String name)

     {

          this.name = name;

     }

     // setGpa method to set new gpa of the student

     public void setGpa(double gpa)

     {

          this.gpa = gpa;

     }

     @Override

     // equals method for comapre two objects

     public boolean equals(Object obj)

     {

          if (obj instanceof Student)

          {

              // convert object into student

              Student other = (Student) obj;

              // return true if two objects are same

              return (this.name.equals(other.name) && this.gpa == other.gpa);

          } else

              return false;

     }

     @Override

     // toString method

     public String toString()

     {

          // return student name and gpa as astring

          return "Student name=" + name + ", gpa=" + gpa;

     }

}

Course.java:

package persistence;

import java.io.Serializable;

import java.util.ArrayList;

public class Course implements Serializable

{

     private String courseName;

     private ArrayList<Student> students = new ArrayList<>();

     // constructor of course class

     public Course(String courseName)

     {

          this.courseName = courseName;

     }

     // method to get the name of the course

     public String getName()

     {

          return courseName;

     }

     // Method to get the students in that course

     public ArrayList<Student> getStudents()

     {

          return students;

     }

     // Method for set the new name of the course

     public void setName(String courseName)

     {

          this.courseName = courseName;

     }

     // Method to set the new students in course

     public void setStudents(ArrayList<Student> students)

     {

          this.students = students;

     }

     // Method to add the students in course

     public void addStudent(Student student)

     {

          students.add(student);

     }

     @Override

     public boolean equals(Object obj)

     {

          if (obj instanceof Course)

          {

              // convert object into course type

              Course other = (Course) obj;

              // return true if two objects are same

              return (this.courseName.equals(other.courseName)

                        && this.students.equals(other.students));

          }

          else

              return false;

     }

     @Override

     public String toString()

     {

// return course name and student list in the course

          return "Course Name=" + courseName + ", students=" + students;

     }

}

PersistentImp.java:

package persistence;

import java.io.File;

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.io.Serializable;

import java.util.List;

public class PersistentImp<T> implements Persister<T>, Serializable

{

     // Declare output stream variable

     ObjectOutputStream oos = null;

     // Declare input stream variable

     ObjectInputStream ois = null;

     @Override

     public void saveObjectToFile(File f, T ob)

     {

          // for writing or saving binary data

          try

          {

              // create file output stream object

              FileOutputStream fos = new FileOutputStream(f);

              // converting java-object to binary-format

              oos = new ObjectOutputStream(fos);

              // writing or saving object's value to stream

              oos.writeObject(ob);

              oos.flush();

              oos.close(); // closeing output stream

          }

          catch (FileNotFoundException e)

          {

              e.printStackTrace();

          }

          catch (IOException e)

          {

              e.printStackTrace();

          }

     }

     @Override

     public void saveListToFile(File f, List<T> myList)

     {

          try

          {

              FileOutputStream fos = new FileOutputStream(f);

              oos = new ObjectOutputStream(fos);

              // writing or saving student object's value to stream

              oos.writeObject(myList);

              oos.flush();

              oos.close();// closeing output stream

          }

          catch (FileNotFoundException e)

          {

              e.printStackTrace();

          }

          catch (IOException e)

          {

              e.printStackTrace();

          }

     }

     @Override

     public T readObjectFromFile(File f)

     {

          Object obj = null;

          // reading binary data

          try

          {

              FileInputStream fis = new FileInputStream(f);

              // converting binary-data to java-object

              ois = new ObjectInputStream(fis);

              // reading object's value and casting to student class

              obj = ois.readObject();

              ois.close();// closeing input stream

          }

          catch (FileNotFoundException e)

          {

              e.printStackTrace();

          }

          catch (IOException e)

          {

              e.printStackTrace();

          }

          catch (ClassNotFoundException e)

          {

              e.printStackTrace();

          }

          return (T) obj;

     }

     @SuppressWarnings("unchecked")

     @Override

     public List<T> readListFromFile(File f)

     {

          // creating List reference to hold students after de-serialization

          List<T> list = null;

          try

          {

              // reading binary data

              FileInputStream fis = new FileInputStream(f);

              // converting binary-data to java-object

              ois = new ObjectInputStream(fis);

              // reading object's value and casting ArrayList<String>

              list = (List<T>) ois.readObject();

          }

          catch (FileNotFoundException fnfex)

          {

              fnfex.printStackTrace();

          }

          catch (IOException ioex)

          {

              ioex.printStackTrace();

          }

          catch (ClassNotFoundException ccex)

          {

              ccex.printStackTrace();

          }

          return list;

     }

}

TestPersister.java:

package persistence;

     import static org.junit.Assert.*;

     import org.junit.Test;

     import java.io.File;

     import java.util.ArrayList;

     import org.junit.Before;

     public class TestPersister

     {

          //Declare list variables as student

          Student student[];

          //Declare list variables

          Course course1,     course2;

          //Declare list variables as student and course

          ArrayList<Student> students = new ArrayList<>();

          ArrayList<Course> courses = new ArrayList<>();   

          @Before

          public void before() throws Exception

          {

              //initialise students array

              student=new Student[10];

              //create 10 student object

              student[0] = new Student("Fax", 6.7);

              student[1]= new Student("Adam", 5.7);

              student[2] = new Student("Even", 7.7);

              student[3] = new Student("Samir", 8.7);

              student[4] = new Student("Jmith", 8.0);

              student[5] = new Student("JohnWesly", 9.1);

              student[6] = new Student("Jacob", 5.7);

              student[7] = new Student("Adel", 8.7);

              student[8] = new Student("Sam", 6.7);

              student[9] = new Student("Ellena", 6.2);

              //initialise objects of course

              course1 = new Course("CSE01");

              course2 = new Course("CSE02");

              //

              students = new ArrayList<>();

              courses = new ArrayList<>();

             

              System.out.println("Setting it up!");

              //add 10 students into student list

              for(int i=0;i<10;i++)

              students.add(student[i]);

              //add 5 students into course1     

              for(int i=0;i<5;i++)

              course1.addStudent(student[i]);

              //add 5 students into course1     

              for(int i=5;i<10;i++)

              course2.addStudent(student[i]);

              //add two coureses into coures list

              courses.add(course1);

              courses.add(course2);

          }

          @Test

          public void testStudentPersistence()

          {

              //Declare file object paramater with student.ser

              File serializableFile = new File("Student.ser");

              //Inialise obeject PersistentImp as student type

              PersistentImp<Student> obj=new PersistentImp<Student>();

              obj.saveObjectToFile(serializableFile, student[0]);

              Student restoredstudent =(Student) obj.readObjectFromFile(serializableFile);

              assertEquals(student[0], restoredstudent);

          }

          @Test

          public void testStudentListPersistence()

          {

              //Inialise obeject PersistentImp as student type

              PersistentImp<Student> obj=new PersistentImp<Student>();

             

              File serializableFile = new File("StudentList.ser");

              //save student data as binary formate in Serializable file

              obj.saveListToFile(serializableFile, students);

              //restore student data from Serializable file

              ArrayList<Student> stus = (ArrayList<Student>) obj.readListFromFile(serializableFile);

              assertEquals("Student list is not same", students, stus);

              assertEquals("Students list size is not same", students.size(), stus.size());

          }

          @Test

          public void testCoursePersistence()

          {

              //Inialise obeject PersistentImp as course type

              PersistentImp<Course> courseObj=new PersistentImp<Course>();

              File serializableFile = new File("Course.ser");

              //save course data as binary formate in Serializable file

              courseObj.saveObjectToFile(serializableFile, course1);

              //restore course data from Serializable file

              Course course= courseObj.readObjectFromFile(serializableFile);

              assertEquals("Course object is not same", course1, course);

              assertEquals("Course's Students object is not same", course1.getStudents(), course.getStudents());

          }

          @Test

          public void testCourseListPersistence()

          {

              //Inialise obeject PersistentImp as course type

              PersistentImp<Course> courseObj=new PersistentImp<Course>();

              File serializableFile = new File("CourseList.ser");

              //save course data as binary formate in Serializable file

              courseObj.saveListToFile(serializableFile, courses);

              //restore course data from Serializable file

              ArrayList<Course> cours = (ArrayList<Course>)courseObj.readListFromFile(serializableFile);

              assertEquals("Course list is not same", courses, cours);

              assertEquals("Course list size is not same", courses.size(), cours.size());

          }   

}

Persister.java:

package persistence;

import java.io.File;
import java.util.List;

public interface Persister < T > {
public void saveObjectToFile(File f, T ob);
public void saveListToFile(File f, List< T > myList);
public T readObjectFromFile(File f);
public List< T > readListFromFile(File f);

}

Kindly revert for any queries

Thanks.


Related Solutions

Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. Student has a String name, a double GPA, and a reasonable equals() method. Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister does...
IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods...
IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. Student has a String name, a double GPA, and a reasonable equals() method. Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. The student has a String name, a double GPA, and a reasonable equals() method. The course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses the binary file I/O to save and retrieve Students and Lists of...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. The student has a String name, a double GPA, and a reasonable equals() method. The course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses the binary file I/O to save and retrieve Students and Lists of...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
Write a program in Java and run it in BlueJ according to the following specifications: The...
Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line) and determines their type (excellent or ok). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "all" - prints all student records (first name, last name, grade, type). "excellent" - prints students with grade > 89. "ok"...
1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
XML and JAVA Write a Java program that meets these requirements. It is important you follow...
XML and JAVA Write a Java program that meets these requirements. It is important you follow these requirements closely. • Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1. • In the Java file, print a welcome message that includes your full name. • The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called "DoTheMath" that accepts as input three floating-point numbers x, y, and z (define them as double) and outputs several calculations: x to the power of y x to the power of (y to the power of z) The absolute value of x The square root of (x*y to the power of z). Sample Run: Enter the values for x, y, z: -3.7 -3 5...
Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a...
Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a title Ask the user to enter the following values: The type of plane(Boeing 747 or Airbus A300) The number of passengers on board the Plane The number of crew aboard the plane The maximum number of people that can be carried by one liferaft assuming all the liferafts on the plane are the same size The actual number of liferafts that are available on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT