In this lab, you will continue the implementation of the VeryLongInt class described in Chapter 6 of the text and expanded in Programming Exercise 6.5.
There is a neat way to design greater from less, and that neat way is to use the reserved word this. Recall that whenever a message is sent, the method invoked can affect the object sending the message. For example, suppose we start with:
VeryLongInt veryLong = new VeryLongInt("75");
If, subsequently, the following message is sent:
veryLong.fibonacci (10);
then the object (referenced by) veryLong is invoking its fibonacci method. The effect is that the VeryLongInt object (referenced by) veryLong has the value 55, the 10th fibonacci number. Similarly, suppose we have:
VeryLongInt sum = new VeryLongInt ("330");
VeryLongInt current = new VeryLongInt ("82");
sum.add (current);
then the VeryLongInt object referenced by sum has the value 412. How does the fibonacci method -- or, in general, any method -- actually affect the object that invokes the method? The answer lies in the reserved word this, which is a reference to the calling object. Whenever a message is sent, a reference to the calling object is stored in this. So this can be used within the method to refer to the calling object.
For example, in the constructor with a String parameter, there is a loop that includes the message
digits.add (c - LOWEST_DIGIT_CHAR);
An equivalent version of that message is
this.digits.add (c - LOWEST_DIGIT_CHAR);So within any method, whenever you see a field or method without a controlling reference, this is implicit, and whenever you see this, it constitutes a reference to the calling object.
But what does all this have to do with the methods greater and equals? Here's how to handle greater. Notice that the calling object is greater than otherVeryLong exactly when otherVeryLong is less than the calling object. So we can define greater in terms of less as follows:
public boolean greater (VeryLongInt otherVeryLong)
{
return otherVeryLong.less (this);
} // method greater