15
Karel J Robot Selected Advanced Topics Chapter 7 and 8

Karel J Robot

  • Upload
    jemma

  • View
    69

  • Download
    2

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Karel  J Robot

Karel J RobotSelected Advanced TopicsChapter 7 and 8

Page 2: Karel  J Robot

RecursionRecursion – where the function

being defined is applied within it’s own definition

Ex)(p185) public void findBeeper(){ if(!nextToABeeper){ move(); findBeeper();}}

Page 3: Karel  J Robot

RecursionRecursive 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 integerStops at 1Your method should evaluate whether or

not you’ve reached 1

Page 4: Karel  J Robot

Solution public static int factorial(int x)

{ if(x<=1) { return 1; } return x*factorial(x-1); }

Page 5: Karel  J Robot

challengeCreate the following method:int sumOfDigits(int x)If x is 39, sumOfDigits should

return 12Hints: base case is a number

from 0-9For the recursive call, consider

how x/10 or x%10 will help% - modulus – returns the

remainder/ - division – doesn’t round:

9/2 = 4

Page 6: Karel  J Robot

HomeworkRead chapter 7

Page 7: Karel  J Robot

Thread creationIn 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!

Page 8: Karel  J Robot

Thread creationImplement the Runnable class //this is a java

class that sets up threadsIn constructor:World.setupThread(this) //this tells the robot

to run itselfNEED: public void run()Inside of the run method will be the code that

will automatically runjLastly – in the main, we need:World.setTrace(false);

World.showSpeedControl(true)Click resume!

Page 9: Karel  J Robot

Thread creationModify 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!

Page 10: Karel  J Robot

2 different threadsConcurrent threads sometimes

pose issuesHomeworkRead chapter 8 – program the

racers on 212 and the philosophers on 214

Page 11: Karel  J Robot

Problem setCh 7, #1(Solve just #16

recursively), 18Ch 8, #1 (requires editing

steeplechase program)PACMAN

Page 12: Karel  J Robot

pacmanOur 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!

Page 13: Karel  J Robot

Pacman3rd – 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);

Page 14: Karel  J Robot

PacmancommandPacman() 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

Page 15: Karel  J Robot

ProjectWork in pairs! (we will try and mimic

teamwork)1 person can work on pacman while

the other on ghosts10 points – working pacman class10 points – working ghost classes (i.e.

shut off pacman when they touch)2 points – accurate world3 points – bonus for best version

(class will vote)