Download presentation
Presentation is loading. Please wait.
1
Selected Advanced Topics Chapter 7 and 8
Karel J Robot Selected Advanced Topics Chapter 7 and 8
2
Recursion Recursion – where the function being defined is applied within it’s own definition Ex)(p185) public void findBeeper() { if(!nextToABeeper) { move(); findBeeper(); }}
3
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
4
Solution public static int factorial(int x) { if(x<=1) { return 1; } return x*factorial(x-1); }
5
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
6
Homework Read chapter 7
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!
8
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!
9
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!
10
2 different threads Concurrent threads sometimes pose issues Homework
Read chapter 8 – program the racers on 212 and the philosophers on 214
11
Problem set Ch 7, #1(Solve just #16 recursively), 18
Ch 8, #1 (requires editing steeplechase program) PACMAN
12
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!
13
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);
14
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
15
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.