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
  • Methods
  • Tower and Turn Right

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

Karel Can't Turn Right

You may have noticed that Karel can not turn right. Karel can only turn left. To get Karel to turn right, you have to write out three consecutive turnLeft(); commands like so:

// Turning left three times is the same as turning right once.
turnLeft();
turnLeft();
turnLeft();

Wouldn't it be easier if we had a turnRight(); command that did the same thing?

Methods

In the previous chapter, we introduced the run method. This method is where our program starts executing our commands.

public void run()
{
    // Our code begins here
}

We can create additional methods using a similar syntax. A method is how we teach Karel new commands. By using the power of methods, we can teach Karel how to turnRight();. Here is the turnRight() method:

private void turnRight()
{
    turnLeft();
    turnLeft();
    turnLeft();
}

We write all of our commands within the method body; this is the area between the opening and closing curly brackets ({}).

Tower and Turn Right

Let's have Karel build a tower and then turnRight(); using methods.

public class TowerKarel extends Karel
{

    public void run()
    {
        move();
        putBall();
        turnLeft();
        move();
        putBall();
        move();
        putBall();
        move();
        turnLeft();
        turnLeft();
        turnLeft();
    }
}

While this solution works, we aren't using a turnRight() method. Let's fix that.

public class TowerKarel extends Karel
{
    public void run()
    {
        move();
        putBall();
        turnLeft();
        move();
        putBall();
        move();
        putBall();
        move();
        turnRight();
    }
}

We replaced our three turnLeft(); commands with one turnRight(); command, but the program still does not work. Why is that?

When we run the program, we get an error message telling us that Karel does not know how to turn right. We still need to teach Karel how to turnRight(); by writing a turnRight() method.

public class TowerKarel extends Karel
{
    public void run()
    {
        move();
        putBall();
        turnLeft();
        move();
        putBall();
        move();
        putBall();
        move();
        turnRight();
    }

    private void turnRight()
    {
        turnLeft();
        turnLeft();
        turnLeft();
    }
}

By writing a turnRight() method, we have taught Karel a new command and completed the program.

PreviousJava Programs and the Run MethodNextMethods in Karel

Last updated 5 years ago

Was this helpful?

Tower and Turn Right
Karel Can't Turn Right