Step 1, 80 points: Create a Java application that meets the following specificat
ID: 3743564 • Letter: S
Question
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:
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<Student> students = new ArrayList<>();
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
public ArrayList<Student> getStudents() {
return students;
}
public void setName(String name) {
this.name = name;
}
public void setStudents(ArrayList<Student> 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<Course> {
// 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<Course> 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<Course> readListFromFile(File f) {
// creating List reference to hold courses after de-serialization
List<Course> 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<String>
courses = (ArrayList<Course>) 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<Student> {
// 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<Student> 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<Student> readListFromFile(File f) {
// creating List reference to hold students after de-serialization
List<Student> 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<String>
students = (ArrayList<Student>) 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<Student> students = new ArrayList<>();
ArrayList<Course> 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<Student> stus = (ArrayList<Student>) 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<Course> cours = (ArrayList<Course>) persistentCourse.readListFromFile(f);
assertEquals("Course list is not same", courses, cours);
assertEquals("Course list size is not same", courses.size(), cours.size());
}
}
Explanation / Answer
According to the requirements in the question, Step 2 proceeds with the following file changes in the provided files.
1.Student.java in package persistence2 is as follows
package persistence2;
import java.io.Serializable;
public class Student implements Serializable {
private String firstname;
private String lastname;
private double gpa;
public Student(String firstname,String lastname, double gpa) {
this.firstname = firstname;
this.lastname = lastname;
this.gpa = gpa;
}
public String getFirstName() {
return firstname;
}
public String getLastName() {
return lastname;
}
public double getGpa() {
return gpa;
}
public void setFirstName(String firstname) {
this.firstname = firstname;
}
public void setLastName(String lastname) {
this.lastname = lastname;
}
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 + ((firstname == null) ? 0 : firstname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student other = (Student) obj;
return (this.firstname.equals(other.firstname)&& this.lastname.equals(other.lastname) && this.gpa == other.gpa);
} else
return false;
}
@Override
public String toString() {
return "Student firstname=" + firstname + " lastname=" +lastname+", gpa=" + gpa;
}
}
2.Course.java in package persistence2
package persistence2;
import java.io.Serializable;
import java.util.ArrayList;
import persistence2.Student;
public class Course implements Serializable {
private String name;
private double meangpa;
private ArrayList<Student> students = new ArrayList<>();
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
public double getMeanGpa() {
return meangpa;
}
public ArrayList<Student> getStudents() {
return students;
}
public void setName(String name) {
this.name = name;
}
public void setStudents(ArrayList<Student> students) {
this.students = students;
}
public void setMeanGpa(double meangpa) {
this.meangpa = meangpa;
}
public void addStudent(Student stu) {
students.add(stu);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(meangpa);
result = prime * result + (int) (temp ^ (temp >>> 32)/students.size());
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 +"Mean GPA=" +meangpa;
}
}
3. JUNIT test file is as follows
package persistence2;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import persistence2.Student;
class PersisterTest {
Student st1 = new Student("Max","Well", 6.7);
Student st2 = new Student("Adam","Smith", 5.7);
Student st3 = new Student("Eve", "Butler",7.7);
Student st4 = new Student("Sam","Srini", 8.7);
Student st5 = new Student("Smith","Nack", 8.0);
Student st6 = new Student("John", "Schneider",9.1);
Student st7 = new Student("Eva","Watcher", 5.7);
Student st8 = new Student("Adel","Beckker", 8.7);
Student st9 = new Student("Sam","Govind", 6.7);
Student st10 = new Student("Ellen", "Srivatsav",6.2);
Course course1 = new Course("CSE01");
Course course2 = new Course("CSE02");
ArrayList<Student> students = new ArrayList<>();
ArrayList<Course> 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<Student> stus = (ArrayList<Student>) 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<Course> cours = (ArrayList<Course>) persistentCourse.readListFromFile(f);
assertEquals("Course list is not same", courses, cours);
assertEquals("Course list size is not same", courses.size(), cours.size());
}
@Test
void testCourseMeanGPAPersistence(){
persistentCourse.saveObjectToFile(f, course1);
Course c1 = persistentCourse.readObjectFromFile(f);
assertEquals("Course object is not same", course1, c1);
assertEquals("Course's Students object is not same", course1.getMeanGpa(), c1.getMeanGpa());
}
}
Step3: What is the need for Step2?
Answer:
=>Two students may have a similar name when only name field is specified as described in step1 of the question.
=>In order to test if the persister retrieves the correct student is made more accurate by splitting the
names into firstname and lastname.
=>No two students may have same firstname and lastname. Hence there will be no confusion while retreiving the studentinformation.
=>Secondly gpa of each student is calculated in the first step
=>Mean GPA of all students those belong to a course is calculated in the step2 which is needed to be tested to make sure persister
correctly retrieves those details as well.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.