//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Example 3.7 on page 59
//  Correct cloning

class Widget implements Cloneable
{ int n;
  Widget w;
  Widget(int n) { this.n = n; }

  public Object clone() throws CloneNotSupportedException
  { Widget newWidget = new Widget(this.n);
    Widget y = newWidget;
    Widget t = this.w;
    while (t != null)
    { y.w = new Widget(t.n);
      y = y.w;
      t = t.w;
    }
    return newWidget;
  }

  public String toString()
  { return "(" + n + "," + w.n + ")";
  }
}

public class Ex0307
{ public static void main(String[] args)
        throws CloneNotSupportedException
  { Widget x = new Widget(44);
    x.w = new Widget(66);
    System.out.println("x = " + x);
    Widget y = (Widget)x.clone();
    System.out.println("y = " + y);
    x.n = 55;
    x.w.n = 77;
    System.out.println("x = " + x);
    System.out.println("y = " + y);
  }
}
