This approach can be used to compare efficiency of an implementation.
enough
which is set to 5.
flip()
which flips the
sign of the data (for instance, if the data is 1, after the flip it
will become -1, and the other way around).
enough
. Otherwise the object prints a message "I am too
tired, no more flips", and returns without changing the data.
getRest()
. If this
method is called when the counter has reached enough
,
then the counter gets reset to 0 and the message gets printed "I
am ready for more flips". If the counter is less than
enough
, then the counter is not changed and the message
gets printed "I don't need any rest yet"
printData()
to print the
data.
SampleLazyObject.class
(you can download it):
public class TestLazy {
public static void main(String [] args) {
SampleLazyObject slo1 = new SampleLazyObject(3);
SampleLazyObject slo2 = new SampleLazyObject(2);
slo1.flip();
slo2.flip();
slo1.flip();
SampleLazyObject.getRest();
slo1.flip();
slo2.flip();
slo1.flip();
slo1.getRest();
slo2.flip();
slo1.printData();
slo2.printData();
}
}
You can use
it for testing by
replacing SampleLazyObject
by
LazyObject
. The behavior should not change.
A similar technique is used to create objects that can have only one instance (such as Java SecurityManager).
public class LabFiveProbThree {
public static void main(String [] args) {
int n = 1;
int m = 2;
SomeObject so1 = new SomeObject(1);
SomeObject so2 = new SomeObject(2);
tryInt(n,m);
System.out.println("main: n = " + n + " m = " + m);
System.out.println();
tryObjectOne(so1,so2);
System.out.println("main: so1 data = " + so1.getData() +
" so2 data = " + so2.getData());
System.out.println();
tryObjectTwo(so1,so2);
System.out.println("main: so1 data = " + so1.getData() +
" so2 data = " + so2.getData());
System.out.println();
}
public static void tryInt(int x, int y) {
System.out.println("tryInt before: x = " + x + " y = " + y);
// "switching" the values of x and y
int temp = x;
x = y;
y = temp;
System.out.println("tryInt after: x = " + x + " y = " + y);
}
public static void tryObjectOne(SomeObject s1, SomeObject s2) {
System.out.println("tryObjectOne before: s1 = " + s1.getData() +
" s2 = " + s2.getData());
// "switching" the values of s1 and s2
SomeObject temp = s1;
s1 = s2;
s2 = temp;
System.out.println("tryObjectOne after: s1 = " + s1.getData() +
" s2 = " + s2.getData());
}
public static void tryObjectTwo(SomeObject s1, SomeObject s2) {
System.out.println("tryObjectOne before: s1 = " + s1.getData() +
" s2 = " + s2.getData());
// "switching" the values of data fields of s1 and s2
int temp = s1.getData();
s1.setData(s2.getData());
s2.setData(temp);
System.out.println("tryObjectOne after: s1 = " + s1.getData() +
" s2 = " + s2.getData());
}
}
and the class SomeObject:
public class SomeObject {
private int data;
public SomeObject(int d) {
data = d;
}
public void setData(int d) {
data = d;
}
public int getData() {
return data;
}
}
Draw memory diagrams to explain how each of the methods works. What
would be printed in main after each of the methods returns? Run the
program to check your answers. Correct the diagram if needed.
public SomeObject
minimum(SomeObject s1, SomeObject s2)
that takes two SomeObject
objects and returns a reference to the one with the smaller
data. After the call to the method in main show that the returned
reference is to the existing object, and not to the new one (modify
it, and print the corresponding SomeObject to see that it got
modified).
copyMinimum
which is
similar to minimum, but instead of returning one of the existing
references, it creates a new SomeObject whose data field has the value
of the smaller data of the two. Write the code in main to show that
the new object is distinct from the two original ones.