CSC 216/s08/feign ignorance: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
===Formatting Resources===
==Cloning and the Object Superclass==
[http://meta.wikimedia.org/wiki/Help:Wikitext_examples Formatting Help Guide from MetaWiki]
 
==Place Title of Exercise Here==


Give the title of your exercise, which may include the name of the topic you are covering, or some other catchy title.
A group exercise to help clarify the ambiguities related to cloning objects in Java.


===The problem===
===The problem===


Describe what you are attempting to teach students by this exercise.
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===
===Participants and props===


How many students will participate? What else do you need (e.g., old tennis ball, Powerpoint slides, software).
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:
[[Image:Name_cards.png|Prepared Index Cards]]


===The script===
===The script===


Describe how to do your exercise.
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.
[[Image:Clone_Example]]
 
'''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.]]
 
'''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.]]
 
===References===
*[http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Supplements/Chapter08/cloning.html Learning Java - Chapter 8 : Java : Supplements]
*[http://www.java2s.com/Code/Java/Language-Basics/YoucaninsertCloneabilityatanylevelofinheritance.htm You can insert Cloneability at any level of inheritance : Clone&nbsp;&laquo;&nbsp;Language Basics&nbsp;&laquo;&nbsp;Java]
 
===Formatting Resources===
[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:

Prepared Index Cards

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.

  1. Ask Person A their name, age and to exhibit their cellphone.
  2. Clone Person A by exchanging the Person B card with the (null) card held by the second student.
  3. Person B should write their new name and age on the Person B card.
  4. 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).
  5. What would happen if Person A changes the number associated with their cellphone? How does this affect Person B?

Lame art for demo of shallow copy.

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.

  1. Ask Person A their name, age and to exhibit their cellphone.
  2. 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.
  3. 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).
  4. What would happen if Person A changes the number associated with their cellphone? How does this affect Person B?

Lame art for demo of deep copy.

References

Formatting Resources

Formatting Help Guide from MetaWiki

Authors

  • Jason C Smith
  • Will Jessup
  • Reem Saffouri