Menu

(Solved) : Part 1 Already Completed Codes Pasted Bottom Need Help Part 2 Persistency Please Update Up Q36642484 . . .

Part 1 is already completed(Codes are pasted at the bottom),need help on Part 2: Persistency please.
Update: Have updated Course and Student classes. The Course codesare pasted as image as it has exceeds the word limit.

1. create the following classes in your package (fields should be private): a. Course, with properties for number and name (b

Address.java

import java.io.Serializable;
import java.util.Objects;

public class Address implements Cloneable,Serializable {
       private String town = null;
       private String street = null;
       private String post_code =null;
       private int house_number;
      
       public Address(){}
      
       public Address(String town,Stringstreet,String post_code,int house_number) {
           this.town =town;
           this.street =street;
           this.post_code =post_code;
          this.house_number = house_number;
       }
      
       //getters and setters
       public String getTown() {
           returntown;
       }
       public void setTown(String town){
           this.town =town;
       }
      
      
       public String getStreet() {
           returnstreet;
       }
       public void setStreet(Stringstreet) {
           this.street =street;
       }
      

       public String getPost_code(){
           returnpost_code;
       }
       public void setPost_code(Stringpost_code) {
           this.post_code =post_code;
       }
      
      
       public int getHouse_number(){
           returnhouse_number;
       }
       public void setHouse_number(inthouse_number) {
          this.house_number = house_number;
       }
      
       @Override
       public Object clone() {
           Address addr =null;
           try{
              addr = (Address)super.clone();
              }catch(CloneNotSupportedException e) {
                  e.printStackTrace();
              }
       return addr;
       }

      
       //Override something

      
       @Override
        public boolean equals(Objecto) {
            if (o== this) return true;
            if(!(o instanceof Address)) {
              return false;
           }
           Address address = (Address) o;
           return house_number==address.house_number &&Objects.equals(town, address.town) &&Objects.equals(street, address.street)
                  &&Objects.equals(post_code, address.post_code);
        }
  
        @Override
        public int hashCode() {
           return Objects.hash(street,post_code,house_number,town);
        }
      
        @Override
        public StringtoString(){
           return town+””+street+” “+post_code+” “+house_number;
        }
}

————————————————-1 import java.io.SerializabLe; 2 import java.util.objects; 4 5 public class Course implements Cloneable, Serializable 6 priva

Student.java

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

public class Student implements Cloneable,Serializable {
       private String name = null;
       private String first_name =null;
       private String id = null;
       private Date dob = null;
       private Course course = null;
       private Address address =null;
      
       public Student(){      }
      
       public Student(String name,Stringfirst_name,String id,Date dob,Course course,Address address){
           this.name =name;
           this.first_name= first_name;
           this.id =id;
           this.dob =dob;
           this.course =course;
           this.address =address;
       }
      
       //getters and setters
       public String getName() {
           returnname;
       }
       public void setName(String name){
           this.name =name;
       }
      
      
       public String getFirst_name(){
           returnfirst_name;
       }
       public void setFirst_name(Stringfirst_name) {
           this.first_name= first_name;
       }
      
      
       public String getId() {
           return id;
       }
       public void setId(String id){
           this.id =id;
       }
      
      
       public Date getDob() {
           returndob;
       }
       public void setDob(Date dob){
           this.dob =dob;
       }
      
      
       public Course getCourse() {
           returncourse;
       }
       public void setCourse(Coursecourse) {
           this.course =course;
       }
      
      
       public Address getAddress() {
           returnaddress;
       }
       public void setAddress(Addressaddress) {
           this.address =address;
       }
      
       //Override something
       @Override
       public Object clone() {
           Student stu =null;
            try{
              stu = (Student)super.clone();  //shallow clone
              }catch(CloneNotSupportedException e) {
                 e.printStackTrace();
              }
               stu.address =(Address)address.clone();   //deep clone
               stu.dob =(Date)dob.clone();
               return stu;
           }
      
       @Override
        public boolean equals(Objecto) {
            if (o== this) return true;
            if(!(o instanceof Student)) {
              return false;
           }
           Student student = (Student) o;
           return Objects.equals(name, student.name) &&Objects.equals(first_name, student.first_name)
                  && Objects.equals(id,student.id) && Objects.equals(dob, student.dob)
                  &&Objects.equals(course, student.course) &&Objects.equals(address, student.address);
        }
  
        @Override
        public int hashCode() {
           return Objects.hash(name,first_name,id,dob,address,course);
        }
      
        @Override
        public String toString(){
           return name+””+id+” “+dob+” “+course+” “+address;
        }
      
}

1. create the following classes in your package (fields should be private): a. Course, with properties for number and name (both String) b. Address, with properties for town (String), street (String), post code (String) and house number (int) Student, with properties for surname, first name, id (all String), dob (for c. dateOfBirth”, of type java.time.LocalDate), course (of type Course) and address (of type Address) 2. Auto-generate getters, setters, equals and hashCode for all three classes. If two objects are equal, their hashCode should be the same check that the generated code fulfils this contract. Student should have a clone() method that is a combination of deep and shallow clone: deep clone should be used for the address, a shallow clone should be used for course 3. Note LocalDate is immutable like a String. You can clone the Student date using: clone.dob- this.dob If you later change the dob on the clone, the original Student will not have its dob changed Part 2 Persistency [3 marks] This section is focused on saving and loading the data we have so we can use it between runs of our programme Create a utility class StudentStorage with the following three static methods: 1. 2. 3. void void collection<Student> save (CollectionくStudent>, save (Collection<Student», File) String) throws IOException throws IOException IOException fetch (File) throws The save methods should save a collection of students to a binary file with the supplied name. Do not copy paste code between the two versions of save have one call the other The fetch method reads student data from the file and returns it (if you save students and then fetch them, you should get the original data back). The above methods should preserve referential integrity. For example if two students share a course (i.e. the courses are at the same memory address), when loading the students back from a file, they should still reference the same course object, not two separate course objects (double check by comparing the memory locations of the courses on the fetched students in your Part 3 tests). Hints Make your data classes implement Serializable (a tag interface you do not need to implement any methods, it’s just there as a marker). Check out the ObjectinputStream and ObjectOutputStream objects 1 import java.io.SerializabLe; 2 import java.util.objects; 4 5 public class Course implements Cloneable, Serializable 6 private String number = null; private String name null; public Course() public Course(String number, String name) f 8 10 12 13 // TODO Auto-generated constructor stub this.number number; this.name name; 15 16 //getters and setters public String getNumber) t return number; 18 19 20 21 public void setNumber(String number) this.number number; 23 public String getName () 25 26 return name; 27 public void setName (String name) 28 29 this.name name; 32 //Override something @Override 35 36 37 38 39 40 41 42 43 public boolean equals (Object o) if (o- this) return true; if ((o instanceof Course)) f return false; Course course = (Course) o; return Objects.equals (name, course.name) && Objects.equals(number, course.number); Override public int hashCode() 45 46 47 48 49 50 return Objects.hash(name, number); Override public String toStringO) return name+”+number; 52 53 Show transcribed image text

Expert Answer


Answer to Part 1 Already Completed Codes Pasted Bottom Need Help Part 2 Persistency Please Update Up Q36642484 . . .

OR