570 likes | 689 Views
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
E N D
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 and Other Objects Using Java
First Example • Task to write a non-negative integer to screen with decimal digits stacked • Number 7895 • Output 7 8 9 5
One digit solution • Print that digit! • Number 7 • Output 7
Many digit solution • Print all digits except the last, stacked vertically • Then print the last digit 7 8 9 • Now print the 5 which is 7895%10
Pseudocode //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 solution • Always has a stopping case (base case) if (number < 10) System.out.println(number); • Always has a recursive call else { writeVertical(number/10); system.out.println(number%10); }
Recursively call Number is 7895 Call4 7 Call 3 78 Call 3 78 Call 2 789 Call 2 789 Call 2 789 Call 2 789 Call 1 7895 Call 1 7895 Call 1 7895 Call 1 7895 Call 1 7895
Now come back Call4 7 Call 4 with a 7 is a one digit number Therefore, print out that single digit and that call is complete Call 3 78 Call 3 78 Call 2 789 Call 2 789 7 Call 1 7895 Call 1 7895
Now return from 4th call Returning from that 4th call the next statement if System.out.println(number%10) Since that number is 78 : print out 78%10 or 8 Call 3 78 7 8 Call 2 789 Call 2 789 Call 1 7895 Call 1 7895
Now return from third call Returning from that 3rd call the next statement if System.out.println(number%10) Since that number is 789 : print out 789%10 or 9 7 8 9 Call 2 789 Call 1 7895 Call 1 7895
Returning from 2nd call Returning from that 2nd call the next statement if System.out.println(number%10) Since that number is 7895 : print out 789%10 or 5 7 8 9 5 Call 1 7895
Return from first call We’re Done!!!!
To start the example, think about your favorite family car A Car Object
To start the example, think about your favorite family car A Car Object
To start the example, think about your favorite family car A Car Object
To start the example, think about your favorite family car 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 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 The radio signals are generated by activating methods of a Car object A Car Class public class Car { . . . }
methods for the Car Class 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! ... }
When we declare a Car and activate the constructor, the computer makes a radio link with a car that has a particular number. The Constructor public static void main(...) { Car racer = new Car(7); . . .
When we activate turnAround, the computer signals the car to turn 180 degrees. The turnAround method public static void main(...) { Car racer = new Car(7); racer.turnAround( ); . . .
When we activate move, the computer signals the car to move forward one foot. The move method public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( ); . . .
When we activate move, the computer signals the car to move forward one foot. The move method public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( ); . . .
The isBlocked method detects barriers. The isBlocked( ) method public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( ); if (racer.isBlocked( ) ) System.out.println (“Cannot move!”); . . .
Write a method which will move a Car forward until it reaches a barrier... Your Mission
Write a method which will move a Car forward until it reaches a barrier... Your Mission
Write a method which will move a Car forward until it reaches a barrier... Your Mission
Write a method which will move a Car forward until it reaches a barrier... ...then the car is turned around... 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. 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. 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. Your Mission public void ricochet(Car movingCar)
if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around. Pseudocode for ricochet public void ricochet(Car movingCar)
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: Pseudocode for ricochet public void ricochet(Car movingCar) movingCar.move( ); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); This makes the problem a bit smaller. For example, if the car started 100 feet from the barrier... movingCar.move( ); . . . 100 ft.
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: Pseudocode for ricochet 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. movingCar.move( ); . . . 99 ft.
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: Pseudocode for ricochet public void ricochet(Car movingCar); We now have a smaller version of the same problem that we started with. movingCar.move( ); . . . 99 ft.
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: Pseudocode for ricochet public void ricochet(Car movingCar); Make a recursive call to solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . . 99 ft.
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . . 99 ft.
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .
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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . . 99 ft.
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: Pseudocode for ricochet public void ricochet(Car movingCar); What is the last step that's needed to return to our original location ? movingCar.move( ); ricochet(movingCar); . . . 99 ft.
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: Pseudocode for ricochet public void ricochet(Car movingCar); What is the last step that's needed to return to our original location ? movingCar.move( ); ricochet(movingCar); movingCar.move( ); 100 ft.
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: Pseudocode for ricochet public void ricochet(Car movingCar) This recursive method follows a common pattern that you should recognize. movingCar.move( ); ricochet(movingCar); movingCar.move( );