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 SuperKarel
  • Using SuperKarel
  • A SuperKarel Example

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

SuperKarel

Karel the Dog only knows a few basic commands. For example, in order to have Karel turn right, you first must create a new method to teach Karel how to turn right. Wouldn't it be nice if Karel already knew some of these commands?

Introducing SuperKarel

Now that you've had some practice writing methods like turnRight();, you are ready to meet SuperKarel. SuperKarel already knows turnRight() and turnAround(), so you don't need to create your own methods for those two commands anymore!

Using SuperKarel

To write a Karel class, you extended Karel. To use SuperKarel, you will extend SuperKarel instead.

// Regular Karel
public class HurdleKarel extends Karel

// SuperKarel
public class HurdleKarel extends SuperKarel

A SuperKarel Example

Using SuperKarel allows us to write fewer methods, since Karel already knows how to turn right and turn around. Below is an example of the HurdleKarel program using SuperKarel. Notice how the jumpHurdle method calls turnRight(); even though this method is not defined in the program?

/* 
 * This program has Karel run a race by jumping
 * two hurdles. This is a SuperKarel program which
 * means turnRight() is built in.
 */
public class HurdleKarel extends SuperKarel
{

    /* 
     * 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 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 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();
      }

}
PreviousCommenting Your CodeNextFor Loops

Last updated 5 years ago

Was this helpful?