Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming...

47
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 Data Structures and Other Objects and Other Objects Using Java Using Java

Transcript of Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming...

Page 1: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

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 ThinkingRecursive Thinking

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing JavaUsing Java

Page 2: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

A Car ObjectA Car Object

To start the example, think about your favorite family car

Page 3: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

A Car ObjectA Car Object

To start the example, think about your favorite family car

Page 4: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

A Car ObjectA Car Object

To start the example, think about your favorite family car

Page 5: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

A Car ObjectA Car Object

To start the example, think about your favorite family car

Page 6: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

A Car ObjectA 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

Page 7: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

A Car ClassA 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

Page 8: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

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 Classmethods for the Car Class

Page 9: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

public static void main(...){ Car racer = new Car(7);

. . .

The ConstructorThe Constructor

When we declare a Car

and activate the constructor, the computer makes a radio link with a car that has a particular number.

Page 10: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

public static void main(...){ Car racer = new Car(7);

racer.turnAround( ); . . .

The turnAround methodThe turnAround method

When we activate turnAround, the computer signals the car to turn 180 degrees.

Page 11: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

public static void main(...){ Car racer = new Car(7);

racer.turnAround( ); racer.move( ); . . .

The move methodThe move method

When we activate move, the computer signals the car to move forward one foot.

Page 12: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

public static void main(...){ Car racer = new Car(7);

racer.turnAround( ); racer.move( ); . . .

The move methodThe move method

When we activate move, the computer signals the car to move forward one foot.

Page 13: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

public static void main(...){ Car racer = new Car(7);

racer.turnAround( ); racer.move( ); if (racer.isBlocked( ) ) System.out.println (“Cannot move!”); . . .

The isBlocked( ) methodThe isBlocked( ) method

The isBlocked method detects barriers.

Page 14: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

Page 15: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

Page 16: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

Page 17: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

...then the car is turned around...

Page 18: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Your MissionYour 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.

Page 19: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Your MissionYour 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.

Page 20: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Your MissionYour 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)

Page 21: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Pseudocode for ricochetPseudocode 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)

Page 22: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Pseudocode for ricochetPseudocode 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( );. . .

Page 23: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );. . .

Pseudocode for ricochetPseudocode 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.

Page 24: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );. . .

Pseudocode for ricochetPseudocode 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.

Page 25: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );. . .

Pseudocode for ricochetPseudocode 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.

Page 26: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 27: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 28: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 29: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 30: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 31: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 32: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 33: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 34: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 35: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 36: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 37: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode 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.

Page 38: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode 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.

Page 39: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode 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.

Page 40: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode 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.

Page 41: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode 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...

Page 42: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode 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...

Page 43: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode 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..

Page 44: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Implementation of ricochetImplementation 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.

Page 45: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

An ExerciseAn Exercise

Can you write Can you write ricochet as a new ricochet as a new method of the Car method of the Car class, instead of a class, instead of a separate method?separate method?

You have 2 minutes towrite the implementation.

public void ricochet( ){ . . .

Page 46: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

An ExerciseAn Exercise

public void ricochet( ){ if (isBlocked( )) turnAround( ); // Base case else { // Recursive pattern move( ); ricochet( ); move( ); }}

One solution:

Page 47: Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

THE ENDTHE END

Presentation copyright 1999 Addison Wesley Longman,For use with Data Structures and Other Objects Using Javaby 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 (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using Java are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.