Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scoping and Recursion CSC 171 FALL 2004 LECTURE 12.

Similar presentations


Presentation on theme: "Scoping and Recursion CSC 171 FALL 2004 LECTURE 12."— Presentation transcript:

1 Scoping and Recursion CSC 171 FALL 2004 LECTURE 12

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

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

4 Using Javadoc Demo

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

6 Parameter Scoping public class myClass { int y = 0 ; public static void main(String args[]){ int x = myMethod1(5); y+=x; System.out.println(“x ==“ + x + “ y == “ + y); } public static int myMethod1(int x) { y+= x; return myMethod2(x) + 1; } public static int myMethod2(int x) {y+=x; return x + 1; } }

7 Recursion 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 Recursion is when we think of things in terms of itself.

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

9 A Theological Aside Disclaimer: you don’t have to believe Can computer science inform theology? Christian notion of “Trinity”? "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, co- eternal persons; one God in three persons." 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."

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

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

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

13 Weiss’s fundamental rules of recursion 1. 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.

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

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

16 Recursion vs. Iteration 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

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

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

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

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


Download ppt "Scoping and Recursion CSC 171 FALL 2004 LECTURE 12."

Similar presentations


Ads by Google