Tino APCS

Program Comments

There are three kinds of program comments in Java.

// This is a single-line comment.
// Everything after the double slashes is ignored by Java.

/* This is a multi-line comment.
Everything between the slash stars is ignored by Java.
Note the reversal of slash-STAR then STAR-slash.
*/

/** This is a Javadoc comment.
Javadoc comments have additional formatting and are used to create APIs.
You will learn about them in Lesson 6.
*/

Why?

Comments are used to add clarity to programs.

It’s not obvious what this code does:

pen.up();
    pen.move(-1020Math.cos(Math.toRadians(60)), -150);
    pen.down();
 
for (int i = 0; i < 10; i++) { pen.setDirection(90); pen.forward(100); pen.turnRight(30); pen.forward(20); pen.turnRight(120); pen.forward(20); pen.turnRight(30); pen.forward(100); pen.turnRight(90); pen.forward(220Math.cos(Math.toRadians(60))); pen.setDirection(0); pen.forward(220Math.cos(Math.toRadians(60))); }

Here is the same code but much easier to understand:

/* This program draws a row of 10 fenceposts by
using a for-loop to repeatly draw one post and
moving over to the next post location.
*/

    // Move the pen's starting location so the fence is centered horizontally
    pen.up();
    pen.move(-10*20*Math.cos(Math.toRadians(60)), -150);
    pen.down();

    // Draw 10 fenceposts by repeating one post 10 times
    for (int i = 0; i < 10; i++) {
        pen.setDirection(90);      // Left side
        pen.forward(100);
        pen.turnRight(30);         // Triangle top
        pen.forward(20);
        pen.turnRight(120);
        pen.forward(20);
        pen.turnRight(30);
        pen.forward(100);          // Right side
        pen.turnRight(90);

        // Base
        pen.forward(2*20*Math.cos(Math.toRadians(60)));
        pen.setDirection(0);

        // Move to next starting location
        pen.forward(2*20*Math.cos(Math.toRadians(60)));
    }

Remember:

Comment every program you write!


Dark Mode

Outline