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

/// 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]]]]
*/


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