Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Step 1, 80 points: Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these

Step 1, 80 points: 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.

PersisterImpl < T > (the extra spaces around the T just prevent your browser from parsing the type parameter as an HTML tag while you are reading this assignment-don't put them in your code) uses binary file I/O to save and retrieve objects of any Serializable type T and lists of objects of type T. PersisterImpl < T > must implement the following interface:

 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); } 

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 Persister. Test to make sure Persister correctly saves and retrieves Students, lists of Students, Courses, and lists of Courses. Make sure a Course's list of Students is correctly saved and retrieved. Test storage using one instance of 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.

Step 2, 15 points: Change the Student and Course classes and write new JUnit tests.

Copy the package persistence1 with all its contents to a new package, persistence2

Change the Student class in persistence2 so that it has two separate String variables for the student's first and last names

Change the Course class in persistence2 so that it contains a new method that calculates and returns the mean GPA of all the students in the class.

Change the JUnit tests in persistence2 appropriately.

Step 3, 5 points. Answer this question in comments in your code: why do you think I made you do Step 2?

I have done the step 1, anyone can help for the step2&3? Thank you.

code for step 1:

///////////////////////////////////////////////////Student.java////////////////////////////////////////////////////////

package persistence1;

import java.io.Serializable;

public class Student implements Serializable {

private String name;

private double gpa;

public Student(String name, double gpa) {

this.name = name;

this.gpa = gpa;

}

public String getName() {

return name;

}

public double getGpa() {

return gpa;

}

public void setName(String name) {

this.name = name;

}

public void setGpa(double gpa) {

this.gpa = gpa;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

long temp;

temp = Double.doubleToLongBits(gpa);

result = prime * result + (int) (temp ^ (temp >>> 32));

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (obj instanceof Student) {

Student other = (Student) obj;

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

} else

return false;

}

@Override

public String toString() {

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

}

}

////////////////////////////////////////////End Student.java/////////////////////////////////////

////////////////////////////////////////////////Course.java//////////////////////////////////////////////////////////////

package persistence1;

import java.io.Serializable;

import java.util.ArrayList;

