Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
1
Chapter 6 – Methods
Part II
2003 Prentice Hall, Inc. All rights reserved.
2
Method
public void
init()
When the method is called and its purpose
This method is called once by the applet container when an applet is loaded for execution. It performs initialization of an
applet. Typical actions performed here are initializing fields, creating GUI components, loading sounds to play, loading images
to display (see Chapter 19, Multimedia) and creating threads (see Chapter 16, Multithreading).
public void
start()
This method is called after the init method completes execution. In addition, if the browser user visits another Web site and
later returns to the HTML page on which the applet resides, method start is called again. The method performs any tasks
that must be completed when the applet is loaded for the first time and that must be performed every time the HTML page on
which the applet resides is revisited. Typical actions performed here include starting an animation (see Chapter 19) and
starting other threads of execution (see Chapter 16).
public void
paint(
Graphics g )
This drawing method is called after the init method completes execution and the start method has started. It is also called
every time the applet needs to be repainted. For example, if the user covers the applet with another open window on the screen
and later uncovers the applet, the paint method is called. Typical actions performed here involve drawing with the
Graphics object g that is passed to the paint method by the applet container.
public void
stop()
This method is called when the applet should stop executing—normally, when the user of the browser leaves the HTML page
on which the applet resides. The method performs any tasks that are required to suspend the applet’s execution. Typical
actions performed here are to stop execution of animations and threads.
public void
destroy()
This method is called when the applet is being removed from memory—normally, when the user of the browser exits the
browsing session (i.e., closes all browser windows). The method performs any tasks that are required to destroy resources
allocated to the applet.
Fig. 6.11 JApplet methods that the applet container calls during an applet’s execution.
2003 Prentice Hall, Inc. All rights reserved.
3
6.16 Methods of Class JApplet
• Java API defines several JApplet methods
– Defining methods of Fig. 6.11 in a JApplet is called
overriding those methods.
2003 Prentice Hall, Inc. All rights reserved.
4
6.15 Method Overloading
• Several methods of the same name
• Different parameter set for each method
– Number of parameters
– Parameter types
2003 Prentice Hall, Inc. All rights reserved.
5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Outline
// Fig. 6.12: MethodOverload.java
// Using overloaded methods
import java.awt.Container;
MethodOverload.
java
import javax.swing.*;
public class MethodOverload extends JApplet {
// create GUI and call each square method
public void init()
{
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add( outputArea );
Lines 22-29
Method square
receives an int as an
argument
outputArea.setText( "The square of integer 7 is " + square( 7 ) +
"\nThe square of double 7.5 is " + square( 7.5 ) );
} // end method init
Method square receives an
// square method with int argument
int as an argument
public int square( int intValue )
{
System.out.println( "Called square with int argument: " +
intValue );
return intValue * intValue;
} // end method square with int argument
2003 Prentice Hall, Inc.
All rights reserved.
6
31
32
33
34
35
36
37
38
39
40
41
// square method with double argument
public double square( double doubleValue )
{
System.out.println( "Called square with double argument: " +
doubleValue );
Outline
MethodOverload.
java
return doubleValue * doubleValue;
} // end method square with double argument
} // end class MethodOverload
Lines 32-39
Overloaded method
square receives a
Overloaded method square
double as an
receives a double as an argument
argument
Called square with int argument: 7
Called square with double argument: 7.5
2003 Prentice Hall, Inc.
All rights reserved.
7
6.12 Recursion
• Recursive method
– Calls itself (directly or indirectly) through another method
– Method knows how to solve only a base case
– Method divides problem
• Base case
• Simpler problem
– Method now divides simpler problem until solvable
– Recursive call/step
2003 Prentice Hall, Inc. All rights reserved.
8
Final value = 120
5!
5!
5! = 5 * 24 = 120 is returned
5 * 4!
5 * 4!
4! = 4 * 6 = 24 is returned
4 * 3!
4 * 3!
3! = 3 * 2 = 6 is returned
3 * 2!
3 * 2!
2! = 2 * 1 = 2 is returned
2 * 1!
2 * 1!
1 returned
1
1
(a) Sequence of recursive calls.
(b) Values returned from each recursive call.
Fig. 6.14 Recursive evaluation of 5!.
Fig. 6.15: FactorialTest.java
Recursive factorial method.
2003 Prentice Hall, Inc. All rights reserved.
9
6.13 Example Using Recursion: The
Fibonacci Series
• Fibonacci series
– 0, 1, 1, 2, 3, 5, 8, 13, 21…
– fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(n) = fibonacci(n - 1) + fibonacci( n – 2 )
– fibonacci(0) and fibonacci(1) are base cases
– Each number in the series is sum of two previous numbers
2003 Prentice Hall, Inc. All rights reserved.
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Fig. 6.16: FibonacciTest.java
// Recursive fibonacci method.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Outline
FibonacciTest.j
ava
public class FibonacciTest extends JApplet implements ActionListener {
JLabel numberLabel, resultLabel;
JTextField numberField, resultField;
// set up applet’s GUI
public void init()
{
// obtain content pane and set its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );
// create numberLabel and attach it to content pane
numberLabel = new JLabel( "Enter an integer and press Enter" );
container.add( numberLabel );
// create numberField and attach it to content pane
numberField = new JTextField( 10 );
container.add( numberField );
// register this applet as numberField’s ActionListener
numberField.addActionListener( this );
2003 Prentice Hall, Inc.
All rights reserved.
11
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// create resultLabel and attach it to content pane
resultLabel = new JLabel( "Fibonacci value is" );
container.add( resultLabel );
// create numberField, make it uneditable
// and attach it to content pane
resultField = new JTextField( 15 );
resultField.setEditable( false );
container.add( resultField );
} // end method init
// obtain user input and call method fibonacci
public void actionPerformed( ActionEvent event )
{
long number, fibonacciValue;
// obtain user’s input and convert to long
number = Long.parseLong( numberField.getText() );
showStatus( "Calculating ..." );
Outline
FibonacciTest.j
ava
Line 43
Method
actionPerformed
is invoked when user
presses Enter
Method actionPerformed
is
Line 45
invoked when user
Enter
Wepresses
use long,
because Fibonacci
We use long, because
numbers become large
Fibonacci numbers
quickly
become large quickly
Lines 48-53
Pass user input to
method fibonacci
// calculate fibonacci value for number user input
fibonacciValue = fibonacci( number );
Pass user input to method fibonacci
// indicate processing complete and display result
showStatus( "Done." );
resultField.setText( Long.toString( fibonacciValue ) );
} // end method actionPerformed
2003 Prentice Hall, Inc.
All rights reserved.
12
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// recursive declaration of method fibonacci
public long fibonacci( long n )
{
// base case
if ( n == 0 || n == 1 )
return n;
Outline
Test for base case
(method fibonacci
can solve base case)
// recursive step
else
return fibonacci( n - 1 ) + fibonacci( n - 2 );
} // end method fibonacci
} // end class FibonacciTest
FibonacciTest.j
ava
Else return simpler problem
that
Lines 65-66
method fibonacciTest
might
for solve
base case
in next recursive
call fibonacci
(method
can solve base case)
Lines 69-70
Else return simpler
problem that method
fibonacci might
solve in next recursive
call
2003 Prentice Hall, Inc.
All rights reserved.
13
• C:\Documents and
Settings\USER\BookExamples2_11\ch06\fig06_1
6\FibonacciTest.html
2003 Prentice Hall, Inc. All rights reserved.
14
fibonacci( 3 )
return
return
fibonacci( 1 )
return 1
fibonacci( 2 )
+
+
fibonacci( 1 )
fibonacci( 0 )
return 0
Fig. 6.17 Set of recursive calls for fibonacci (3).
2003 Prentice Hall, Inc. All rights reserved.
return 1
15
6.14 Recursion vs. Iteration
• Iteration
–
–
–
–
Uses repetition structures (for, while or do…while)
Repetition through explicitly use of repetition structure
Terminates when loop-continuation condition fails
Controls repetition by using a counter
• Recursion
–
–
–
–
Uses selection structures (if, if…else or switch)
Repetition through repeated method calls
Terminates when base case is satisfied
Controls repetition by dividing problem into simpler one
2003 Prentice Hall, Inc. All rights reserved.
16
6.14 Recursion vs. Iteration (cont.)
• Recursion
–
–
–
–
More overhead than iteration
More memory intensive than iteration
Can also be solved iteratively
Often can be implemented with only a few lines of code
2003 Prentice Hall, Inc. All rights reserved.