Unit 3 Test: Friday.

Slides:



Advertisements
Similar presentations
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Advertisements

Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Multiple Choice Solutions True/False a c b e d   T F.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CS212: Data Structures and Algorithms
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion -- Introduction
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Recursion DRILL: Please take out your notes on Recursion
Abdulmotaleb El Saddik University of Ottawa
More on Recursive Recursion vs. Iteration Why Recursion?
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
More on Recursive Methods
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 5 - Functions Outline 5.1 Introduction
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Chapter 8: Recursion Java Software Solutions
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Java Programming: Chapter 9: Recursion Second Edition
Dr. Sampath Jayarathna Cal Poly Pomona
Java Software Solutions Foundations of Program Design Sixth Edition
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Unit 3 Test: Friday

AP Exam Takers… Have you bought the Barron’s book? Are you studying for 1 hour per night, at least?

ArrayList z = new ArrayList( ); z.add(1, “dog”); // result? This code will compile with no error, but there will be a runtime error: IndexOutOfBoundsException: Index: 1, Size: 0 Even though an ArrayList shrinks and grows, you still cannot access an index that does not exist.

public class Parent { private char letter = ‘a’; public int age = 18; // violates encapsulation! public double getGPA( ) { return 3.5; } } public class Child extends Parent { public void doStuff( ) { System.out.print(super.age); // valid, but it // violates encapsulation. double x = super.getGPA() // valid char y = super.letter; // not valid

for-each loops are often used with ArrayLists. Open ForEachLoopDemo

Review: static methods A static method can be called without having to create an object. It is called directly by using the class name (example: SavitchIn.readLine( ) ) It makes sense to make a method static when that method doesn’t have to be unique for each object that calls it. Demo: StaticMethodClass, StaticMethodClient

Static variables A variable can also be declared with the keyword static. Just like a static method, a static variable is not accessed through an object. It is accessed by using the name of the class itself. Why use a static variable? When that variable is not unique to each object of the class; when it is the same for all objects of the class. Now we know where instance variables get their name: they are unique for every instance (every object) of the class. A static variable is also known as a class variable. Demo: StaticVariableClass & StaticVariableClient

Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine that computes data” Recursion is a programming technique in which a method calls itself to solve a problem

Recursive Definitions Mathematical formulas often are expressed recursively N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive This definition can be expressed recursively as: 1! = 1 N! = N * (N-1)! The concept of the factorial is defined in terms of another factorial until the base case of 1! is reached

public int getSum (int num) { if (num == 1) // base case return 1; else return (num + getSum (num - 1)); }

Infinite Recursion All recursive definitions must have a non-recursive part If they don't, there is no way to terminate the recursive path The non-recursive part is called the base case, and is implemented using an if-statement Recursion without a base case causes infinite recursion This problem is similar to an infinite loop, and will cause a StackOverflowError exception

Recursive Programming A method in Java can invoke (call) itself; if set up that way, it is called a recursive method The code of a recursive method must be structured to handle both the base case and the recursive case Each call to the method sets up a new execution environment, with new parameters and new local variables As always, when the method execution completes, control returns to the method that invoked it (which may be an earlier invocation of itself)

Demos: RecursionClass RecursionClient

Fibonacci Numbers The Fibonacci Sequence is a series of numbers where a number is found by adding up the two numbers before it. 0, 1, 1, 2, 3, 5, 8, 13 ….. On board: list the nth Fibonacci number. How can we write a recursive method to find the nth Fib #?

public int fib (int n) { if (n <= 1) // base case return n; else return fib(n-1) + fib(n-2); }

Recursion vs. Iteration (looping) Just because we can use recursion to solve a problem, doesn't mean we should Every recursive algorithm (process for solving a problem) can be written iteratively (using a loop). Sometimes a loop is easier to understand, and more efficient Nevertheless, recursive solutions often are more simple and efficient than iterative solutions You must be able to determine when recursion is the correct technique to use http://en.wikipedia.org/wiki/Tower_of_Hanoi

Assignments In a class called Recursion2, write the following methods: factorial( ) -- receives an int parameter, return the factorial exponent( ) – receieves 2 int parameters, x and y, returns x to the y power Obviously, these must be recursive methods. You can’t use Math.pow( ) or any other method from the Math class. Now, write a client, Recursion2Client, to test the methods. Error check: other than the “x” (the base) in exponent( ), parameters must be positive. Complete 2014 Test (Sec 1: skip 5, 18, 23, 39, 40; Sec 2: Do #1-3)