public class Course implements Serializable {

private String name;

private ArrayList students = new ArrayList<>();

public Course(String name) {

this.name = name;

}

public String getName() {

return name;

}

public ArrayList getStudents() {

return students;

}

public void setName(String name) {

this.name = name;

}

public void setStudents(ArrayList students) {

this.students = students;

}

public void addStudent(Student stu) {

students.add(stu);

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((name == null) ? 0 : name.hashCode());

result = prime * result + ((students == null) ? 0 : students.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (obj instanceof Course) {

Course other = (Course) obj;

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

} else

return false;

}

@Override

public String toString() {

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

}

}

//////////////////////////////ENd Course.java//////////////////////////////////////////////////////

///////////////////////////////////////PersistentImplCourse.java//////////////////////////////////////////////////////////////////

package persistence1;

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.util.ArrayList;

import java.util.List;

public class PersisterImplCourse implements Persister {

// creating output stream variables

FileOutputStream fos = null;

ObjectOutputStream oos = null;

// creating input stream variables

FileInputStream fis = null;

ObjectInputStream ois = null;

@Override

public void saveObjectToFile(File f, Course ob) {

// for writing or saving binary data

try {

fos = new FileOutputStream(f);

// converting java-object to binary-format

oos = new ObjectOutputStream(fos);

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

oos.writeObject(ob);

oos.flush();

oos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public void saveListToFile(File f, List myList) {

try {

fos = new FileOutputStream(f);

// converting java-object to binary-format

oos = new ObjectOutputStream(fos);

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

oos.writeObject(myList);

oos.flush();

oos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public Course readObjectFromFile(File f) {

Course stu = null;

// reading binary data

try {

fis = new FileInputStream(f);

// converting binary-data to java-object

ois = new ObjectInputStream(fis);

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

stu = (Course) ois.readObject();

ois.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return stu;

}

@SuppressWarnings("unchecked")

@Override

public List readListFromFile(File f) {

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

List courses = null;

try {

// reading binary data

fis = new FileInputStream(f);

// converting binary-data to java-object

ois = new ObjectInputStream(fis);

// reading object's value and casting ArrayList

courses = (ArrayList) ois.readObject();

} catch (FileNotFoundException fnfex) {

fnfex.printStackTrace();

} catch (IOException ioex) {

ioex.printStackTrace();

} catch (ClassNotFoundException ccex) {

ccex.printStackTrace();

}

return courses;

}

}

/////////////////////////////////////ENd PersistentImplCourse.java//////////////////////////////////////////////////////

///////////////////////////////////////////////PersistentImplStudent.java//////////////////////////////////////////////

package persistence1;

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.util.ArrayList;

import java.util.List;

public class PersistentImplStudent implements Persister {

// creating output stream variables

FileOutputStream fos = null;

ObjectOutputStream oos = null;

// creating input stream variables

FileInputStream fis = null;

ObjectInputStream ois = null;

@Override

public void saveObjectToFile(File f, Student ob) {

// for writing or saving binary data

try {

fos = new FileOutputStream(f);

// converting java-object to binary-format

oos = new ObjectOutputStream(fos);

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

oos.writeObject(ob);

oos.flush();

oos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public void saveListToFile(File f, List myList) {

try {

fos = new FileOutputStream(f);

// converting java-object to binary-format

oos = new ObjectOutputStream(fos);

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

oos.writeObject(myList);

oos.flush();

oos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public Student readObjectFromFile(File f) {

Student stu = null;

// reading binary data

try {

fis = new FileInputStream(f);

// converting binary-data to java-object

ois = new ObjectInputStream(fis);

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

stu = (Student) ois.readObject();

ois.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return stu;

}

@SuppressWarnings("unchecked")

@Override

public List readListFromFile(File f) {

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

List students = null;

try {

// reading binary data

fis = new FileInputStream(f);

// converting binary-data to java-object

ois = new ObjectInputStream(fis);

// reading object's value and casting ArrayList

students = (ArrayList) ois.readObject();

} catch (FileNotFoundException fnfex) {

fnfex.printStackTrace();

} catch (IOException ioex) {

ioex.printStackTrace();

} catch (ClassNotFoundException ccex) {

ccex.printStackTrace();

}

return students;

}

}

////////////////////////////////////ENd PersistentImplStudent.java/////////////////////////////////////////////

////////////////////////////////////////////PersistentTester.java/////////////////////////////////////////////////

package persistence1;

import static org.junit.Assert.assertEquals;

import java.io.File;

import java.util.ArrayList;

import org.junit.Before;

import org.junit.jupiter.api.Test;

class PersisterTest {

Student st1 = new Student("Max", 6.7);

Student st2 = new Student("Adam", 5.7);

Student st3 = new Student("Eve", 7.7);

Student st4 = new Student("Sam", 8.7);

Student st5 = new Student("Smith", 8.0);

Student st6 = new Student("John", 9.1);

Student st7 = new Student("Eva", 5.7);

Student st8 = new Student("Adel", 8.7);

Student st9 = new Student("Sam", 6.7);

Student st10 = new Student("Ellen", 6.2);

Course course1 = new Course("CSE01");

Course course2 = new Course("CSE02");

ArrayList students = new ArrayList<>();

ArrayList courses = new ArrayList<>();

private PersistentImplStudent persistentStu = new PersistentImplStudent();

private PersisterImplCourse persistentCourse = new PersisterImplCourse();

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

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

@Before // setup()

public void before() throws Exception {

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

students.add(st1);

students.add(st2);

students.add(st3);

students.add(st4);

students.add(st5);

students.add(st6);

students.add(st7);

students.add(st8);

students.add(st9);

students.add(st10);

course1.addStudent(st1);

course1.addStudent(st2);

course1.addStudent(st3);

course1.addStudent(st4);

course1.addStudent(st5);

course2.addStudent(st6);

course2.addStudent(st7);

course2.addStudent(st8);

course2.addStudent(st9);

course2.addStudent(st10);

courses.add(course1);

courses.add(course2);

}

@Test

void testStudentPersistence() {

persistentStu.saveObjectToFile(f, st1);

Student student = persistentStu.readObjectFromFile(f);

assertEquals("Student object is not same", st1, student);

}

@Test

void testStudentListPersistence() {

persistentStu.saveListToFile(f, students);

ArrayList stus = (ArrayList) persistentStu.readListFromFile(f);

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

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

}

@Test

void testCoursePersistence() {

persistentCourse.saveObjectToFile(f, course1);

Course course = persistentCourse.readObjectFromFile(f);

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

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

}

@Test

void testCourseListPersistence() {

persistentCourse.saveListToFile(f, courses);

ArrayList cours = (ArrayList) persistentCourse.readListFromFile(f);

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

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

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Describe the two stages of activity-based costing.

Answered: 1 week ago

Question

Apply your own composing style to personalize your messages.

Answered: 1 week ago

Question

Format memos and e-mail properly.

Answered: 1 week ago