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
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. Data Structures - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
First ExampleFirst Example
Task to write a non-negative integer to Task to write a non-negative integer to screen with decimal digits stackedscreen with decimal digits stacked
Number 7895Number 7895 OutputOutput
77
88
99
55
One digit solutionOne digit solution
Print that digit!Print that digit! Number 7Number 7 Output Output
77
Many digit solutionMany digit solution
Print all digits except the last, stacked Print all digits except the last, stacked verticallyvertically
Then print the last digitThen print the last digit
77
88
99 Now print the 5 which is 7895%10Now print the 5 which is 7895%10
PseudocodePseudocode
//printing the digits of a non-negative number stacked vertically
if (the number has only one digit)
write that digit.
else
write the digits of the number/10 stacked vertically
Write the single digit of number %10.
Recursive solutionRecursive solution
Always has a stopping case (base case)Always has a stopping case (base case)if (number < 10)if (number < 10)
System.out.println(number);System.out.println(number); Always has a recursive callAlways has a recursive callelseelse
Write a method which will move a Car forward until it reaches a barrier...
Your MissionYour Mission
Write a method which will move a Car forward until it reaches a barrier...
Your MissionYour Mission
Write a method which will move a Car forward until it reaches a barrier...
Your MissionYour Mission
Write a method which will move a Car forward until it reaches a barrier...
...then the car is turned around...
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.
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.
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)
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)
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( );. . .
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 ?
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..
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.
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( ){ . . .
An ExerciseAn Exercise
public void ricochet( ){ if (isBlocked( )) turnAround( ); // Base case else { // Recursive pattern move( ); ricochet( ); move( ); }}