Title: A Methodical Approach to Methods
1A Methodical Approach to Methods
Java's Work Horses
2Whats up?
- Automatic variables scope
- Recursion
- Applets again
3Scope Duration
- In life
- Scope is where you go
- Duration is how long you live
- In programming
- Scope is where a variable can be referenced
- Duration is how long the variable's value exists
4Automatic Means There When You Need Them
- Automatic variables
- Automatic duration automatically created
destroyed - Don't get intialized
- Automatic variables include
- Local variables declared (and defined) inside a
method - Parameter values passed to a method
5Initializing Variables
- Class instance variables are initialized
- public class Circle
- private int x_center, y_center, radius
- // Each of the above variables is now
- // set to 0, because all instance
- // variables are initialized, and
- // numeric variables are initialized
- // to 0.
-
6Initializing Auto Variables
- Automatic variables are not initialized
- public class Circle
-
- public double Area()
- double r_squared
- // Next line's no good, because we
- // haven't initialized r_squared
- // yet.
- return Math.PI r_squared
-
7Auto in Action
Suppose the circle class includes the following
method public double Area() double
r_squared r_squared radius
radius return Math.PI r_squared In the
course of a program, we call the area method on a
Circle object Circle testCircle new Circle(10,
10, 200) // r_squared (defined in Area())
doesn't exist testCircle.Area() // r_squared did
exist, but it's gone now
8The Rules of Scope
- Class scope
- Visible everywhere within a class
- class Test
- Test()
- k 2 // Notice that k hasn't appeared
-
- int j 1
- int i j
- int k // k is defined here, but its scope
- // is the entire class
9Block Scope
- Block scope
- Variables are visible everywhere within curly
braces and . - Includes
- Methods
- for loops
- Other blocks
10Hiding Variables
- What happens if a variable is declared in the
scope of another variable with the same name? - class Test
- static int x 1
- public static void main(String args)
- int x 0
-
- // What's the value of x here?
-
11Telling Them Apart
- When you run this code
- class Test
- static int x 1
- public static void main(String args)
- int x 0
- System.out.print("x" x)
- System.out.println(", Test.x" Test.x)
-
-
- You get
- x0, Test.x1
12Running Again with Recursion
- Doing the same thing over and over
- Looping
- Recursion functions that call themselves
- Recursion is like induction
- Example Factorial
- 5! 5 4 3 2 1
- is the same as
- 5! 5 4!
13The Factorial Example
- How do we calculate the factorial of n?
- Multiply n times the factorial of n-1
- n (n-1)!
- n (n-1) ((n-1) -1)!
- n (n-1) ((n-1) - 1) (((n-1) -1) - 1)!
- n (n-1) ((((((((n-1) -1) -1) -1)
- Now we have to translate this into Java
- Remember that n! n (n-1)!
- Special case when n 1
14Factorial Recursion
// Recursive definition of method
factorial public long factorial( long number )
if ( number lt 1 ) // base case return
1 else // n (n-1)!
return number factorial( number - 1 )
15Bottoming Out
// Recursive definition of method
factorial public long factorial( long number )
if ( number lt 1 ) // base case // 1!
1 (By definition, 0! 1) return 1 else
// n (n-1)! return number
factorial( number - 1 )
16Visual Café Example
- The Call window shows the call stack
- Call stack is a list of "active" methods
- As one method calls another, each method is
placed on the stack - For the factorial example, what do you expect to
see in the call stack?
17Why Choose Recursion?
- Recursion is very similar to iteration
- Recursive methods can be rewritten with iteration
- Recursion is a different way of looking at the
problem - Factorial recursion based on
- n! n (n-1)!
- Factorial iteration based on
- n! n (n-1) (n-2) ... 1
18Factorial Again
// Iterative definition public long factorialIter
( long number ) long factorial 1 if
(number 0) return 1 else
for(long tempnumber temp ! 1 temp--)
factorial temp return factorial
19Applets Revisited
- Applets aren't standalone applications
- Depend on somebody else to provide numerous
functions - Applet class has 21 methods, but 5 of them
control interaction between an applet and its
container
Applet
Browser
20The Fab Five Applet Methods I
- public void init()
- Gets things started
- Good place to set up the initial screen
appearance - Only gets called once
- public void paint(Graphics g)
- Draws stuff on the screen
- called every time the applet needs to be redrawn
21The Fab Five Applet Methods II
- Three methods used with threads
- public void start()
- Called after init()
- Called again every time the browser returns to
the HTML page - public void stop()
- Called every time the browser leaves the page
- public void destroy()