Selected Advanced Topics Chapter 7 and 8

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
For(int i = 1; i
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Karel J Robot Chapter 6.
Starting Out with Java: From Control Structures through Objects
Recursion Recursive Thinking Recursive Programming
void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
Chapter 7 Sorting Part II. 7.3 QUICK SORT Example left right pivot i j 5 > pivot and should go to the other side. 2 < pivot and should go to.
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Recursion October 5, Reading Read pp in the text.
Programming Recursion.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Hopefully this lesson will give you an inception of what recursion is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
2.3 Introduction to Functions
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
IB Computer Science Unit 5 – Advanced Topics Recursion.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
Chapter 11 Sequences, Induction, and Probability Copyright © 2014, 2010, 2007 Pearson Education, Inc Sequences and Summation Notation.
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.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Karel J. Robot Chapter 6 Instructions That Repeat.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
8/2: Recursion About Scoping.java Recursion Program of the Day.
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 Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
8.1 – Sequences and Series. Sequences Infinite sequence = a function whose domain is the set of positive integers a 1, a 2, …, a n are the terms of the.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 10A Recursion (Concepts)
Karel J. Robot Chapter 6 Instructions That Repeat.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter Topics Chapter 16 discusses the following main topics:
Topic 6 Recursion.
Recursion DRILL: Please take out your notes on Recursion
using System; namespace Demo01 { class Program
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Introduction to Functions
Recursion Recursive Thinking Recursive Programming
Introduction to Functions
Steady-State Errors System & Control Engineering Lab.
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
Warm-up: 1. For an arithmetic sequence, , find,
Vocabulary Review Topic.
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Chapter 2 Section 2.
class PrintOnetoTen { public static void main(String args[]) {
CS302 - Data Structures using C++
Java Programming: Chapter 9: Recursion Second Edition
Review of Previous Lesson
Recursion Method calling itself (circular definition)
Recursion Pepper.
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Selected Advanced Topics Chapter 7 and 8 Karel J Robot Selected Advanced Topics Chapter 7 and 8

Recursion Recursion – where the function being defined is applied within it’s own definition Ex)(p185) public void findBeeper() { if(!nextToABeeper) { move(); findBeeper(); }}

Recursion Recursive functions do not use loops! Fun Practice: What is a factorial? Create the following method: public static int factorial(int x) Hints: what does a factorial do? Takes the number… multiply’s it by the next smallest integer Stops at 1 Your method should evaluate whether or not you’ve reached 1

Solution public static int factorial(int x) { if(x<=1) { return 1; } return x*factorial(x-1); }

challenge Create the following method: int sumOfDigits(int x) If x is 39, sumOfDigits should return 12 Hints: base case is a number from 0-9 For the recursive call, consider how x/10 or x%10 will help % - modulus – returns the remainder / - division – doesn’t round: 9/2 = 4

Homework Read chapter 7

Thread creation In the main, we have typically seen the following: MileMover karel = new MileMover(1,1,East,0); karel.move(); Having a robot set up his own thread, we can do the following: MileMover karel = new MileMover(1,1,East, 0); He will do the same thing as above without calling any methods if we have it set up its own thread!

Thread creation Implement the Runnable class //this is a java class that sets up threads In constructor: World.setupThread(this) //this tells the robot to run itself NEED: public void run() Inside of the run method will be the code that will automatically runj Lastly – in the main, we need: World.setTrace(false); World.showSpeedControl(true) Click resume!

Thread creation Modify the path finding robot from ch 6 pr set so that in the main, you only have the following: new ch6pr14(2,2,East,0) //you don’t even have to give the robot a name!

2 different threads Concurrent threads sometimes pose issues Homework Read chapter 8 – program the racers on 212 and the philosophers on 214

Problem set Ch 7, #1(Solve just #16 recursively), 18 Ch 8, #1 (requires editing steeplechase program) PACMAN

pacman Our final karel experience: 1st – set up the robot world to mimic a pacman world (karel images replaces with pacman, etc.) 2nd – create an abstract class called ghost Look up the 4 different types of ghosts…. And program their behaviors into different types of classes!

Pacman 3rd – the pacman class We need to be able to control him Eventually we will work with keylisteners for seemless play, but for now we’ll do an easier solution We need a ‘commandPacman()’ method Creates a local variable: Scanner kbd = new Scanner(System.in);

Pacman commandPacman() continued… Need a variable to hold keyboard input: char move = kb.next().charAt(0); Need a switch statement so 4 different keys control movements: switch(move) { case ‘w’: karel.faceNorth(); karel.move(); break; case ‘s’: karel.faceSouth(); //… you can figure the rest!  Use a while loop so that move == ‘q’ makes karel turnOff

Project Work in pairs! (we will try and mimic teamwork) 1 person can work on pacman while the other on ghosts 10 points – working pacman class 10 points – working ghost classes (i.e. shut off pacman when they touch) 2 points – accurate world 3 points – bonus for best version (class will vote)