11/9: recursion, method overloading about scoping.java recursion method overloading

23
11/9: Recursion, Method Overloading • About Scoping.java • Recursion • Method overloading

Upload: lindsay-shaw

Post on 12-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

11/9: Recursion, Method Overloading

• About Scoping.java

• Recursion

• Method overloading

Page 2: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Scoping.java pt.1//Fig. 6.10: Scoping.java -- A scoping exampleimport java.awt.Container;import javax.swing.*;

public class Scoping extends JApplet { JTextArea outputArea; int x = 1; //note: an instance variable public void init () { outputArea = new JTextArea(); Container c = getContentPane(); c.add ( outputArea ); }

Page 3: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Scoping.java pt.2 public void start () { int x = 5; //note: a local variable with the same name. outputArea.append ( "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 outputArea.append ( "\n\nlocal x in start is " + x ); }

Page 4: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Scoping.java pt.3 public void methodA() { int x = 25; //initialized each time methodA is called. outputArea.append( "\n\nlocal x in methodA is " + x + " after entering methodA" ); ++x; outputArea.append( "\nlocal x in methodA is " + x + " before exiting methodA" ); }

Page 5: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Scoping.java pt.4 public void methodB() { outputArea.append ( "\n\ninstance variable x is " + x + " on entering methodB" ); x *= 10; outputArea.append ( "\ninstance variable x is " + x + " on exiting methodB" ); }}

Page 6: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Recursion

• Somewhat like iteration

• A recursive method calls itself.

• EX from math: factorial: n! = n * ( n - 1 ) !

• EX: finding a name in phone book– go to the middle of the phone book.– if name is before that page, go to middle of front half– if name is before that page, go to middle of front half

of front half– if name is before that page, go to middle of front half

of front half of front half...

Page 7: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Example: countZeros.java pt. 1//Counting zeros: a recursion exampleimport javax.swing.JOptionPane;public class CountZeros { public static void main ( String args [] ) { int number, zeros; number = Integer.parseInt ( JOptionPane.showInputDialog ( "Give me an integer. I'll count the zeros for you." ) ); zeros = count0s( number ); JOptionPane.showMessageDialog ( null , "The number " +

number + " has " + zeros + " zeros in it." ); System.exit ( 0 ); }

Page 8: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Example: countZeros.java pt. 2 public static int count0s ( int n ) //called by the method above { if ( n == 0 ) return 1;

else if ( n < 10 ) //and not zero, mind you return 0;

else if ( n % 10 == 0 ) //remainder of n/10 return ( count0s ( n / 10 ) + 1 );

else //n%10 is not = to zero return ( count0s ( n / 10 ) ); }}

Page 9: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Notes about Recursive Methods

• base case (or stopping case)– last one to be evaluated– must exist for recursion to end. Without it, the

recursion is endless (and the program won’t stop.)– EX: factorial: n! = n * ( n - 1 ) !– the base case would be 2 * 1– factorials, by definition, stop at 1: 0! = 1, 1! = 1

Page 10: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Memory Issue: Recursive Methods

• Java stores all values generated along the way:– Java calculating factorial of 6 :

6! = 6 * (6 - 1 )! = 720 = 5 * ( 5 - 1 )! = 120 = 4 * ( 4 - 1 )! = 24 = 3 * ( 3 - 1 )! = 6

= 2 * ( 2 - 1 )! = 2 = 1

• Java stores these values in a stack.

Page 11: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Recursive vs. Iterative Methods

• Any recursive method can be rewritten as an iterative method.

• Why use recursion, then?– recursion may mimic the problem at hand better.– iterative solutions may not be obvious.

Page 12: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

1st Program of the day

• pg. 239 FibonacciTest.java

Page 13: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

FibonacciTest: JTextField, JLabelimport java.awt.*;import java.awt.event.*;import javax.swing.*;

public class FibonacciTest extends JApplet implements ActionListener {

JLabel numLabel, resultLabel; JTextField num, result; public void init() { Container c = getContentPane(); c.setLayout ( new FlowLayout() );

Page 14: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

FibonacciTest: JTextField, JLabel numLabel = new JLabel ("Enter int. & press enter:"); c.add ( numLabel ); num = new JTextField ( 10 ); num.addActionListener( this ); c.add ( num ); resultLabel = new JLabel ( "Fibonacci value is:" ); c.add ( resultLabel ); result = new JTextField ( 10 ); result.setEditable ( false ); c.add ( result ); }

Page 15: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

JLabels & JTextFields

• JLabels: text labels intended for use with input fields.

• JTextFields: input fields that accept String-type values.

• have to be added to containers (Applet windows, frames, etc.): c.add ( numlabel );

Page 16: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

actionPerformed method num.addActionListener( this ); //from above

public void actionPerformed ( ActionEvent e ) { long number, fValue; number = Long.parseLong(num.getText()); showStatus ( "Calculating..." ); fValue = fibonacci ( number ); showStatus ( "Done." ); result.setText ( Long.toString ( fValue ) ); }

Page 17: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

JLabels & JTextFields

• JLabels: text labels intended for use with input fields.

• JTextFields: input fields that accept String-type values.

• have to be added to containers (Applet windows, frames, etc.): c.add ( numlabel );

Page 18: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

actionPerformed method• Used to respond to user’s actions ( pressing the enter

key, clicking a key on the keyboard, etc.)• Must have an actionListener added to the

JTextField, etc. to be used. (Have to know what to listen to for actions.)

• Note the parameter “ActionEvent e”. See similarity with parameter for paint method “Graphics g”.

Page 19: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

recursive Fibonacci method

• Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, …

• add the previous two numbers in the series to get next number.

• “the golden ratio”

• series describes a spiral of sorts.

number 0 1 2 3 4 5 6 7 8 9

fibonacci value

0 1 1 2 3 5 8 13 21 34

Page 20: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

recursive Fibonacci method

//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 ); }

number 0 1 2 3 4 5 6 7 8 9

fibonacci value 0 1 1 2 3 5 8 13 21 34

Page 21: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Method Overloading

• Naming two methods with the same name

• Signatures differ: methods have the same name, but the input types are different

• Examples are plentiful: – JOptionPane.showMessageDialog ( null , String );– JOptionPane.showMessageDialog ( null , String,

String, icon_type );

• Why do it?– creating certain types of output based on input types– allowing for different types of input to occur

Page 22: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

Method Overloading

public int root ( int a ){

return (int) Math.sqrt ( a );}

public double root ( double a ){

return Math.sqrt ( a );}

Page 23: 11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading

2nd Program of the Day

• MethodOverload.java – pg. 245

• After you get it to run, modify it to return an int-type value with a double-type input and a double-type value with an int-type input.

• Don’t forget: Quiz #4 (last one!) on Thursday.guaranteed programs: scope & recursion.