CS 100Lecture 201 Announcements P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday:

Slides:



Advertisements
Similar presentations
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Advertisements

Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Programming with Recursion
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Cosc236/recursion1 Recursion Recursive method calls itself Recursion frequently occurs in mathematics Recursive definition –2 parts base part (basis) recursive.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
CS 241 – Computer Programming II Lab Kalpa Gunaratna –
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
IT253: Computer Organization
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
1 CS February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.
CS 100Lecture 231 Announcements Check your grades on the door of 5141 Upson More review tomorrow Review session Sunday night w/Alan FINAL EXAM: Tuesday.
Program Development and Design Using C++, Third Edition
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
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.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Programming with Recursion
CSC 205 Programming II Lecture 8 Recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion (Continued)
Chapter Topics Chapter 16 discusses the following main topics:
Topic: Recursion – Part 2
Topic 6 Recursion.
Chapter 15 Recursion.
Computers as an Expressive Medium
Chapter 15 Recursion.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Java 4/4/2017 Recursion.
Recursion (Continued)
Recursion, Tail Recursion
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Introduction to C++ Recursion
Recursion (Continued)
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Recursion Chapter 11.
Recursion Data Structures.
Recursion (Continued)
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review
Yan Shi CS/SE 2630 Lecture Notes
CS148 Introduction to Programming II
Programming with Recursion
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion Method calling itself (circular definition)
The structure of programming
Main() { int fact; fact = Factorial(4); } main fact.
CS100A Lecture 25 1 December 1998 Chance to replace grade on Prelim 3, Question 2. Everyone is expected to be in lecture on Thursday, 3 December.
Thinking procedurally
Lecture 20 – Practice Exercises 4
Presentation transcript:

CS 100Lecture 201 Announcements P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday: review, review, review

CS 100Lecture 202 Today’s Topics Recursion Another sample class hierarchy Brainstorming on P5

CS 100Lecture 203 Recursion A method is recursive if it calls itself. Many algorithms are most easily written using recursive methods. (e.g., quicksort) To see that recursive method calls (calls of a method from within the body of a method) work, you have only to execute a method call of a recursive method yourself, using the rules that you already know. We’ll show that for a simple case in this lecture.

CS 100Lecture 204 Correctness of recursive methods However, to understand that a particular recursive method is correct, you should not think about executing it. Rather, do two things: Understand that each recursive method call is correct in terms of the specification of the method. Make sure that the recursion “terminates” (much like making sure that a loop terminates). That is, see to it that in some sense the arguments of the recursive call are “smaller” than the parameters of the method and that when the parameters are “as small as possible”, the method terminates without calling itself recursively. We’ll see this in the examples.

CS 100Lecture 205 Example: reverse string // Return the reverse of string s. For example, if s is // “abcd”, return the string “dcba”. public static String rev(String s) { // If the string is empty or contains one character, return it if (s.length( ) <= 1) return s; // The string has the form C c, where C is a character; // c is a string. C is s.charAt(0). c is s.substring(1). // Return the reverse of c catenated with C return rev(s.substring(1)) + s.charAt(0); }

CS 100Lecture 206 Comments on reverse string: Note that the argument to the recursive call rev(s.substring(1)) has one less character than parameter s. Therefore, the “depth” of recursion --the maximum number of recursive calls with frames that will exist at any time-- is the number of characters in s. Note that in the simple case (length 1), there is no recursive call All of this suggests that the recursion will terminate

CS 100Lecture 207 Frame trace Suppose rev called in main as: t = rev(“abc”); F0: first frame for rev. s “abc” Called from main, frame M0 M0: frame for main. t ____ Called from system F2: second frame for rev. s “bc” Called from rev inside rev, frame F1 F3: third frame for rev. s “c” Called from rev inside rev, frame F2 //Return the reverse of string s. public static String rev(String s) { if (s.length( ) <= 1) return s; return rev(s.substring(1)) + s.charAt(0); }

CS 100Lecture 208 Example 2: remove blanks // Return a copy of s, but with blanks removed public static String removeBlanks (String s) { if (s.length() == 0) return s; if (s.charAt(0) == ‘ ‘) return removeBlanks(s.substring(1)); // first character of s is not a blank. // Return first character followed by rest of s // but with blanks removed return s.charAt(0) + removeBlanks(s.substring(1)); }

CS 100Lecture 209 Example 3: duplicate chars // Return a copy of s but with each character repeated public static String dup(String s) { if s.length() == 0) return s; return s.charAt(0) + s.charAt(0) + dup(s.substring(1)); }

CS 100Lecture 2010 Example 4: factorial // Given n>=0, return !n = 1*2*3*…*n public static factorial(int n) { if (n<=1) return 1; return n * factorial(n-1); }

CS 100Lecture 2011 Another class hierarchy Problem context: data storage facilities in a computer system Suppose there are three: –cache (very fast, very small) –main memory (RAM), pretty fast, moderately sized –disk (hard drive), slow, very big Methods we’d like: –getSpeed, getSize, getCost –for hd: how much is swap space? for RAM: what is the page size? For cache: What’s the replacement policy?

CS 100Lecture 2012 Draw the Hierarchy

CS 100Lecture 2013 Superclass features What’s the constructor look like? What methods should be included in the superclass? What fields should be in the superclass? Do we want to define any abstract methods here?

CS 100Lecture 2014 Main memory subclass features What fields does this subclass have uniquely? What should the constructor look like? What methods does this subclass provide?

CS 100Lecture 2015 Disk subclass features What fields does this subclass have uniquely? What should the constructor look like? What methods does this subclass provide?

CS 100Lecture 2016 Cache subclass features What fields does this subclass have uniquely? What should the constructor look like? What methods does this subclass provide?

CS 100Lecture 2017 Brainstorming on P5_2 Some facts about U.S. politicians that should be represented in your system? Possible routines to write about this data? Possible interface options? What else?