Download presentation

Presentation is loading. Please wait.

Published byRegina Pick Modified over 4 years ago

1
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. Recursive Thinking Data Structures and Other Objects Using Java

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 public class Car {... } 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
public class Car { public Car(int carNumber); public public void move( ); public void turnAround( ); public boolean isBlocked( );... We don't need to know the private fields!... } methods for the Car Class

9
public static void main(...) { Car racer = new Car(7);... 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
public static void main(...) { Car racer = new Car(7); racer.turnAround( );... The turnAround method When we activate turnAround, the computer signals the car to turn 180 degrees.

11
public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( );... The move method When we activate move, the computer signals the car to move forward one foot.

12
public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( );... The move method When we activate move, the computer signals the car to move forward one foot.

13
public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( ); if (racer.isBlocked( ) ) System.out.println (Cannot move!);... 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. public void ricochet(Car movingCar)

21
Pseudocode for ricochet if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around. public void ricochet(Car movingCar)

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: public void ricochet(Car movingCar) movingCar.move( );...

23
movingCar.move( );... 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: public void ricochet(Car movingCar); This makes the problem a bit smaller. For example, if the car started 100 feet from the barrier... 100 ft.

24
movingCar.move( );... 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: public void ricochet(Car movingCar); This makes the problem a bit smaller. For example, if the car started 100 feet from the barrier... then after activating move once, the distance is only 99 feet. 99 ft.

25
movingCar.move( );... 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: public void ricochet(Car movingCar); We now have a smaller version of the same problem that we started with. 99 ft.

26
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); Make a recursive call to solve the smaller problem. 99 ft.

27
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem. 99 ft.

28
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem.

29
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem.

30
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem.

31
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem.

32
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem.

33
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem.

34
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem.

35
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem.

36
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); The recursive call will solve the smaller problem. 99 ft.

37
movingCar.move( ); ricochet(movingCar);... 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: public void ricochet(Car movingCar); What is the last step that's needed to return to our original location ? 99 ft.

38
movingCar.move( ); ricochet(movingCar); movingCar.move( ); 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: public void ricochet(Car movingCar); What is the last step that's needed to return to our original location ? 100 ft.

39
movingCar.move( ); ricochet(movingCar); movingCar.move( ); 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: public void ricochet(Car movingCar) This recursive method follows a common pattern that you should recognize.

40
movingCar.move( ); ricochet(movingCar); movingCar.move( ); 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: public void ricochet(Car movingCar) When the problem is simple, solve it with no recursive call. This is the base case.

41
movingCar.move( ); ricochet(movingCar); movingCar.move( ); 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: public void ricochet(Car movingCar) When the problem is more complex, start by doing work to create a smaller version of the same problem...

42
movingCar.move( ); ricochet(movingCar); movingCar.move( ); 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: public void ricochet(Car movingCar)...use a recursive call to completely solve the smaller problem...

43
movingCar.move( ); ricochet(movingCar); movingCar.move( ); 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: public void ricochet(Car movingCar)...and finally do any work that's needed to complete the solution of the original problem..

44
Implementation of ricochet public void ricochet(Car movingCar) { if (movingCar.isBlocked( )) movingCar.turnAround( ); // Base case else { // Recursive pattern movingCar.move( ); ricochet(movingCar); movingCar.move( ); } Look for this pattern in the other examples of Chapter 8.

45
An Exercise Can you write ricochet as a new method of the Car class, instead of a separate method? You have 2 minutes to write the implementation. public void ricochet( ) {...

46
An Exercise public void ricochet( ) { if (isBlocked( )) turnAround( ); // Base case else { // Recursive pattern move( ); ricochet( ); move( ); } One solution:

47
T HE E ND Presentation copyright 1999 Addison Wesley Longman, For use with Data Structures and Other Objects Using Java by Michael Main. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using Java are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.

Similar presentations

OK

Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.

Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google