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
  • Karel's World
  • Walls:
  • Streets:
  • Avenues:
  • Karel's Direction
  • Our Second Karel Program

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

More Basic Karel

PreviousIntroduction to Programming with KarelNextJava Programs and the Run Method

Last updated 5 years ago

Was this helpful?

Karel can navigate a world by moving and turning left within it. Karel can also take balls and put balls down. Recall that these are the only four commands Karel knows:

move();
putBall();
takeBall();
turnLeft();

Karel resides in a boxed-in world. Let's review the basic components of Karel's world.

Karel's World

Karel's world is a grid composed of walls, streets, and avenues.

Walls:

Walls block Karel's movement. Walls include the borders of a world as well as any lines running through the world.

Karel cannot move through walls! If Karel attempts a move(); command while directly in front of a wall, Karel will crash into it.

Streets:

A street is a row in the grid.

The 5th row, or 5th street, is highlighted in the example world above.

Avenues:

An avenue is a column in the grid.

The 8th column, or 8th avenue, is highlighted in the example world above.

Karel's Direction

Karel can face one of four directions at any given time - north, east, south, or west. The direction Karel is facing determines which direction Karel will move. If Karel is facing north, a move(); command will make Karel move up. If Karel is facing east, a move(); command will make Karel move to the right. The same is true for the south and west directions.

Karel can always go right, left, or forward from any position.

For example, if Karel is initially facing east, a turnLeft(); command will result in Karel facing north.

Similarly, if Karel is initially facing north, a turnLeft(); command will result in Karel facing west.

Our Second Karel Program

Using our knowledge of direction, let's have Karel make a square out of balls.

Here is the solution. The comments explain which direction Karel ends up facing after each turnLeft(); command.

/* Tennis Ball Square
 * This program has karel place a square of tennis balls
 * and return to his starting point.
 */

//Karel begins facing east
putBall();
move();
turnLeft(); //Karel is now facing north

putBall();
move();
turnLeft(); //Karel is now facing west

putBall();
move();
turnLeft(); //Karel is now facing south

putBall();
move();
turnLeft(); //Karel finishes facing east.
Karel's World: Walls
Karel's World: Streets
Karel's World: Avenues
KarelNorthSouthEastWest
KarelLeftFrontRight
KarelEastToNorthTurn
KarelNorthToWestTurn
Our Second Karel Program