Free University of Bolzano/Bozen
Faculty of Computer Science - Bachelor in Applied Computer Science
Introduction to Programming - A.A. 2005/2006

Exercise 10

Recursion


Exercise 10A

Use BlueJ's debugger to follow the evolution of the calling stack of the program Fibonacci.java.
Proceed as follows:

  1. save the source code in a local folder;
  2. open the folder from within BlueJ (command Open Non BlueJ);
  3. compile class Fibonacci;
  4. open Fibonacci.java and set a breakpoint at line 4 (first instruction in fibonacci method); you can do this by moving the cursor to the instruction and selecting Set/Clear Breakpoint from the Tools menu (alternatively, just click on the left border next to the instruction);
  5. launch the main method and follow the execution step by step (use the Step, Step Into and Continue buttons in the debug window).

Note how each call of fibonacci is suspended until all recursive calls return (the first call is the last to return).


Exercise 10B

A monochromatic image can be seen as a matrix of elements that are 0 or 1. If the image is shown on a screen, a 1 means a picture element (called pixel) is on and 0 means a pixel is off. A matrix of this kind is called a bitmap.
Implement a class Map that represents a monochromatic image as a bitmap. To keep things simple use int as the type for the matrix elements.
Besides the constructor, Map should have the following two methods:

Write a class Main that creates a Map object given the following example matrix:

    int[][] image =
            {
              { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
              { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 },
              { 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
              { 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
              { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
              { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
              { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
              { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
              { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
              { 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
              { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
              { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
              { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
              { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
              { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };
Print out the Map object before and after calling the floodfill method for row 6 and column 6.
This is how the result should look:
          ####      
   ###  ##    #     
  #   ###     #     
 #            #     
 #             #    
 #             #    
  #             #   
  #             #   
   ####         #   
       #       #    
       #       #    
        ##     #    
          #####     
before method call
          ####      
   ###  #######     
  #############     
 ##############     
 ###############    
 ###############    
  ###############   
  ###############   
   ##############   
       #########    
       #########    
        ########    
          #####     
after method call

Solution