4
Instruction – CoffeeDregs
4.1
CoffeeDregs
We are going to use the tool CoffeeDregs that visualizes running Java programming. It can help you
understand what is going on when a program is executing. Furthermore, it may help you with problems
with programs that do not behave as intended (i.e., it helps you solve bugs).
To install and run CoffeeDregs, proceed as follows.
1. Download the file CoffeeDregs3.0 Fat.jar from the website.
2. Put it in a convenient place on your hard disk.
3. Run it by opening (double clicking) the jar-file.
4. On Macintosh, double clicking the jar-file may not work. In that case, proceed as follows:
(a) open a Terminal window (the application Terminal can be found in the folder Utilities in the
folder Applications.
(b) Type cd and drag the folder where the jar-file resides to the window. Its complete path name
will appear in the window. Press Enter.
(c) Type java -jar CoffeeDregs3.0.jar
5. Load a program into CoffeeDregs (via the File-menu). You load the .java file. The corresponding
.class file should be in the same folder.
Remark: the .class file should be compiled with the so-called debugging information included. DrJava
does this automatically.
4.1.1
Exercise – While
Download the following program from the website.
class WhileExample {
int n;
int i;
void demo( ) {
n = 1;
i = 0;
while ( n < 10 ) {
i++;
n = n ∗ 2;
}
}
public static void main( String[] args) {
( new WhileExample() ).demo();
}
}
1. Compile the program with DrJava. Start CoffeeDregs and load the program.
2. Execute the program step by step. If all is well, it should take 18 steps to reach the end.
1
3. Go back to the state where n is checked for the last time in the loop, i.e., the last time where the line
while ( n < 10 ) is marked yellow. What is the number of this state? What is the value of n in
that state? And i?
4. Write down the values of n and i every time the program is at the guard of the loop, i.e., the line
with while is yellow.
5. Express the relationship between n and i in a formula n = . . . that should hold every time the
program is at the guard of the loop.
6. Execute the program and check whether your formula holds every time the program is at the beginning of the loop and after the loop.
4.2
Methods
Download the following program Cascade.java from the website. Load it in CoffeeDregs and compile.
class Cascade {
int xl;
void demo() {
cas( 1 );
System.out.println( ” xl is now ”+xl );
}
void cas( int x3 ) {
xl += x3;
ca( 1 );
}
void ca( int x2 ) {
xl += x2;
de( 1 );
}
void de( int xl ) {
xl += xl;
}
public static void main(String[] args) {
( new Cascade() ).demo();
}
}
1. The program calls three methods, cas, ca, and de in a chain, or cascade. Each method adds 1 to the
instance variable xl, so you would expect the final value of xl to be 3. Run the program in DrJava
and observe that the output is not what we expected.
2. Load the program in CoffeeDreges (if you have a program loaded there, close it first). Execute it
step by step and observe waht happens to xl. Explain why the result is not 3. Which method is the
culprit?
3. Repair the program in DrJava, compile. Close the program in CoffeeDregs and load it again. Observe
again its behavior.
2
4.3
Arrays
Download the following program Searching.java from the website. Load it in CoffeeDregs and compile.
class Searching {
int[] a;
void search() {
int there;
a = new int[]{ 3, 5, −1, −2, 7, 6, 5, 4, 3};
there = whereIs7();
System.out.println( ”A 7 has been found at position ” +there+ ”.”);
}
int whereIs7( ) {
int i = 0;
while ( a[i] != 7 ) {
i++;
}
return i;
}
public static void main(String[] ar) {
( new Searching() ).search();
}
}
1. Execute the program in CoffeeDregs and observe its beahvior.
2. Go to DrJava, remove the 7 from the initalization of the array and compile. Run the program in
CoffeeDregs. Observe that execution does not continue at a certain point (about state 24), whereas
the methods main, search, and whereIs7 are not finished. The program has crahsed, as it is called.
Explain why.
3. Repair the method whereIs7 such that returns the value -1 when the array a does not contain the
number 7. Execute it in CoffeeDregs.
3