A navigation application implements the composition shown in this UML excerpt. A waypoint's x coordinate is measured in metres.
An additional implementation requirement says that each plan must store independent waypoint copies: callers must not be able to change stored coordinates through either the supplied data or returned data. This copying requirement is explicit; do not infer it from the composition diamond alone.
Review this candidate implementation. Assume every supplied array is non-null, nonempty, and contains only non-null waypoints.
class Waypoint { int x; Waypoint(int x) { this.x = x; } } class RoutePlan { private Waypoint[] waypoints; RoutePlan(Waypoint[] input) { waypoints = input.clone(); } Waypoint[] copyWaypoints() { return waypoints.clone(); } }
A test method in the same package executes these statements in order:
Waypoint original = new Waypoint(5); Waypoint[] supplied = { original }; RoutePlan plan = new RoutePlan(supplied); original.x = 9; Waypoint[] returned = plan.copyWaypoints(); returned[0].x = 12; returned[0] = new Waypoint(25);
Complete the table for the state immediately after the last statement. Rows labelled stored refer to plan's private array, inspected with a debugger. Enter exact integers; coordinate rows use metres.
Unlock full course notes by signing up
Count links rather than objects
A contact-management application implements the association below using one private Contact[] array in each Directory. There are seven directory objects. Every array contains exactly three non-null references to distinct contacts within that directory. Different directories may share contact objects.
How many contact-reference entries are stored across the seven arrays? Count entries in these arrays only. Give an exact whole number of entries, to zero decimal places.
Test association constraints
A warehouse diagram gives each Batch between one and four associated Item objects. Its Java constructor receives an array and is intended to enforce three rules: the array length is from one to four inclusive, every entry is non-null, and no object reference appears twice.
The labels A, B, C, D, and E denote distinct non-null item objects. Each row is an independent constructor call using the listed entries in a new array. All three rules must hold. Enter the exact word accept or reject for each call.
Trace class-scoped and object-scoped fields
A venue implements the following UML class excerpt using standard attribute-scope notation. Translate the field declarations from the diagram; those declarations are deliberately omitted from the Java fragment.
The constructor is:
In a fresh, single-threaded run, the class is initialized once and these are the only constructor calls:
Preserve both ends during a transfer
A staffing model permits an employee to belong to zero or one department and each department to contain zero to four employees. Java stores both an employee's department reference and each department's employee collection. They must agree after every public method returns or throws an exception.
All relationship fields are private, and transfers use this public operation:
addInternal throws before changing its collection if the destination is full. Assume execution uses one thread. Internal removal and insertion can be made non-throwing once their conditions have been checked. A rejected transfer must preserve the original links.