Exercise index of this lecture   Alphabetic index   Course home   

Exercises
Data Access, Properties, and Methods


5.1   A funny BankAccount  

In this exercises we provide a version of class BankAccount with a "funny version" of the Balance property. You should access the exercise via the web version, in order to get access to the source programs involved.

Study the Balance property of the funny version of class BankAccount.

Explain the behaviour of the given client of the funny BankAccount.

Next test-run the program and confirm your understanding of the two classes.

Please notice how difficult it is to follow the details when properties - like Balance in the given version of class BankAccount - do not pass data directly to and from the instance variables.

 

Solution


5.2   Point setters  

In the Point class on the accompanying slide we have shown how to program getter properties in the class Point. Extend the four properties with setters as well. The new version of this class will support mutable points.

Write a small client program that demonstrates the use of the setter properties.

Hint: Please be aware that the revised class should allow us to get and set the rectangular and polar coordinates (x, y, angle, radius) of a point independent of each other. You should first consider what it means to do so.

 

Solution


5.3   Passing references as ref parameters  

In the Date and Person classes of the corresponding slide we pass a reference as a value parameter to method DayDifference in class Date. Be sure to understand this. Read about ref parameters later in this lecture.

Assume in this exercise that the formal parameter other in Date.DayDifference is passed by reference (as a C# ref parameter). Similarly, the actual parameter dateOfBirth to DayDifference should (of course) be passed by reference (using the keyword ref in front of the actual parameter).

What will be the difference caused by this program modification.

Test-drive the program with a suitable client to verify your answer.

 

Solution


5.4   Passing struct values as ref parameters  

This exercise corresponds to the similar exercise on the previous slide.

In the Date struct and the Person class of this slide we pass a struct value as a value parameter to the method DayDifference.

Assume in this exercise that the formal parameter other in Date.DayDifference is passed by reference (a C# ref parameter). Similarly, the actual parameter dateOfBirth to DayDifference should (of course) be passed by reference (using the keyword ref in front of the actual parameter).

What will be the difference caused by this program modification. You should compare with the version on on the slide.

Test-drive the program with a suitable client to verify your answer.

 

Solution


5.5   Extending struct Double  

At the accompanying page we have seen how class Point can be extended with the instance method DistanceTo. This is an extension method.

If you study the method DistanceTo you will see that we use the squareroot function Math.Sqrt, defined statically in class Math. And we could/should have used a similar Square function had it been available.

It is remarkable that C# 3.0 allows us to extend the structs behind the primitive types, such as Double and Int32.

Write a static class that extends struct Double with the two extension methods Sqrt and Square. Afterwards, rewrite the method DistanceTo such that it makes use of the new instance methods in struct Double.

 

Solution


Generated: Monday February 7, 2011, 12:15:27