I have a project that I need to do very specifically and I need some help. I have looked basically everywhere for an answer and can't find it, not even on Stack Overflow. It has to do with cloning hashtables. (Both shallow and deep.)
I have pasted the code I have below, but in short, I have a class called EHashtable, which extends Hashtable. I then add some string keys and values of various custom class types. In the EHashtable class, there are methods called Clone() and dClone(). Clone() is supposed to create a shallow clone of its Hashtable - meaning the Hashtable is cloned but its values are not. dClone() is supposed to create a new Hashtable and clone each of the original hashtable's values to it (meaning each value points to different memory references than the first). It is also supposed to throw an exception if the custom made object is not cloneable.
Right now, even on a shallow clone (the Clone() method), changes one value in one Hashtable and will NOT change the value in the other. It seems as if each value is pointing to different references. I don't understand how to make the Clone() and dClone() methods do the two different things I want them to do. One other thing, the hashtable cannot have Generics. It needs to be Hashtable
and not Hashtable<K, V>
.
I have looked up how to for loop through a hashtable. That only works on an Object type, and Object types can't clone() due to the method's protected status. Below is my code, starting with the main method. I realize this is very specific, and all help is greatly appreciated.
import java.util.Hashtable;
import java.util.Iterator;
public class _Test {
public static void main(String[] arguments) {
Circle cr1 = new Circle(1);
Circle cr2 = new Circle(2);
Point po1 = new Point(10, 10);
Point po2 = new Point(20, 20);
PlaneCircle pcr1 = new PlaneCircle(po1, 11f);
PlaneCircle pcr2 = new PlaneCircle(po2, 12f);
EHashtable eh = new EHashtable(20);
eh.add(new String("C1"), cr1);
eh.add(new String("C2"), cr2);
eh.add(new String("PC1"), pcr1);
eh.add(new String("PC2"), pcr2);
try {
EHashtable ehCloned = (EHashtable) eh.Clone();
System.out.println("/***--Before Alteration--***/");
System.out.println("eh:");
System.out.println(eh);
System.out.println();
System.out.println("ehCloned:");
System.out.println(ehCloned);
System.out.println();
Circle cr3 = new Circle(99);
Point po3 = new Point(99, 99);
PlaneCircle pcr3 = new PlaneCircle(po3, 9999f);
eh.add(new String("C1"), cr3);
eh.add(new String("PC1"), pcr3);
System.out.println("/***--After Alteration--***/");
System.out.println("eh:");
System.out.println(eh);
System.out.println();
System.out.println("ehCloned:");
System.out.println(ehCloned);
System.out.println();
}
catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}
catch (ClassCastException e) {
System.out.println(e.toString());
}
catch (Exception e) {
System.out.println("\nError Message:" + e.getMessage());
}
}
}
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
public class EHashtable extends Hashtable {
public EHashtable() {
}
public EHashtable(int capacity) {
}
// Done
public boolean add(Object key, Object value) {
if (!(containsKey(key) && containsValue(value))) {
put(key, value);
return true;
}
else {
return false;
}
}
// Done
public void Clear() {
clear();
}
// Done
public Object Clone() throws CloneNotSupportedException {
EHashtable eh = (EHashtable) this.clone();
return eh;
}
public Object dClone() {
EHashtable eh = new EHashtable();
for (Object key : keySet())
eh.put(key, get(key));
return eh;
}
// Done
public boolean isNotEmpty() {
return !isEmpty();
}
// Done
public Iterator iterate() {
return entrySet().iterator();
}
}
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[x=" + x + ", y=" + y + "]";
}
}
public class PlaneCircle {
private Point p;
private float radius;
public PlaneCircle (Point p, float radius) {
this.p = p;
this.radius = radius;
}
public String toString() {
return "[p=" + p.toString() + ", radius=" + radius + "]";
}
}
public class Circle {
private float radius;
public Circle(float radius) {
this.radius = radius;
}
public String toString() {
return "[radius=" + radius + "]";
}
}