Question

In: Computer Science

I need to deep clone these 4 classes, using the cloneable interface. This is my code...

I need to deep clone these 4 classes, using the cloneable interface. This is my code so far. Is it correct or do I need to make any changes?

class Record implements Cloneable {

private String CNAME;
private ArrayList<Subject> eCores;
private Major eMajor;
private ArrayList<Subject> eElectives;
private int totalCredit;
private Status status;
private ArrayList<Student> students;

@Override
protected Record clone() throws CloneNotSupportedException {
Record record = (Record) super.clone();
record.eMajor = (Major) eMajor.clone();
eCores = new ArrayList<>();
for (Subject s : eCores) {
Subject s1 = (Subject) s.clone();
record.eCores.add(s1);
}
eElectives = new ArrayList<>();
for (Subject ss : eElectives) {
Subject ss1 = (Subject) ss.clone();
record.eElectives.add(ss1);
}
return record;
}

public class Subject implements Cloneable {

private String subjectName;
private String subjectCode;
private int subjectCreditPoint;
private ArrayList<Student> studentList;

@Override
protected Subject clone() throws CloneNotSupportedException {
return (Subject) super.clone();
}

abstract class Student implements Cloneable, Enrollment {

private String studentName;
private String studentDOB;
private String studentSex;//Student Gender
private int studentNumber;
private ArrayList<Record> record;

@Override
protected Student clone() throws CloneNotSupportedException {
Student student = (Student) super.clone();
record = new ArrayList<>();
for (Record r : record) {
Record r1 = (Record) r.clone();
student.record.add(r1);
}
return student;

}

class Major implements Cloneable {

private String mName;
private ArrayList<Subject> mCores;
@Override
protected Major clone() throws CloneNotSupportedException {
Major major = (Major) super.clone();
mCores = new ArrayList<>();
for (Subject s : mCores) {
Subject s1 = (Subject) s.clone();
major.mCores.add(s1);
}
return major;
}

Solutions

Expert Solution

/******************* Record.java *********************/

import java.util.ArrayList;

class Record implements Cloneable {

   private String CNAME;
   private ArrayList<Subject> eCores;
   private Major eMajor;
   private ArrayList<Subject> eElectives;
   private int totalCredit;
   private Status status;
   private ArrayList<Student> students;

   public Record() {

   }

   public Record(String cNAME, ArrayList<Subject> eCores, Major eMajor, ArrayList<Subject> eElectives, int totalCredit,
           Status status, ArrayList<Student> students) {
       super();
       CNAME = cNAME;
       this.eCores = eCores;
       this.eMajor = eMajor;
       this.eElectives = eElectives;
       this.totalCredit = totalCredit;
       this.status = status;
       this.students = students;
   }

   @Override
   public Record clone() {
       Record record = null;
       try {
           record = (Record) super.clone();
       } catch (CloneNotSupportedException cne) {
           record = new Record();
           record.CNAME = this.CNAME;
           record.eCores = new ArrayList<>();
           for (Subject s : this.eCores) {
               Subject s1 = s.clone();
               record.eCores.add(s1);
           }
           record.eMajor = this.eMajor.clone();
           record.eElectives = new ArrayList<>();
           for (Subject ss : this.eElectives) {
               Subject ss1 = ss.clone();
               record.eElectives.add(ss1);
           }
           record.status = this.status;
           record.students = new ArrayList<>();
           for (Student stud : this.students) {
               Student s = stud.clone();
               record.students.add(s);
           }
       }
       return record;
   }

}

/********************* Subject.java ****************/

import java.util.ArrayList;

public class Subject implements Cloneable {

   private String subjectName;
   private String subjectCode;
   private int subjectCreditPoint;
   private ArrayList<Student> studentList;

   @Override
   public Subject clone() {
       Subject subject = null;
       try {
           subject = (Subject) super.clone();
       } catch (CloneNotSupportedException nse) {
           subject.subjectName = this.subjectName;
           subject.subjectCode = this.subjectCode;
           subject.subjectCreditPoint = this.subjectCreditPoint;
           subject.studentList = new ArrayList<>();
           for (Student s : this.studentList) {
               Student stud = s.clone();
               subject.studentList.add(stud);
           }
       }
       return subject;
   }
}


/******************* Student.java *******************/

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

public abstract class Student implements Cloneable {

