Building Java Programs

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
When confronted with a new problem there are two questions you should ask: 1. Is this problem like, or a special case of, a problem that I already know.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 12 Lecture 12-2: recursive programming reading:
Building Java Programs Chapter 13 Searching reading: 13.3.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 367 Introduction to Data Structures Lecture 6.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Recursion Copyright (c) Pearson All rights reserved.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion. 2 recursion: The definition of an operation in terms of itself. –Solving a problem using recursion depends on solving smaller occurrences of.
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.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Chapter 18 Recursion CS1: Java Programming Colorado State University
Chapter 14 Recursion.
Recursion Version 1.0.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Introduction to Arrays
Recursion -- Introduction
Lecture 10: recursive programming reading:
Chapter 19 Recursion.
null, true, and false are also reserved.
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Lecture 10: recursive programming reading:
CSE 143 Lecture 11 Recursive Programming reading:
Recursion.
CS18000: Problem Solving and Object-Oriented Programming
Recursion.
CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp
Recursion Chapter 18.
slides created by Marty Stepp and Alyssa Harding
Recursion based on slides by Alyssa Harding
Recursion Problems.
Chapter 18 Recursion.
Lecture 14: Strings and Recursion AP Computer Science Principles
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Introduction to Arrays
CSE 143 Lecture 13 Recursive Programming reading:
CSE 143 Lecture 13 Recursive Programming reading:
Recursion Method calling itself (circular definition)
Lecture 10: recursive programming reading:
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.
CSS161: Fundamentals of Computing
Dasar-Dasar Pemrograman 2: Recursion
Presentation transcript:

Building Java Programs Chapter 12 Lecture 12-2: recursive programming reading: 12.2 - 12.3

Exercise Write a recursive method pow accepts an integer base and exponent and returns the base raised to that exponent. Example: pow(3, 4) returns 81 Solve the problem recursively and without using loops.

An optimization Notice the following mathematical property: 312 = 531441 = 96 = (32)6 531441 = (92)3 = ((32)2)3 When does this "trick" work? How can we incorporate this optimization into our pow method? What is the benefit of this trick if the method already works?

Exercise Write a recursive method printBinary that accepts an integer and prints that number's representation in binary (base 2). Example: printBinary(7) prints 111 Example: printBinary(12) prints 1100 Example: printBinary(42) prints 101010 Write the method recursively and without using any loops. place 10 1 32 16 8 4 2 value

Stutter How did we break the number apart? public static int stutter(int n) { if (n < 10) { return (10 * n) + n; } else { int a = mystery(n / 10); int b = mystery(n % 10); return (100 * a) + b; }

Case analysis Recursion is about solving a small piece of a large problem. What is 69743 in binary? Do we know anything about its representation in binary? Case analysis: What is/are easy numbers to print in binary? Can we express a larger number in terms of a smaller number(s)?

printBinary solution // Prints the given integer's binary representation. // Precondition: n >= 0 public static void printBinary(int n) { if (n < 2) { // base case; same as base 10 System.out.println(n); } else { // recursive case; break number apart printBinary(n / 2); printBinary(n % 2); } Can we eliminate the precondition and deal with negatives?

Exercise Write a recursive method isPalindrome accepts a String and returns true if it reads the same forwards as backwards. isPalindrome("madam")  true isPalindrome("racecar")  true isPalindrome("step on no pets")  true isPalindrome("able was I ere I saw elba")  true isPalindrome("Java")  false isPalindrome("rotater")  false isPalindrome("byebye")  false isPalindrome("notion")  false

Exercise solution // Returns true if the given string reads the same // forwards as backwards. // Trivially true for empty or 1-letter strings. public static boolean isPalindrome(String s) { if (s.length() < 2) { return true; // base case } else { char first = s.charAt(0); char last = s.charAt(s.length() - 1); if (first != last) { return false; } // recursive case String middle = s.substring(1, s.length() - 1); return isPalindrome(middle); }

Exercise solution 2 // Returns true if the given string reads the same // forwards as backwards. // Trivially true for empty or 1-letter strings. public static boolean isPalindrome(String s) { if (s.length() < 2) { return true; // base case } else { return s.charAt(0) == s.charAt(s.length() - 1) && isPalindrome(s.substring(1, s.length() - 1)); }