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
  • Writing a Java Class
  • Adding The Run Method
  • Square Karel as a Full Java Program

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

Java Programs and the Run Method

So far, we have been writing our Java programs as a series of commands. However, a truly proper Java program requires a few additional parts. From now on, we will be writing our commands inside of a Karel class and a run method.

Writing a Java Class

The general format for writing a Java class is:

public class ClassName extends SomeOtherClass
{

}

Let's break this down a little further by looking at each of these parts individually:

  • public - The visibility of the class. For right now, all of our classes will be public.

  • class - This simply tells Java that we are writing a class.

  • ClassName - The name of the class. You can name your class anything, but the name should make sense and describe your program appropriately. Class names are written in UpperCamelCase, where each word starts with a capital letter.

  • extends SomeOtherClass - Extending SomeOtherClass allows us to use properties and methods from that class. This is known as inheritance. You will learn more about inheritance as well as the extends keyword in future chapters.

  • { } - Classes start with an opening curly bracket ({) and end with a closing curly bracket (}). The rest of our program goes in between these two brackets.

Let's look at an example using the Karel class:

public class SquareKarel extends Karel
{

}

In this example, SquareKarel is the name of our class. Our SquareKarel class extends the Karel class. This allows us to write a Karel program. You will learn more about this later, but for right now, you will only need to extend the Karel class.

Adding The Run Method

A class contains methods. Before we begin writing our code, we need to add a run method to our class. The run method is where our program starts its execution.

public class SquareKarel extends Karel
{
    public void run()
    {
        // Our code here
    }
}

The run method is indented inside of the opening and closing curly brackets ({}) of the class body. The run method has its own set of curly brackets ({}). We write our Karel commands within the run method's curly brackets.

Let's look at an example:

public class MoveKarel extends Karel
{
    public void run()
    {
        move();
    }
}

When we run this program, Karel will simply move forward one spot.

Square Karel as a Full Java Program

Let's rewrite our second Karel program, SquareKarel, as a full and proper Java program. What would it look like?

We simply encapsulate our code from before inside of the run method. The run method is inside of our SquareKarel class.

/* SquareKarel
 * This program has karel place a square of tennis balls
 * and return to his starting point.
 */
public class SquareKarel extends Karel
{
    public void run()
    {
        //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.
    }
}
PreviousMore Basic KarelNextKarel Can't Turn Right

Last updated 5 years ago

Was this helpful?

Our Second Karel Program