Recursion Pepper.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Recursion Pepper. Recursion Definition English –procedure that can repeat a version of itself indefinitely – such as mirrors looking at each other. Math.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Basic Control Structures
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
294 ÷ 14. Look at the division problem.  The divisor, 14, can be divided into the first two digits of the dividend, 29, since you can get groups of 14.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
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.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Recursion occurs when a method calls itself. Google “recursion”
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
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.
Stage 3: Artist What do you remember from the last class?
Program design Program Design Process has 2 phases:
Lecture 9: introduction to recursion reading: 12.1
Recursion what is it? how to build recursive algorithms
Chapter 15 Recursion.
Building Java Programs
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Chapter 15 Recursion.
Recursion Chapter 11.
Recursion- Fibonacci mpack.
Chapter 6 More Conditionals and Loops
Recursion
Chapter 5: Loops and Files.
Programming Fundamentals
Counted Loops.
slides adapted from Marty Stepp and Hélène Martin
Flow Control in Java.
Decision statements. - They can use logic to arrive at desired results
Algorithm design and Analysis
PC02 Term 2 Test Recursion and Sorting. PC02 Term 2 Test Recursion and Sorting.
Java Language Basics.
We’re moving on to more recap from other programming languages
Building Java Programs
Recursion Pepper.
slides created by Marty Stepp and Alyssa Harding
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Recursion based on slides by Alyssa Harding
Basics of Recursion Programming with Recursion
slides adapted from Marty Stepp and Hélène Martin
Dr. Sampath Jayarathna Cal Poly Pomona
Building Java Programs
Building Java Programs
Building Java Programs
Making a Smile Face Pepper.
Loops and Iteration CS 21a: Introduction to Computing I
slides created by Marty Stepp
Building Java Programs
Snow Plough Division.
Building Java Programs
Lecture 20 – Practice Exercises 4
CSC Data Structures, Fall, 2008
loops revisited I/O arrays
Presentation transcript:

Recursion Pepper

Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count -- Ouput – total of all those numbers added up Logic: If the number I am counting is 0, just return 0. If the number I am counting is anything else, return the number I have + the total of all the rest (the number I have – 1) Requires: Trust the function will do its job A stopper A way to reach the stopper (decrement or increment) Code the addUp method and call it to addUp(10)

addUp Solution public class AddUp { public static void main() System.out.println(addUp(10)); } public static int addUp(int numberToCount) if (numberToCount == 0) return numberToCount;} else return numberToCount + addUp(numberToCount-1);

Create a string of stars starString routine -- Input – number of stars needed -- Ouput – String of that many stars Logic: If the number I am counting is 1, just return 1 star. If the number I am counting is anything else, return one star + all the rest of the stars (the number I have – 1) Requires: Trust the function will do its job A stopper A way to reach the stopper (decrement or increment) Code the starString method and call it to create starString(10)

String of Stars public static String starString (int numberStars) { if (numberStars == 0) return(""); } else return "*" + starString(numberStars-1);

Work with shapes Divide a rectangle up 4 levels deep First print 1 big square Then divide that one square Test fully Then create another method to printInsideSquares with input: level, paintbrush, starting x,y and size. Stop at level 1 – just print the square with no division At every other level, print a big square and printInsideSquares for the smaller squares

Work with Shapes First hint: Here is how to draw 1 level of 4 squares inside 1 square: public static void printOneInsideSquares(int level, Graphics g, int x, int y, int size) { g.drawRect(x,y,size,size); g.drawRect( x,y,size/2,size/2); // top left g.drawRect( x+(size/2), y,size/2,size/2); // top right g.drawRect( x,y+size/2,size/2,size/2); // bottom left g.drawRect( x+size/2,y+size/2,size/2,size/2); // bottom right } Now – add the level logic: stopper: If level == 1, just draw the main rectangle do a job and call itself to do the rest: If level higher, draw main rectangle and printInsideSquares of the small squares at the next level.