Question: Program5Main.java: import java.util.GregorianCalendar; /** * Main program to test drive our date list objects and ensure that they are cloned or * copied properly! */

Program5Main.java:
import java.util.GregorianCalendar; /** * Main program to test drive our date list objects and ensure that they are cloned or * copied properly! */ public class Program5Main { /** * Create and test some objects from MyClass * @param args the command line arguments */ public static void main(String[] args) { MyDateList datelist1 = new MyDateList("Beth's Events "); GregorianCalendar aNewDate = new GregorianCalendar(); // let's start out with a new date object //STEP 1: Run this code segment first. What should the last 2 dates be? //Fix this in the MyDateList class method addAnEvent datelist1.addAnEvent(aNewDate); aNewDate = new GregorianCalendar(2023,1, 1); datelist1.addAnEvent(aNewDate); aNewDate.set(2021, 7, 4); datelist1.addAnEvent(aNewDate); System.out.println("My objects contain the following information -"); System.out.println(datelist1.toString()); /* END STEP 1 */ // STEP 2: // Implement the Cloneable Interface for the MyDateList Class // Use the following code to test and ensure that the two date lists are truly independent of each other /* MyDateList datelist2 = new MyDateList("Doug's Events "); GregorianCalendar d = new GregorianCalendar(2001, 6, 6); datelist2.addAnEvent(d); d = new GregorianCalendar(1999,2, 2); datelist2.addAnEvent(d); d.set(1998, 3, 1); datelist2.addAnEvent(d); d.set(1997, 5, 3); datelist2.addAnEvent(d); System.out.println("Doug's objects contain the following information -"); System.out.println(datelist2.toString()); MyDateList datelist3 = datelist2.clone(); // copy datelist2 datelist3.setDescription("Susan's Events "); datelist3.changeEventYear(0, 2011); // Change the 0 event's year to 2011 System.out.println("Susan's objects contain the following information -"); System.out.println(datelist3.toString()); */ /* END STEP 2 */
// STEP 3 // Repeat the same logic above, but instead of cloning datelist2 replace the statement with: // MyDateList datelist3 = new MyDateList(datelist2); // make a copy of datelist2 // STEP 4, turn in the entire project with the changes to this program and MyDateList.java } }
MyDateList.java
import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.Calendar; /** * TO BE SOLVED * MyDateList contains both mutable and immutable instance variables stored in a list * of dates. Imagine that this represents a list of events from a calendar app. * Practice writing proper clones and copy constructors for this object type. * @author mea0010 * */ public class MyDateList { ArrayList
public String toString() { String str = description + ": "; for (GregorianCalendar d: events) { str += d.get(Calendar.YEAR) +"-" + d.get(Calendar.MONTH) + "-" + d.get(Calendar.DAY_OF_MONTH) + " "; } return str; } }
Will surely give thumbs up!
This is an individual homework grade. The assignment is to add a clone operation and a copy constructor to a class that has mutable instance variables in it. MyDateList contains a description string and an arraylist of GregorianCalendar objects (mutable), to store dates in them. We will add dates to the list, we will find and change a date in the list to test that our deep copies are working properly. PROGRAM REQUIREMENTS I have posted a test program (Program5Main.java and MyDateList.java) along with this assignment file on canvas, in the Assignments Directory. Add clone and a copy constructor to MyDateList.java (don't forget to put your name in the comments). See the comments in Program5Main.java for the three steps you are to complete for this program. STEP 1: When we add date references to our internal ArrayList, those are still references to an outside object that may get changed. Add code to fix addAnEvent() to make this work properly. STEP 2: Add the cloneable interface and the clone() operation to MyDateList to make this work properly. STEP 3: Add a copy constructor to MyDateList to make this work properly
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
