Exercise 10A
Use BlueJ's debugger to follow the evolution of the calling stack of the
program Fibonacci.java
.
Proceed as follows:
Open Non BlueJ
);
Fibonacci
;
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);
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:
toString
returns a string that is a textual representation
of the stored bitmap - you might show 0 as space (" ") and
1 as hash sign ("#");floodfill
that takes the coordinates of the seed point
(row r
and column c
) as parameters and fills the
connected area of 0-pixels the seed point is in with 1-pixels -
remember to check the boundaries of the bitmap as well to avoid overflow.
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