Assignment 3 (due 18 April)

(last updated:  16 April 2001)

In this assigment you will use JBuilder to write a Java application (not a JApplet!) that will consist of three classes:  two classes will implement the Cloneable interface and the third class (your application) will contain your main() function.  This program will read integers from a file into each of an array, and two Vector objects.  Since vectors must contain object references, we will wrap the vector integers in a class, MyInteger (defined below).

The array and two vectors will be cloned.  By default, when array and vector objects are cloned, they are shallow copied.  However, one of the vector objects will be an instance of class DeepCopyVector (defined below), an extension of class Vector that supports deep copying via the clone() method.  Note that classes MyInteger and DeepCopyVector both implement the Cloneable interface, and both override the clone() method to support a deep copy.

The main() method of the application will then write the contents of the array, the two vectors, and their respective clones -- before and after modifications -- out to a file for further study.  You should be able to tell by the observed behavior which clones were shallow versus deep copied.  Be careful, though!  Some of the cloning behavior may surprise you!

To be handed in:  a diskette labeled with your name, containing the subdirectories comprising your JBuilder project subtree (your .jpr project file should be in the root of the subtree) and printouts of your Java source files (*.java).  Please place your diskette and printouts in a large envelope also labeled with your name.

The project will consist of the following three classes:

               DeepCopyVector extends Vector implements Cloneable
               DeepCopyVector( )
                 constructs a new DeepCopyVector object by invoking the constructor of its superclass
                Sample invocation:
                    DeepCopyVector dcv = new DeepCopyVector( );
               public synchronized Object clone()
                    returns a reference to a new DeepCopyVector, containing references to objects that are deep
                    copies of the original vector's references to objects (recursive deep copying). Note:  this clone()
                    implementation overrides that of class Vector, and since Vector's clone() method treats (rather
                    than throws) the CloneNotSupportedException, this implementation must do its work within
                    a try-catch statement for this exception.

               CloneIOApplication              n/a              n/a              public static void main(String [] args) {

                    //array and vector variables
                   int[] intArray;           // original integer array
                    int[] intArrayClone;  // cloned integer array

                    Vector v;                // original vector
                    Vector vShallow;   // shallow copy of v

                    DeepCopyVector dcv;          // original deep copy vector
                    DeepCopyVector dcvDeep;  // deep copy of dcv

                    // Step 1.
                    // Read ten integers from file "FileIn.txt" into intArray, v, and dcv.
                    // You will need to create this file using Notepad.  Numbers should
                    //  span at least two lines, and be delimited by spaces.

                    // Step 2.
                    // Write the contents of intArray, v, and dcv (for Vectors, use Iterator)
                    //    to file "FileOut.txt".
                    // Be sure to print a header indicating it's the original array and vectors,
                    // and label the array and vectors accordingly.

                    // Step 3.
                    // Clone intArray, v, and dcv, creating intArrayClone, vShallow, and dcvDeep

                    // Step 4.
                    // Iterate through intArray, v, and dcv, multiplying each value by 2
                    // Iterate through intArrayClone, vShallow, and dcvDeep, multiplying each value by 3
                    // Note:  for the vectors, you must use an Iterator object
                    //           and the hasNext() and next() methods.  No excuses!

                    // Step 5.
                    // Write the contents of all arrays and vectors to file "FileOut.txt"
                    //     (similar to Step 2...)
                    // Thought questions:
                    //    Which originals and clones are multiplied by 2?  by 3?  by 6?
                    //    Multiplied by 6 indicates shallow copies

                    // Step 6.
                    // Compare the original array and vectors to their respective clones
                    //    using "==" , "equals()" , and "hashCode()" ,
                    // and write out messages to file "FileOut.txt" indicating what was
                    //    being compared and the outcome of each comparison.  e.g.,
                    //       "v == vShallow?  Yes/No"
                    //       "v.equals(vShallow)?  Yes/No"
                    //       "v.hashCode() == vShallow.hashCode()?  Yes/No"


(Look at the course home page for a sample application using file I/O)