In: Computer Science
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.
/// Please write each class in a separate file :
// Class 1 : Student.java
package persistence1;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID =
-6123469366535229726L;
private String name;
private double gpa;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public Student() {
}
public Student(String name, double gpa) {
super();
this.name = name;
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 (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Student other = (Student)
obj;
if (Double.doubleToLongBits(gpa) !=
Double.doubleToLongBits(other.gpa))
return
false;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equals(other.name))
return
false;
return true;
}
@Override
public String toString() {
return "Student [name=" + name + ",
gpa=" + gpa + "]";
}
}
// Class 2: Course.Java
package persistence1;
import java.io.Serializable;
import java.util.ArrayList;
public class Course implements Serializable{
/**
*
*/
private static final long serialVersionUID =
-4616187168507101589L;
private String name;
private ArrayList<Student> arrStdList = new
ArrayList<>();
public Course() {
super();
}
public Course(String name, ArrayList<Student>
arrStdList) {
super();
this.name = name;
this.arrStdList = arrStdList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Student> getStdList() {
return arrStdList;
}
public void setStdList(ArrayList<Student>
arrStdList) {
this.arrStdList = arrStdList;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name ==
null) ? 0 : name.hashCode());
result = prime * result +
((arrStdList == null) ? 0 : arrStdList.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Course other = (Course) obj;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equals(other.name))
return
false;
if (arrStdList == null) {
if
(other.arrStdList != null)
return false;
} else if
(!arrStdList.equals(other.arrStdList))
return
false;
return true;
}
public void addStudent(Student s)
{
arrStdList.add(s);
}
@Override
public String toString() {
return "Course [name=" + name + ",
arrStdList=" + arrStdList + "]";
}
}
// Class 3 : StudentPersister.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;
public class StudentPersister {
static void save(ArrayList<Student> s)
{
try {
FileOutputStream
f = new FileOutputStream(new File("Student.txt"));
ObjectOutputStream o = new ObjectOutputStream(f);
// Write
objects to file
o.writeObject(s);
o.close();
f.close();
} catch (FileNotFoundException
e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
}
}
static ArrayList<Student> read(String loc)
{
ArrayList<Student> s1 =
null;
try
{
FileInputStream fi = new
FileInputStream(new File(loc));
ObjectInputStream oi = new
ObjectInputStream(fi);
// Read objects
s1 = (ArrayList<Student>)
oi.readObject();
oi.close();
fi.close();
} catch (FileNotFoundException
e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
} catch (ClassNotFoundException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
return s1;
}
}
// Class 4. CoursePersister.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;
public class CoursePersister {
static void save(ArrayList<Course> s)
{
try {
FileOutputStream
f = new FileOutputStream(new File("Course.txt"));
ObjectOutputStream o = new ObjectOutputStream(f);
// Write
objects to file
o.writeObject(s);
o.close();
f.close();
} catch (FileNotFoundException
e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
}
}
static ArrayList<Course> read(String loc)
{
ArrayList<Course> c1 =
null;
try
{
FileInputStream fi = new
FileInputStream(new File(loc));
ObjectInputStream oi = new
ObjectInputStream(fi);
// Read objects
c1 = (ArrayList<Course>)
oi.readObject();
oi.close();
fi.close();
} catch (FileNotFoundException
e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
} catch (ClassNotFoundException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
return c1;
}
}
// Class 5 : Junit Student class : StudentPersisterTest.java
package persistence1;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.BeforeClass;
import org.junit.Test;
public class StudentPersisterTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception
{
//StudentPersister sp = new
StudentPersister();
}
@Test
public void test() {
Student s = new
Student("Sourabh",8.2);
Student s2 = new
Student("Raja",7.9);
String loc="Student.txt";
ArrayList<Student> sList =
new ArrayList<>();
sList.add(s);
sList.add(s2);
StudentPersister.save(sList);
ArrayList<Student> sList2
= StudentPersister.read(loc);
System.out.println(sList2);
fail("Not yet implemented");
}
}
// Class 6 : junit class for Course ; CoursePersisterTest.java
package persistence1;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.BeforeClass;
import org.junit.Test;
public class CoursePersisterTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception
{
}
@Test
public void test() {
Student s1 = new
Student("Sourabh",8.2);
Student s2 = new
Student("Raja",7.9);
String loc="Course.txt";
Course c = new Course();
c.addStudent(s1);
c.addStudent(s2);
c.setName("maths");
Course c2 = new Course();
c2.addStudent(s1);
c2.addStudent(s2);
c2.setName("computer");
ArrayList<Course> cList = new
ArrayList<>();
cList.add(c);
cList.add(c2);
CoursePersister.save(cList);
ArrayList<Course> cList2 =
CoursePersister.read(loc);
System.out.println(cList2);
fail("Not yet implemented");
}
}
// Here we have implemented Serializable interface in both classes, so need to worry about NotSerializableExceptions.
// Out put :
/*
Output 1.
[Student [name=Sourabh, gpa=8.2], Student [name=Raja, gpa=7.9]]
Output 2.
[Course [name=maths, arrStdList=[Student [name=Sourabh,
gpa=8.2], Student [name=Raja, gpa=7.9]]], Course [name=computer,
arrStdList=[Student [name=Sourabh, gpa=8.2], Student [name=Raja,
gpa=7.9]]]]
*/