Scoping and Recursion CSC 171 FALL 2001 LECTURE 8.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Advertisements

Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Divisor máximo de dois inteiros. unsigned int gcd(unsigned int A, unsigned int B) { if (B > A) return gcd(B,A); else if (B==0) return A; else return gcd(B,A%B);}
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Scoping and Recursion CSC 171 FALL 2004 LECTURE 12.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Recursion!. Can a method call another method? YES.
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
JAVA JAVA is an object-oriented programming (OOP) language introduced by Sun Microsystems in In the Java programming language: A program is made.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
BUILDING JAVA PROGRAMS CHAPTER 1 ERRORS. 22 OBJECTIVES Recognize different errors that Java uses and how to fix them.
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Review of Recursion For those of you that have slept since.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
Take out a piece of paper and PEN. The quiz starts ONE minute after the tardy bell rings. You will have 45 – 90 seconds per question. Determine the output.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
RECURSION Go back, Jack, and do it again.. Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion:
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
Methods.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
Output Programs These slides will present a variety of small programs. Each program has a compound condition which uses the Boolean Logic that was introduced.
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.
Recursion occurs when a method calls itself. Google “recursion”
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
Lecture 7 Macro Review Stack Frames
Chapter 19: Recursion.
Recursion CSE 2320 – Algorithms and Data Structures
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Department of Computer Science
C Functions -Continue…-.
Recursion CSE 2320 – Algorithms and Data Structures
Building Java Programs
Using local variable without initialization is an error.
Advanced Programming in Java
CSC 253 Lecture 8.
Writing Methods.
CSC 253 Lecture 8.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
The Basics of Recursion
Recursive GCD Demo public class Euclid {
AKA the birth, life, and death of variables.
class PrintOnetoTen { public static void main(String args[]) {
Scope of variables class scopeofvars {
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Recursion Method calling itself (circular definition)
Class Example - Rationals
A Methodical Approach to Methods
CIS 110: Introduction to Computer Programming
Presentation transcript:

Scoping and Recursion CSC 171 FALL 2001 LECTURE 8

History: Konrad Zuse 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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