Scoping and Recursion CSC 171 FALL 2004 LECTURE

  • Slides: 20
Download presentation
Scoping and Recursion CSC 171 FALL 2004 LECTURE 12

Scoping and Recursion CSC 171 FALL 2004 LECTURE 12

JAVADOC l Setting Documentation comments – Delimited by “/**” and “*/” in source l

JAVADOC l Setting Documentation comments – Delimited by “/**” and “*/” in source l You can comment – Classes – Methods – Fields l Each comment is placed immediately above the feature it documents

Tagged Documentation l @param l @return l @throws l @deprecated l @see l @author

Tagged Documentation l @param l @return l @throws l @deprecated l @see l @author l @version

Using Javadoc l Demo

Using Javadoc l Demo

Parameter Scoping public class my. Class { int x = 4; public static void

Parameter Scoping public class my. Class { int x = 4; public static void main(String args[]){ int x = my. Method 1(5); System. out. println(“x ==“ + x); } public static int my. Method 1(int x) { my. Method 2(x); return x + 1; } public static int my. Method 2(int x) { x = 7; } }

Parameter Scoping public class my. Class { int y = 0 ; public static

Parameter Scoping public class my. Class { int y = 0 ; public static void main(String args[]){ int x = my. Method 1(5); y+=x; System. out. println(“x ==“ + x + “ y == “ + y); } public static int my. Method 1(int x) { y+= x; return my. Method 2(x) + 1; } public static int my. Method 2(int x) {y+=x; return x + 1; }

Recursion l l l Methods can call other methods Methods can also call themselves

Recursion l l l Methods can call other methods Methods can also call themselves Consider the “factorial” method – – – 4! = 4*3*2*1 We can think of this as 4! = 4*3! 3! = 3*2! 2! = 2* 1! 1! = 1 l Recursion is when we think of things in terms of itself.

Recursion l We often think of recursion as a “programming technique” l Recursion is

Recursion l We often think of recursion as a “programming technique” l Recursion is also a design pattern for data structures l Recursion is a way to think about problem solving

A Theological Aside Disclaimer: you don’t have to believe l Can computer science inform

A Theological Aside Disclaimer: you don’t have to believe l Can computer science inform theology? l Christian notion of “Trinity”? l "A three-fold personality existing in one divine being or substance; the union in one God of Father, Son, and Holy Spirit as three infinite, co-equal, coeternal persons; one God in three persons. " l from about 400 AD to about 1800 AD, lots of people were put to death for refusing to believe in the idea of "one God in three persons. "

To this day l “Trinity is still largely incomprehensible to the mind of man.

To this day l “Trinity is still largely incomprehensible to the mind of man. ” – J. Hampton Keathley, III , Th. M l But is it? How can something be “ 3 in 1”? l RECURSION!

Sierpinski Triangle This design is called Sierpinski's Triangle, after the Polish mathematician Waclaw Sierpinski

Sierpinski Triangle This design is called Sierpinski's Triangle, after the Polish mathematician Waclaw Sierpinski who described it in 1916. Among these is its fractal or self-similar character. The large blue triangle consists of three smaller blue triangles, each of which itself consists of three smaller blue triangles, each of which. . . , a process of subdivision which could, with adequate screen resolution, be seen to continue indefinitely.

Serpinski Triangle What is a Serpinski Triangle? A Serpinski Triangle is a triangle made

Serpinski Triangle What is a Serpinski Triangle? A Serpinski Triangle is a triangle made up of 3 Serpinski triangles.

Weiss’s fundamental rules of recursion Base case: always have at least one case that

Weiss’s fundamental rules of recursion Base case: always have at least one case that can be solved without recursion 2. Make progress: Any recursive call must progress toward the base case 3. “You gotta believe”: Always assume that the recursive call works. (In proofs “BTIH”). 4. Compound Interest rule: Never duplicate work by solving the same instance of a problem in separate recursive calls. 1.

Example : Fibonacci Number public static long fib (int n) { if (n <=

Example : Fibonacci Number public static long fib (int n) { if (n <= 1) return n; else return fib (n-1) + fib(n-2); }

Recursion l Methods can call other methods l Methods can also call themselves public

Recursion l Methods can call other methods l Methods can also call themselves public long factorial(long number) { if (number <=1) return 1; else return number * factorial(number – 1); }

Recursion vs. Iteration l GCD – Euclid’s (300 B. C. ) Algorithm – Oldest

Recursion vs. Iteration l GCD – Euclid’s (300 B. C. ) Algorithm – Oldest known non-trivial algorithm – If r is the remainder when a is divided by b – Then the common divisors of a and b are the same as the common divisors of b and r

Examples of Euclid’s Alg l l l GCD(206, 40) ==GCD(40, 6) ==GCD(6, 4) ==GDC(4,

Examples of Euclid’s Alg l l l GCD(206, 40) ==GCD(40, 6) ==GCD(6, 4) ==GDC(4, 2) ==GCD(2, 0) ==2 l l l GCD(154, 35) ==GCD(35, 14) ==GCD(14, 7) ==GCD(7, 0) ==7

In Class Excercise l Using Euclids algorithm write: public int GCD(int a, int b)

In Class Excercise l Using Euclids algorithm write: public int GCD(int a, int b) l Partner up l 10 min l hand in on paper – two names

Iterative version public static int GCD 1(int a , int b) { while (b

Iterative version public static int GCD 1(int a , int b) { while (b != 0){ int temp = a%b; a = b; b = temp; } return a; }

Recursive version public static int GCD 2(int a , int b) { if (b

Recursive version public static int GCD 2(int a , int b) { if (b == 0 ) return a; else return GCD 2(b, a%b); }