Notes
Slide Show
Outline
1
Recursive Thinking
  • Chapter 8 introduces the technique of recursive programming.
  • As you have seen, recursive programming involves spotting smaller occurrences of a problem within the problem itself.
  • This presentation gives an additional example, which is not in the book.
2
A Car Object
  • To start the example, think about your favorite family car
3
A Car Object
  • To start the example, think about your favorite family car
4
A Car Object
  • To start the example, think about your favorite family car
5
A Car Object
  • To start the example, think about your favorite family car
6
A Car Object
  • To start the example, think about your favorite family car
  • Imagine that the car is controlled by a radio signal from a computer
7
A Car Class
  • To start the example, think about your favorite family car
  • Imagine that the car is controlled by a radio signal from a computer
  • The radio signals are generated by activating methods of a Car object
8
methods for the Car Class
9
The Constructor
  • When we declare a Car
  • and activate the constructor, the computer makes a radio link with a car that has a particular number.
10
The turnAround method
  • When we activate turnAround, the computer signals the car to turn 180 degrees.
11
The move method
  • When we activate move, the computer signals the car to move forward one foot.
12
The move method
  • When we activate move, the computer signals the car to move forward one foot.
13
The isBlocked( ) method
  • The isBlocked method detects barriers.
14
Your Mission
  • Write a method which will move a Car forward until it reaches a barrier...
15
Your Mission
  • Write a method which will move a Car forward until it reaches a barrier...
16
Your Mission
  • Write a method which will move a Car forward until it reaches a barrier...
17
Your Mission
  • Write a method which will move a Car forward until it reaches a barrier...
  • ...then the car is turned around...
18
Your Mission
  • Write a method which will move a Car forward until it reaches a barrier...
  • ...then the car is turned around...
  • ...and returned to its original location, facing the opposite way.
19
Your Mission
  • Write a method which will move a Car forward until it reaches a barrier...
  • ...then the car is turned around...
  • ...and returned to its original location, facing the opposite way.
20
Your Mission
  • Write a method which will move a Car forward until it reaches a barrier...
  • ...then the car is turned around...
  • ...and returned to its original location, facing the opposite way.
21
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
22
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
23
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
24
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
25
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
26
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
27
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
28
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
29
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
30
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
31
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
32
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
33
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
34
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
35
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
36
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
37
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
38
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
39
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
40
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
41
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
42
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
43
Pseudocode for ricochet
  • if movingCar.isBlocked( ), then the car is already at the barrier.  In this case, just turn the car around.
  • Otherwise, the car has not yet reached the barrier, so start with:
44
Implementation of ricochet
45
An Exercise
  • Can you write ricochet as a new method of the Car class, instead of a separate method?
46
An Exercise
47
THE  END