Exercises on methods
// Fig. 6.10: [Link]
// A scoping example
import [Link];
import [Link].*;
public class Scoping extends JApplet {
JTextArea outputArea;
int x = 1; // instance variable
public void init()
{
outputArea = new JTextArea();
Container c = getContentPane();
[Link]( outputArea );
}
public void start()
{
int x = 5; // variable local to method start
[Link]( "local x in start is " + x );
methodA(); // methodA has automatic local x
methodB(); // methodB uses instance variable x
methodA(); // methodA reinitializes automatic local x
methodB(); // instance variable x retains its value
[Link]( "\n\nlocal x in start is " + x );
}
public void methodA()
{
int x = 25; // initialized each time a is called
[Link]( "\n\nlocal x in methodA is " + x +
" after entering methodA" );
++x;
[Link]( "\nlocal x in methodA is " + x +
" before exiting methodA" );
}
public void methodB()
{
[Link]( "\n\ninstance variable x is " + x +
" on entering methodB" );
x *= 10;
[Link]( "\ninstance variable x is " + x +
" on exiting methodB" );
}
}
<html>
<applet code="[Link]" width=280 height=260>
</applet>
</html>
1
// Fig. 6.12: [Link]
// Recursive factorial method
import [Link].*;
import [Link].*;
public class FactorialTest extends JApplet {
JTextArea outputArea;
public void init()
{
outputArea = new JTextArea();
Container c = getContentPane();
[Link]( outputArea );
// calculate the factorials of 0 through 10
for ( long i = 0; i <= 10; i++ )
[Link](
i + "! = " + factorial( i ) + "\n" );
}
// Recursive definition of method factorial
public long factorial( long number )
{
if ( number <= 1 ) // base case
return 1;
else
return number * factorial( number - 1 );
}
}
<html>
<applet code="[Link]" width=275 height=195>
</applet>
</html>
// Fig. 6.13: [Link]
// Recursive fibonacci method
import [Link].*;
import [Link].*;
import [Link].*;
public class FibonacciTest extends JApplet
implements ActionListener {
JLabel numLabel, resultLabel;
JTextField num, result;
public void init()
{
Container c = getContentPane();
[Link]( new FlowLayout() );
2
numLabel =
new JLabel( "Enter an integer and press Enter" );
[Link]( numLabel );
num = new JTextField( 10 );
[Link]( this );
[Link]( num );
resultLabel = new JLabel( "Fibonacci value is" );
[Link]( resultLabel );
result = new JTextField( 15 );
[Link]( false );
[Link]( result );
}
public void actionPerformed( ActionEvent e )
{
long number, fibonacciValue;
number = [Link]( [Link]() );
showStatus( "Calculating ..." );
fibonacciValue = fibonacci( number );
showStatus( "Done." );
[Link]( [Link]( fibonacciValue ) );
}
// Recursive definition of method fibonacci
public long fibonacci( long n )
{
if ( n == 0 || n == 1 ) // base case
return n;
else
return fibonacci( n - 1 ) + fibonacci( n - 2 );
}
}
<html>
<applet code="[Link]" width=325 height=75>
</applet>
</html>
// Fig. 6.16: [Link]
// Using overloaded methods
import [Link];
import [Link].*;
public class MethodOverload extends JApplet {
JTextArea outputArea;
public void init()
{
3
outputArea = new JTextArea( 2, 20 );
Container c = getContentPane();
[Link]( outputArea );
[Link](
"The square of integer 7 is " + square( 7 ) +
"\nThe square of double 7.5 is " + square( 7.5 ) );
}
public int square( int x )
{
return x * x;
}
public double square( double y )
{
return y * y;
}
}
<html>
<APPLET CODE = "[Link]" WIDTH = 275 HEIGHT = 40 >
</APPLET>
</html>