   private String studentName;
   private String studentDOB;
   private String studentSex;
   private int studentNumber;
   private ArrayList<Record> record;

   public Student() {

   }

   public Student(String studentName, String studentDOB, String studentSex, int studentNumber,
           ArrayList<Record> record) {
       super();
       this.studentName = studentName;
       this.studentDOB = studentDOB;
       this.studentSex = studentSex;
       this.studentNumber = studentNumber;
       this.record = record;
   }

   @Override
   public Student clone() {
       Student student = null;
       try {
           student = getClass().getDeclaredConstructor().newInstance();
       } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | SecurityException
               | NoSuchMethodException | InvocationTargetException e) {
           e.printStackTrace();
       }
       student.studentName = this.studentName;
       student.studentDOB = this.studentDOB;
       student.studentSex = this.studentSex;
       student.studentNumber = this.studentNumber;
       student.record = new ArrayList<>();
       for (Record r : this.record) {
           Record r1 = r.clone();
           student.record.add(r1);
       }
       return student;
   }
}

/***************   Major.java *******************/

import java.util.ArrayList;

public class Major implements Cloneable {

   private String mName;
   private ArrayList<Subject> mCores;

   @Override
   public Major clone() {
       Major major = null;
       try {
           major = (Major) super.clone();
       } catch (CloneNotSupportedException nse) {
           major.mName = this.mName;
           mCores = new ArrayList<>();
           for (Subject s : this.mCores) {
               Subject s1 = s.clone();
               major.mCores.add(s1);
           }
       }
       return major;
   }
}


Related Solutions

Need the slack clone by using react. Create slack clone by using ReactJS library. Use material...
Need the slack clone by using react. Create slack clone by using ReactJS library. Use material UI for Design improvements and Icons. Urgently required before evening... Don't give useless answers. I'll downvote if not according to expectations.
I needed the code for pong game (using classes) in pygame.
I needed the code for pong game (using classes) in pygame.
I have a problem with the code for my project. I need the two clients to...
I have a problem with the code for my project. I need the two clients to be able to receive the messages but the code I have done only the server receives the messages. I need the clients to be able to communicate with each other directly not throught the server. The requirements of this "local chat" program must meet are: 1. The chat is performed between 2 clients and a server. 2. The server will first start up and...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import java.util.Scanner; /** * * */ public class SoftOpening {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);    ArrayList foodList = generateMenu();    User user = generateUser(input); user.introduce();    userBuyFood(foodList, user, input); user.introduce(); } public static ArrayList generateMenu(){    ArrayList foodList = new ArrayList<>();    Food pizza1 =new Food(1,"pizza","Seafood",11,12); Food pizza2 =new Food(2,"pizza","Beef",9,10); Food Friedrice =new Food(3,"fried rice","Seafood",5,12);...
I need to send my students I am doing distant classes a test How do I...
I need to send my students I am doing distant classes a test How do I do this and have them answer it without the ability for the to keep it or print it out
i have attached my code here and we are supposed to create two classes. one is...
i have attached my code here and we are supposed to create two classes. one is date the other switches accounts for bank and then displays the bank account,type,name,date(pulled from first class and then prints out. i am having issues getting my code to pull from the first class and dont know how to make it read the data from date class if someone can look at my code and tell me how to fix this i would greatly appreciate...
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
Here is my java code. It works and has the correct output, but I need to...
Here is my java code. It works and has the correct output, but I need to add a file and I am not sure how. I cannot use the FileNotFoundException. Please help! import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,0,0}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: "...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface can be found below: List.java public interface List<T> extends Iterable<T> { /** * Insert an element at a specified location. * @param index * @param obj * @throws IndexOutOfBoundsException */ public void add(int index, T obj); /** * Append an object to the end of the list. * @param obj */ public boolean add(T obj); public void clear(); public boolean contains(T obj); /** *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT