Question

In: Computer Science

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 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.

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...
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...
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...
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 an application that uses a constructor and two different methods. JAVA
Create an application that uses a constructor and two different methods. JAVA
Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a...
Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a menu-driven program that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- square 2 -- circle 3 -- right triangle 4 -- quit If the user selects choice 1, the program should find the area of a square. If the user selects choice 2, the program should find the area of a circle. If the...
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"...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT