Exercises in this lecture  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Go to the slide, where this exercise belongs -- Keyboard shortcut: 'u'  

Exercise 4.4
Mutable and immutable Point objects with Move methods


On the slides about mutable and immutable structs we have illustrated two versions of struct Point with two different implementations of the Move method.

In the first struct, Move mutates the x and y coordinates and it returns this- the current object.

The second struct is immutable and Move returns a new instance of Point which is moved relative to the receiver of the Move message.

For both versions of Point we use the same client class, which sends Move messages to different points. In the lecture - and in the textbook - the differences between the first struct and the second struct has been explained relative to the client class. Be sure that you understand the difference!

Now, you are asked to implement version 1 and version 2 of Point from above with classes instead of structs. Compile and run each of them relative to the given client class. In each of the two cases explain the behavior of the Move methods in the classes.

In each of the two cases, explain how many instances of class Point there is created.


Solution