4.1 Equality of value types and reference types
Take a close look at the following program which uses the == operator on integers.
Predict the result of the program. Compile and run the program, and explain the results. Were your predictions correct? |
4.2 Are playing cards and dice immutable?
Evaluate and discuss the classes Die and Card with respect to mutability. Make sure that you understand what mutability means relative to the concrete code. Explain it to your fellow programmers! More specific, can you argue for or against the claim that a Die instance/value should be mutable? And similarly, can you argue for or against the claim that a Card instance/value should be mutable? Why is it natural to use structs for immutable objects and classes for mutable objects? Please compare your findings with the remarks in 'the solution' when it is released. |
4.3 Privacy Leaks
The starting point of this exercise is the observations about privacy leaks on the accompanying slide. Make sure that you understand the problem. Test-drive the program (together with its dependent Person class and Date class) on your own computer. If you have not already done so, read the section about privacy leaks in the textbook! Find a good solution to the problem, program it, and test your solution. Discuss to which degree you will expect that this problem to occur in everyday programming situations. |
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. |
4.5 Pyramid BankAccounts
This exercise can be seen as a continuation of the bank account exercise in which we supplied a bank account with a backup account. The primary purpose of the exercises is to train recursive object-oriented programming. We organize bank accounts in a tree structure (called a pyramid) in which each bank account may have two sons which are also bank accounts. The money in the account are somehow distributed on the accounts of the pyramid. Program a version of class BankAccount with an owner, a balance, and possible references to two additional bank accounts: leftAccount and rightAccount. A skeleton of this class is provided together with a client program. You can assume that an account is either a leaf or the account has two sons.
|
Generated: Monday February 7, 2011, 12:14:35