Java Reference
In-Depth Information
This code compiles and runs without throwing an exception. However, the code contains a
defect that Challenge 18.5 brings out.
CHALLENGE 18.5
The following lines of code create a simulation object of a mixer in bay 1 and a copy
for bay 2:
package com.oozinoz.simulation;
import com.oozinoz.units.*;
public class ShowCloningProblem implements UnitConstants
{
public static void main(String[] args)
{
// Mixer 1 is in bay 1, at coordinate(15m, 25m)
Length x = (Length) METER.times(15);
Length y = (Length) METER.times(25);
Location loc = new Location(1, x, y);
// 10 hours mean-time-between-failure
MixerSimulator m1 =
new MixerSimulator(loc, (Time) HOUR.times(10));
MixerSimulator m2 = (MixerSimulator) m1.clone_1();
// Mixer 2 is in bay 2, at coordinate (20, 75)
m2.location.setBay(2);
m2.location.setCoordinates(
(Length) METER.times(20),
(Length) METER.times(75));
System.out.println(m1.location.bay);
}
}
Draw a diagram of the objects created by this code. Also, write down the results of
the println() statement.
You might want to make Location objects immutable, removing the class's set methods,
so that any number of clients could safely share a reference to the same location. At Oozinoz,
however, machine locations do occasionally change. Rather than making Location objects
immutable, you can fix the problem with MachineSimulator.clone() by making
Location objects cloneable.
CHALLENGE 18.6
Suppose that you change the Location class to declare that it implements
Cloneable and write a properly functioning clone() method for it. Now write
a revised version of the clone() method for the MachineSimulator class.
Search WWH ::




Custom Search