Class Alkio of my previous! question is chosen to do more methods.
Implement to the class in addition to the previous ones following
methods:public boolean suurempiArvo(Alkio verrattava)and
public int compareTo(Alkio verrattava)Method
suurempiArvoreturns true, if the value of the object is
greater than value of verrattava. MethodcompareTocompares the
values of elements and returns an integer( negative, if the value of
the object is less that value of verrattava, zero, is values are the
same and otherwise positive)
Questions.
Why those methods which are used has just one parameter although methods require values of two elements? If I refer in the methods to verrattava by writing verrattava, how do I refer to object?
When you have a method
You are comparing
otherwith the current instance. That is to say, the reason why you do not need the second element in the signature is that the second element is already there, it’s the object whose method is being invoked. You compare the members ofotherwith the members of the present instance, basicallythis, in order to arrive at the proper result.For clarification, if you were to invoke these methods, it would be something like
So you pass the
secondobject into thecompareTomethod offirst.firstthen compares itself against thesecond(thisvs.other).