Help: Reference Semantics in Java


Help is available for each task, or you can go straight to the solution source code.

Task 1

Using the VisualAge Class Browser, fill in the code for the setProject method of the Employee class. This method sets the Employee's project to the given Project, and sets the Project's owner to be the Employee.
Remember, to add a method to the Employee class you need to open a Class Browser by selecting the Workspace->Open Class/Interface Browser... menu-item from the Workspace or Scrapbook windows. When asked to Select a class, simply type in Employee.

Before setting up the new relation, it must break any existing relationships: If the new Project already has an owner, it must set the owner's Project to null. In a similar fashion, if the Employee already has a Project, the Project's owner must be set to null.

Task 2

Augment the toString method of Employee to also print out the name of the current Project. This is a good place to use the ternary conditional operator ?: .
Since the project may be null, test for this before printing it out, then print either "null" or the name of the project.

Task 3

Augment the toString method of Project to print out its current owner.
Use the conditional operator ?: here as well. Print out the name of the owner if there is one.

Task 4

Run the program via the RefTest class, and note the output that appears in the Console window.
In the Workspace window, click on the RefTest package in the Magelang/JavaBasics package and then select the Selected->Run->Run main... menu-item and click the Run button when the dialog appears.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.