Reference Semantics in Java


Prerequisites

This Magercise demonstrates the semantics of assignment and reference in Java by building a one-to-one relationship between two objects.

The starter classes Employee and Project have already been created for you in the VisualAge Workspace and are present in the Reference Semantics in Java project. Additionally, they already have the necessary pointers needed to refer to each other. What needs to be filled in is the setProject method of Employee, which sets up the relationship. In addition, this method must maintain referential integrity. This means that a Project cannot be in relation to an Employee unless that Employee is in relation to it, and vice versa. Here it is legal for the relationship to be null valued.

The semantics of Java are discussed in detail in the course notes.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.Reference Semantics in Java.

If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name.

Tasks

Perform the following tasks:

  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.

  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 ?: .

  3. Augment the toString method of Project to print out its current owner.

  4. Run the program via the RefTest class, and note the output that appears in the Console window.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

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