Programming Exercises



Imagine you are creating an application that has a number of different tools a user may invoke. These tools need a special context to work in. The context describes the current active selection in the application. The selection consists of a reference to an arbitrary object. We wish to create a class representing an editing context that the tools may use. This class should contain the aforementioned selection reference. We do not want to allow direct manipulation of the reference, but want to have methods in the context class that allow anyone to get and set the current selection.

Write such a class. Be sure to get the accessibility right.


A wide variety of tools can exist in an application, as described in Exercise 4.1. What they all have in common is that they can all be given an instance of an editing context, and that they are in either an active or an inactive state.

Write an interface that contains methods for giving the tool an editing context instance and querying the tool whether it is active or not.


Design a class for a bank database. The database should support the following operations:

  • depositing a certain amount into an account

  • withdrawing a certain amount from an account

  • looking up the current amount (i.e. the balance) in an account

  • transferring an amount from one account to another

The amount in the transactions is a value of type long. The accounts are identified by instances of the class Account that resides in a package called com.megabankcorp. records. The database object should reside in a package called com.megabankcorp. system.

The depositing, withdrawing, and balancing operations should not have any implementation, but allow subclasses to provide the implementation. The transferring operation should use the depositing and withdrawing operations to implement the transfer. It should not be possible to alter this operation in any subclass, and only classes within the package com.megabankcorp.system should be allowed to use this operation. The depositing and withdrawing operations should be available from anywhere. The balancing operation should only be available from subclasses and classes within the package com.megabankcorp.system.