tut06 - private Date whenBuilt public House(int id double...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
EE3206/EE5805 Java Programming & Application Tutorial 6 – Shallow Clone and Deep Clone 1. Given the class House , write a driver class TestHouse to create a House object and its clone by using the overloaded clone method. Show it is a shallow copy and explain why. 2. Modify the class House and write another cloning method to generate a deep copy. The method has the following prototype: public Object deepClone(); You have to rewrite your driver class and show the above method generates a deep copy from the invoking object. Discuss the differences between shallow clone and deep clone and explain which one is better in general. The class “House” import java.util.Date; public class House implements Cloneable, Comparable { private int id; private double area;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: private Date whenBuilt; public House(int id, double area) { this.id = id; this.area = area; whenBuilt = new Date(); } public double getId() { return id; } public double getArea() { return area; } public Date getWhenBuilt() { return whenBuilt; } public void setWhenBuilt(Date buildDate) { whenBuilt = buildDate; } /** Override the protected clone method defined in the Object class, and strengthen its accessibility */ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { return null; } } /** Implement the compareTo method defined in Comparable */ public int compareTo(Object o) { if (area > ((House)o).area) return 1; else if (area < ((House)o).area) return -1; else return 0; } }...
View Full Document

Page1 / 2

tut06 - private Date whenBuilt public House(int id double...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online