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
  • Programming with Karel
  • Karel's Grid World
  • Karel's commands
  • What does each command do?
  • The parts of a command
  • Our First Karel Program

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

Introduction to Programming with Karel

Computers play an integral role in the modern world. From playing games to banking online, we interact with computers directly and indrectly every day. Understanding how computers work and how to work with them is an important skill. Computer programming is the process of giving commands to a computer. Sequences of commands can be combined into programs that perform specific tasks.

Programming with Karel

Giving instructions to a computer is much like giving commands to a dog. In this course, we will learn the basics of programming with Karel the Dog.

Karel's world is a simple one: Karel can move around the world and put down and pick up tennis balls. Though Karel only knows a few commands, these commands can be combined into interesting programs to solve many different problems and explore the basics of computer science.

Karel's Grid World

Karel's world is actually a grid. Each point on the grid is marked by a dot. Karel can move from point to point and travel around the world. The horizontal rows are called streets in Karel's world, and the vertical columns are referred to as avenues. In each spot, Karel can face four directions: north, south, east, or west.

Karel's commands

In order to have Karel perform an action, you need to give Karel a command. A command is an instruction that tells Karel what to do.

Karel only knows four commands:

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

What does each command do?

The move() command has Karel move one spot in the direction that Karel is facing.

The putBall() command puts down one tennis ball in the spot where Karel is standing.

The takeBall() command removes one tennis ball from the spot where Karel is standing.

The turnLeft() command turns Karel 90 degrees to the left.

The parts of a command

Notice that Karel's commands look a little different from plain English words. This is because Karel's commands are written as computer code. These simple spelling rules are called the syntax of Karel's language. There are a few things to keep in mind when writing commands for Karel:

  • You need to write the command exactly as it appears in the list above.

  • You need to match the exact capitalization. For example, turnleft(); is incorrect. It should be written as turnLeft(); with an uppercase L.

  • Make sure to end every command with a ();

  • Each command goes on its own line.

Our First Karel Program

Imagine you have Karel starting in the world on the left: in the bottom left corner, facing east. You want to get Karel to the world on the right. What would be the code that you write?

Remember, Karel has four commands, and we can use those commands to write this program. Below you can find the program.

Here's a program we could write to have Karel complete the task:

move();
move();
putBall();
move();
move();
PreviousIntroduction to Programming in Java with Karel the DogNextMore Basic Karel

Last updated 5 years ago

Was this helpful?

Start and Ending World