8/2: Recursion About Scoping.java Recursion Program of the Day.

Slides:



Advertisements
Similar presentations
8/7: Ch. 7: Arrays What is an array? Declaring & allocating arrays Program of the day.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition 5.3The for Repetition Structure 5.4Examples.
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
1 Storage Classes, Scope, and Recursion Lecture 6.
8/9: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
 2002 Prentice Hall. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods.
1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods 6.5 Method Definitions 6.6 Argument Promotion.
Chapter 5 Control Structures: Part II 1 3 Used when you know in advance how many times you want the loop to be executed. 4 Requirements: 1.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Chapter 6 - Methods Outline 6.1Introduction 6.3 Math Class Methods 6.4Methods 6.5Method Definitions 6.6Java API Packages 6.7Random Number Generation 6.8Example:
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1Introduction 6.2Program Modules in Java 6.3 Math Class Methods 6.4Methods.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
11/30: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays:
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
8/8: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
Introduction to Recursion
Recursion DRILL: Please take out your notes on Recursion
2.5 Another Java Application: Adding Integers
Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods 6.5 Method Definitions 6.6 Argument.
Recursion Recursive Thinking Recursive Programming
Java I.
Stacks.
Constructor Laboratory /12/4.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
The Basics of Recursion
Stacks.
CSC 143 Recursion.
A Methodical Approach to Methods
while loop Condition Statement list
JOptionPane class.
Chapter 5 – Control Structures: Part 2
Chapter 5 – Control Structures: Part 2
Presentation transcript:

8/2: Recursion About Scoping.java Recursion Program of the Day

Scoping.java pt.1 //Fig. 6.10: Scoping.java -- A scoping example import 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 ); }

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

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

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

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...

Example: countZeros.java pt. 1 //Counting zeros: a recursion example import 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 ); }

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

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

Memory Issue: Recursive Methods Java stores all values generated along the way: –Java calculating factorial of 6 : 6! = 6 * (6 - 1 )! = 720 = 5 * ( )! = 120 = 4 * ( )! = 24 = 3 * ( )! = 6 = 2 * ( )! = 2 = 1 Java stores these values in a stack.

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.

Program of the day pg. 239 FibonacciTest.java After you get it to work, modify the program to use an iterative approach rather than a recursive approach. Don’t forget: Quiz #4 (last one!) Tomorrow. guaranteed programs: scope & recursion.