(last updated: 16 April 2001)
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:
String toString()returns a reference to a string representation of this object's value (e.g., "42")void setValue(int val)sets this object's value to valint getValue()returns this object's integer valueObject clone() throws CloneNotSupportedException
returns a reference to a new MyInteger object with the same value as the original
boolean equals(MyInteger o)
returns true if this object and MyInteger object o contain the same value;
otherwise returns false
int hashCode()
for our purposes, it is sufficient for hashCode() to return this.getValue()
//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)