CSC 216/s08/feign ignorance: Difference between revisions
No edit summary |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 20: | Line 20: | ||
===The script=== | ===The script=== | ||
Initially, one student shall play the role of Person A, and should write their name and age upon Card A, and hold onto the card, as well as one of the cellphones. A second student shall hold the (null) card (to represent a non-existent Person). The third student shall be the Conductor and will hold the Person B card and the additional cellphone. | |||
'''Shallow Copy'''<br /> | |||
Begin by having the Conductor assist in carrying out a shallow copy clone() of Person A. | |||
#Ask Person A their name, age and to exhibit their cellphone. | |||
#Clone Person A by exchanging the Person B card with the (null) card held by the second student. | |||
#Person B should write their new name and age on the Person B card. | |||
#Ask Person A and Person B their name and age independently, and to exhibit their cellphone. (At this time the conductor should possess the (null) card and one of the cell phone, and both students should attempt to exhibit Person A's cellphone as their own). | |||
#What would happen if Person A changes the number associated with their cellphone? How does this affect Person B? | |||
[[Image:Shallow_copy.png|Lame art for demo of shallow copy.]] | [[Image:Shallow_copy.png|Lame art for demo of shallow copy.]] | ||
<br /> | |||
'''Deep Copy'''<br /> | |||
Begin this section by returning to the original configuration of cards and phones. The Conductor shall assist in carrying out a deep copy clone() of Person A. | |||
#Ask Person A their name, age and to exhibit their cellphone. | |||
#Clone Person A by exchanging the Person B card with the (null) card held by the second student, give the second cellphone to Person B. | |||
#Ask Person A and Person B their name and age independently, and to exhibit their cellphone. (At this time the conductor should possess only the (null) card, and each student should exhibit their own cellphone). | |||
#What would happen if Person A changes the number associated with their cellphone? How does this affect Person B? | |||
[[Image:Deep_copy.png|Lame art for demo of deep copy.]] | [[Image:Deep_copy.png|Lame art for demo of deep copy.]] | ||
Line 31: | Line 48: | ||
===Formatting Resources=== | ===Formatting Resources=== | ||
[http://meta.wikimedia.org/wiki/Help:Wikitext_examples Formatting Help Guide from MetaWiki] | [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Formatting Help Guide from MetaWiki] | ||
===Authors=== | |||
*Jason C Smith | |||
*Will Jessup | |||
*Reem Saffouri |
Latest revision as of 09:38, 27 March 2008
Cloning and the Object Superclass
A group exercise to help clarify the ambiguities related to cloning objects in Java.
The problem
Cloning objects in Java is not as straightforward as one might expect. Every object inherits a set of standard methods, including the clone() method from the Object superclass, but by default this method is protected. This prevents instances of classes outside of the object's package from cloning that object (unless it is a subclass of that object). By default, when a class implements the Cloneable interface, a call to the clone() method creates a shallow copy of that object, which allocates memory for a new instance and copies the memory allocated for the current object. This is ideal for simple objects whose local variables only include primitive types, but if an object includes local variables that are references to other objects, then a deep copy may be preferred. Choosing between implementing a deep copy or shallow copy implementation of the clone() method depends upon the goals of the project and the overall design.
Participants and props
3 Participants are required for this activity. A large class may be divided into groups of three, or three volunteers may demonstrate for the rest of the class.
- 1 Student shall act as the prototype Person A.
- 1 Student shall be (null) until they become Person B, a clone of Person A.
- 1 Student to direct the operation and to ask questions.
Materials required:
- 2 Cellphones (or other ubiquitous semi-identical objects)
- 3 Index cards, formatted as shown in the figure below:
The script
Initially, one student shall play the role of Person A, and should write their name and age upon Card A, and hold onto the card, as well as one of the cellphones. A second student shall hold the (null) card (to represent a non-existent Person). The third student shall be the Conductor and will hold the Person B card and the additional cellphone.
Shallow Copy
Begin by having the Conductor assist in carrying out a shallow copy clone() of Person A.
- Ask Person A their name, age and to exhibit their cellphone.
- Clone Person A by exchanging the Person B card with the (null) card held by the second student.
- Person B should write their new name and age on the Person B card.
- Ask Person A and Person B their name and age independently, and to exhibit their cellphone. (At this time the conductor should possess the (null) card and one of the cell phone, and both students should attempt to exhibit Person A's cellphone as their own).
- What would happen if Person A changes the number associated with their cellphone? How does this affect Person B?
Deep Copy
Begin this section by returning to the original configuration of cards and phones. The Conductor shall assist in carrying out a deep copy clone() of Person A.
- Ask Person A their name, age and to exhibit their cellphone.
- Clone Person A by exchanging the Person B card with the (null) card held by the second student, give the second cellphone to Person B.
- Ask Person A and Person B their name and age independently, and to exhibit their cellphone. (At this time the conductor should possess only the (null) card, and each student should exhibit their own cellphone).
- What would happen if Person A changes the number associated with their cellphone? How does this affect Person B?
References
- Learning Java - Chapter 8 : Java : Supplements
- You can insert Cloneability at any level of inheritance : Clone « Language Basics « Java
Formatting Resources
Formatting Help Guide from MetaWiki
Authors
- Jason C Smith
- Will Jessup
- Reem Saffouri