Download presentation
Presentation is loading. Please wait.
1
Scoping and Recursion CSC 171 FALL 2001 LECTURE 8
2
History: Konrad Zuse 1935-1938 Konrad Zuse, Berlin, developed his Z-1 computer using relays After the war, he reconstructed his Z-4 machine at the University of Zurich Founded a computer company absorbed into Siemens Corporation.
3
Scope the life cycle of variables What is the output? And why? public class myClass { static int x = 4; public static void main(String args[]){ int x = 5; myMethod(); System.out.println(“x ==“ + x); } public static void myMethod() { x = 6; } }
4
Why? Two variables exist, both named “x” – Think of having two friends, both called “Joe” We distinguish Joe from Joe by use of a full name Joe Clarkson vs. Joe Jackson We can think of variables as having full names – one is named “myClass x” – One is named “main x” Consider the scope of the two variables
5
Scope What happens now? public class myClass { static int x = 4; public static void main(String args[]){ myMethod(); System.out.println(“x ==“ + x); } public static void myMethod() { x = 6; } }
6
Types of Scope Class scope – Methods and instance variables of a class have class scope – These things exist within the {} of the class Block Scope – Begins at identifiers declaration – Ends at } – Local variables and method parameters have block scope
7
Block Scope What happens? public class myClass { static int x; public static void main(String args[]){ int x = 5; {int y = 6;} System.out.println(“x ==“ + x + “ y == “ + y); }
8
Scope What happens now? public class myClass { static int x = 4; public static void main(String args[]){ myMethod(x); System.out.println(“x ==“ + x); } public static void myMethod(int x) { x = 6; } }
9
Common Error Will it compile? public class myClass { static int x = 4; public static void main(String args[]){ if (x < 5) { int y = 5; } else { int y = 6;} System.out.println(“x ==“ + x + “ y== “ + y); }
10
Common Error Does this fix it? public class myClass { static int x = 4; public static void main(String args[]){ int y; if (x < 5) { int y = 5; } else { int y = 6;} System.out.println(“x ==“ + x + “ y== “ + y); }
11
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; } }
12
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; } }
13
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); }
14
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
15
Examples of Euclid’s Alg GCD(206,40) ==GCD(6,4) ==GDC(4,2) ==GCD(2,0) ==2 GCD(154,35) ==GCD(35,14) ==GCD(14,7) ==GCD(7,0) ==7
16
In Class Excercise Using Euclids algorithm write: public int GCD(int a, int b) Partner up 10 min hand in on paper – two names
17
Iterative version public static int GCD1(int a, int b) { while (b != 0){ int temp = a%b; a = b; b = temp; } return a; }
18
Recursive version public static int GCD2(int a, int b) { if (b == 0 ) return a; else return GCD2(b,a%b); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.