Completion requirements
This chapter reviews method parameters and local variables, as well as method overloading and method signature.
Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters. When a method is called, the corresponding method is invoked by matching the arguments in the call to the parameter lists of the methods. The name together with the number and types of a method's parameter list is called the signature of a method. The return type itself is not part of the signature of a method.
15. Another Mystery
Answer:
Mystery sum: 40
Mystery sum: 20
Each object has its own instance variables, of course.
Another Mystery
Yet another mystery:
class Mystery
{
private int sum;
public Mystery( int x )
{
sum = x;
}
public void increment( int inc )
{
sum = sum + inc;
}
public void increase( int sum )
{
sum++ ;
}
public String toString()
{
return ("sum: " + sum );
}
}
public class Tester
{
public static void main ( String[] args)
{
Mystery mystA = new Mystery( 10 );
Mystery mystB = new Mystery( 20 );
mystA.increment( 5 );
mystB.increase( 3 );
System.out.println("mystA " + mystA + " mystB " + mystB);
}
}
Question 15:
Now what is printed? Beware: this is a trick question.