AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Introducing comments
  • Why comment?
  • Multi-line comments
  • Single line comments
  • Preconditions and postconditions
  • Example: Hurdle Karel with comments
  • Full Example:

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

Commenting Your Code

There are two parts to writing a program: One part is getting it to work (the functionality). The other part is the style: How did you write the program? Did you break it down into parts? Is it clear to read?

This is where comments come into play!

Introducing comments

Comments are a way for you to leave notes about what your code is doing in plain English so that other people can understand it.

Why comment?

Even though code is run by computers, it is read by other people. Comments help others understand your code. If you read your code ten years from now, would you be able to understand what it does?

The computer that runs your program knows not to ignore comments if they are written as multi-line or single line comments.

Multi-line comments

Use multi-line comments to leave a comment so a reader understands what your program or method is doing. The start of a multi-line comment uses /*, each line following with a single *, and ending the multi-line comment with */.

/* 
 * This program does ______. 
 */

Single line comments

Use single line comments to leave a comment for a single line in your code. For each single line comment, put two backslashes (//) before the comment.

//This line does ______.

Preconditions and postconditions

Another important part of commenting is using preconditions and postconditions. We can leave notes at the top of our methods about assumptions that we make.

Preconditions: What assumptions we make and what must be true before the method is called.

Postconditions: What should be true after the method has been called.

This helps us think about the problem and make sure our methods line up.

Example: Hurdle Karel with comments

Let's add comments to our Hurdle Karel program. First, we want to add a multi-line comment to describe what the program does at the top.

/*
 * This program has Karel run a race 
 * by jumping two hurdles. 
 */
public class HurdleKarel extends Karel
{
    ...
}

Next, let's add another multi-line comment to describe what our method, run(), does.

/*
 * This program has Karel run a race 
 * by jumping two hurdles. 
 */
public class HurdleKarel extends Karel
{
    /* 
     * This method is our main entry point for the program.
     * Karel runs to the hurdle and jumps it twice, before
     * finishing the race.
     * Precondition: Karel should be in the bottom left
     * corner, facing east.
     * Postcondition: Karel should be at the end of the race.
     */ 
    public void run()
    {
        runToHurdle();
        jumpToHurdle();
        runToHurdle();
        jumpToHurdle();
        finish();
    }
}

Let's also add a multi-line comment for another method, runToFinish().

    /* This method has Karel move four times to finish the race.
     * Precondition: Karel has finished jumping the last hurdle.
     * Postcondition: Karel is at the end of the race.
     */
    private void runToFinish()
    {
        move();
        move();
        move();
        move();
    }

In the runToFinish() method, we can add a single line comment.

  private void runToFinish()
  {
        // This method has Karel move four times to finish the race.
        move();
        move();
        move();
        move();
  }

Full Example:

 * This program has Karel run a race by jumping
 * two hurdles.
 */
public class HurdleKarel extends Karel
{

    /* 
     * This method is our main entry point for the program.
     * Karel runs to the hurdle and jumps it twice, before
     * finishing the race.
     * Precondition: Karel should be in the bottom left
     * corner, facing east.
     * Postcondition: Karel should be at the end of the race.
     */
    public void run()
    {
        runToHurdle();
        jumpHurdle();
        runToHurdle();
        jumpHurdle();
        runToFinish();
    }

    /* This method has Karel move four times to finish the race.
     * Precondition: Karel has finished jumping the last hurdle.
     * Postcondition: Karel is at the end of the race.
     */
    private void runToFinish()
    {
        // This method has Karel move four times to finish the race.
        move();
        move();
        move();
        move();
    }

    /* 
     * This method has Karel run to a hurdle that is three spots
     * away.
     * Precondition: Karel is three spaces away from a hurdle,
     * facing east.
     * Postconding: Karel is right next to a hurdle, ready to jump
     * over it.
     */
    private void runToHurdle()
    {
        move();
        move();
        move();
    }

    /*
     * This method has Karel jump over a hurdle that
     * is one row tall.
     * Precondtion: Karel is right next to a hurdle, facing east.
     * Postcondition: Karel has jumped over the hurdle and is on
     * the other side, facing east.
     */
    private void jumpHurdle()
    {
        turnLeft();
        move();
        turnRight();
        move();
        turnRight();
        move();
        turnLeft();
    }

    /* 
     * This method has Karel turn right.
     * Precondition: None, Karel can be facing any direction.
     * Postcondition: Karel will have turned right by 90 degrees.
     */
    private void turnRight()
    {
        turnLeft();
        turnLeft();
        turnLeft();
    }
}
PreviousTop Down Design and Decomposition in KarelNextSuperKarel

Last updated 5 years ago

Was this helpful?