|
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||
A mutable number.
[Note on "compatibility": (You may skip this section on first reading.)
Given the level of abstraction enforced by
the MyNumber interface, is it reasonable to expect
a class's MyNumber-accepting methods to be able to handle
arbitrary MyNumbers?
For example...
MyNumber m = new MyInteger(2); MyNumber n = new MyObscureInteger(3); m.plus(n); // ...what should happen?Doesn't it seem a bit unfair to expect the author
MyInteger to know how to deal with
MyObscureInteger? [Especially since the latter class might
not have even existed when MyInteger was written.]
To give MyInteger's author a way out of this fix, we introduce
a notion of "compatibility" between MyNumber classes...
Suppose that N1 and N2 are two
classes that both implement MyNumber. If N1's
MyNumber-accepting methods can always handle instances of
N2, and likewise if N2's
MyNumber-accepting methods can always handle instances of
N1, then N1 and N2 are said to be
"compatible."
Also, as you might expect, if n1 is an instance of class N1
and n2 is an instance of class N2, then
"n1 is compatible with n2" is equivalent to
saying "N1 is compatible with N2."
At a minimum, a class N that implements MyNumber
must be compatible with itself. In fact, N is typically
the only class that is compatible with N.
Thus, the author of MyInteger can simply proclaim,
"MyInteger is compatible only with itself!" He is
then free to ignore MyObscureInteger, since the behavior
of the m.plus(n) line above is officially undefined...]
[If Java supported a ThisType mechanism, we could cleanly sidestep
the entire "compatibility" morass. With ThisType at our disposal,
we could simply say...
public void plus(ThisType n); public ThisType squareOf(); // etc....since
ThisType essentially means "the same class as
this."]
[Note that the compatibility issue is not peculiar to MyNumber;
the same basic problem surfaces in the standard
java.lang.Comparable interface, for example.]
[What methods might you add to MyNumber, if you were
interested in requiring all MyNumbers to
be compatible?]
| Method Summary | |
java.lang.Object |
clone()
Returns a new MyNumber object that initially
equals this. |
MyNumber |
createCompatible(int i)
Returns a new MyNumber object that initially holds
the value i. |
boolean |
equals(java.lang.Object o)
Tests whether o and this are equal. |
boolean |
lessThan(MyNumber n)
Returns true if this is smaller than
n. |
void |
minus()
Replaces this with -this. |
void |
plus(MyNumber n)
Replaces this with this + n. |
MyNumber |
squareOf()
Returns a new object holding the square of the value of this. |
void |
times(MyNumber n)
Replaces this with this * n. |
java.lang.String |
toString()
Retrieves the decimal representation of the stored integer. |
| Method Detail |
public boolean lessThan(MyNumber n)
true if this is smaller than
n.
[The behavior of the lessThan method is undefined if
n is not compatible with this.]
n - the number to compare against thistrue if this is less than
n; false otherwisepublic void plus(MyNumber n)
this with this + n.
[The behavior of the plus method is undefined if
n is not compatible with this.]
n - the number to be added to thispublic void times(MyNumber n)
this with this * n.
[The behavior of the times method is undefined if
n is not compatible with this.]
n - the number by which this
is to be scaledpublic void minus()
this with -this.public MyNumber squareOf()
this.
[The returned object is guaranteed to be an instance of
this's class.]
MyNumber object (of the same class
as this) that initially stores the
square of this object's integer value.public MyNumber createCompatible(int i)
MyNumber object that initially holds
the value i.
[The returned object is guaranteed to be an instance of
this's class.]
Example:
// construct n... MyNumber n = new MyInteger(3); // increment n... MyNumber one = n.createCompatible(1); // ...one is a MyInteger. n.plus(one);[The behavior of the
createCompatible method is
undefined if i is too large (or too small) to be
handled by this's variety of number.]MyNumber (of the same class as
this) that takes i as its
initial stored valuepublic java.lang.Object clone()
MyNumber object that initially
equals this.
[The returned object is guaranteed to be an instance of
this's class.]
MyNumber (of the same class as
this) that takes this's
integer value as its initial stored valuepublic boolean equals(java.lang.Object o)
o and this are equal.o - the object to compare against thistrue if (1) o is a MyNumber
that is compatible with this and (2) o
and this currently store the same integer value;
false otherwisepublic java.lang.String toString()
String that represents the stored integer,
in decimal
|
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||