Ice01 PART 3: Tracing Programs

Note:  Please keep the programs that you create today, in case you have a question about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save his-or-her own copy.

 

ICE ICE 2.3.1: Code Tracing

One of the long-term goals of this class is for you to be able to write programs, correctly, without needing anything other than what you've got in your head. In order to be able to write programs correctly, you need to be able to correctly predict what a given program will do. In essence, you want to be able to simulate what the robot will do, in your head, without ever having to run the program on an actual computer.

The nice thing about computers is that they've very predictable, and very methodical. Given the same starting point, and the same set of instructions, the computer will do the exact same thing every single time that the program is run. Therefore, you need to learn how to be methodical and predictable, much like a computer is.

Once you've done this, you can write a program, mentally simulate it, and if it's wrong, fix it without ever having to run the program. This is much faster than typing something into the program, hoping it works, running the program, guessing why it doesn't work, and repeating the whole thing till you get the hang of it.

The technique that we'll use here is called code tracing. We're going to use a format very similar to the one used in the book. Remember to fill out a single line in the table below for EVERY line between the start and end of main in the sample code.

Let's look at the beginnings of an example trace table Demo_02_TRACE.doc as it traces the ICE_01_Tutorial_2.java which has an issue (a Wall between the Robot and the Thing).

To start using a Program Trace Table on your own (it can also be found and downloaded from the main page of the class website under Templates Used In Class).

Use it to trace ICE_02_02_Trace.java.

ICE Part 2.3.2: Commenting Code (Single Line)

NOTENote: that you will have to do this in all your homework Assignments, so it's a good idea to practice here in class

We can express what the programs in Java source code if we want, but this is a time-consuming and exacting language – it's often difficult to clearly say what it is that the program does just from looking at the code. So we want to look for other ways to tell our fellow programmers what the program is supposed to do.

One way we can do this is by commenting our code.

A comment is a region of code that the Java compiler ignores. That means that we can put whatever we want there, knowing that it's not really part of the program anyways. In particular, we can put an description, in English, of what we intend for our code to do, in order to make it clear what the program is supposed to do.

This is often a good way to find logical (intent) errors – the comment says that the program should do one thing, but the actually does something else.

There are a number of different ways you can comment your code in Java. We'll start with a single line comment.

Somewhere on the line, you should put two forward slashes right next to each other. If you do this right, jGrasp will color-code them orange, like so:

//

Anything that you write AFTER the slashes is ignored, so you can put English there, and everything will work fine. You should use this to clarify what your code is about to do:

// Make Jo turn right by turning left 3 times
Jo.turnLeft();
Jo.turnLeft();
Jo.turnLeft();

As an aside, anything on the line BEFORE the slashes is normal Java code:

// Make Jo turn right by turning left 3 times
Jo.turnLeft(); // First turn - 90 degrees
Jo.turnLeft(); // Second turn - 180 degrees
Jo.turnLeft(); // Last turn - 270 degrees
// same as 90 deg turn to right

You should make sure that you can do this in your own programs, because you're required to document the code you hand in for homework assignments.

ICE Part 3: Commenting Code (Multiple Lines, Block Comments)

Another way to comment code in Java is to comment out multiple lines using block comments. Let's say I had multiple lines of comments that I wanted to comment out, like those below.

This entire section
is being blocked
out using comments
that will not show up
when the program
is compiled!

If I wanted to I could comment this out line-by-line using the two forward slashes //, like so:

//This entire section
//is being blocked
//out using comments
//that will not show up
//when the program
//is compiled!

But since I have multiple lines to comment, a faster way would be to comment the whole shebang using block comments (the /* and */ characters):

/*
This entire section
is being blocked
out using comments
that will not show up
when the program
is compiled!
*/