public class Karel extends Program implements KarelInterface, Runnable
Karel
class represents the simplest possible Karel the Robot
object. Each instance of the Karel
class represents an
individual robot that can move inside a world laid out as a grid of streets
and avenues. The other characteristics of Karel's world include
walls, which separate individual corners, and
beepers, which are described by Karel's creator as
"plastic cones which emit a quiet beeping noise." As it is shipped from the
factory, Karel can execute only four operations:
move()
turnLeft()
pickBeeper()
pickBeeper
instruction only if there is a beeper on
the current corner.
putBeeper()
pickBeeper
instruction only
if there is a beeper in its bag.
Karel programs are executed by defining a new subclass that extends
Karel
and provides it with a run
method that
defines its operation. For example, the following Karel subclass represents a
Karel program that moves forward, picks up a beeper from the square, and then
moves forward one more time:
|
JApplet.AccessibleJApplet
Applet.AccessibleApplet
Panel.AccessibleAWTPanel
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
CONFIG_FILE_NAME, invisibleObjects
accessibleContext, rootPane, rootPaneCheckingEnabled
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
CENTER
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
Karel() |
Modifier and Type | Method and Description |
---|---|
boolean |
beepersInBag() |
boolean |
beepersPresent() |
protected void |
checkWorld(String caller) |
boolean |
facingEast() |
boolean |
facingNorth() |
boolean |
facingSouth() |
boolean |
facingWest() |
boolean |
frontIsBlocked() |
boolean |
frontIsClear() |
int |
getBeepersInBag() |
int |
getDirection() |
Point |
getLocation() |
KarelWorld |
getWorld() |
boolean |
leftIsBlocked() |
boolean |
leftIsClear() |
static void |
main(String[] args) |
boolean |
menuAction(ActionEvent event)
Called whenever a program action is detected in the menu bar.
|
void |
move() |
boolean |
noBeepersInBag() |
boolean |
noBeepersPresent() |
boolean |
notFacingEast() |
boolean |
notFacingNorth() |
boolean |
notFacingSouth() |
boolean |
notFacingWest() |
void |
pickBeeper() |
void |
putBeeper() |
boolean |
rightIsBlocked() |
boolean |
rightIsClear() |
void |
run()
Specifies the code to be executed as the program runs.
|
void |
setBeepersInBag(int nBeepers) |
void |
setDirection(int dir) |
void |
setDisplayOneFlag(boolean flag) |
void |
setLocation(int x,
int y) |
void |
setLocation(Point pt) |
void |
setWorld(KarelWorld world) |
void |
start(String[] args)
Starts a
KarelProgram containing this Karel instance,
passing it the specified arguments. |
void |
turnLeft() |
actionPerformed, add, addActionListeners, addActionListeners, addActionListenersToButtonsOnly, addAllActionListeners, addExitHook, addImpl, adjustmentValueChanged, alert, alert, animation, center, changedUpdate, checkCompilerFlags, checkKill, checkStartupSettings, componentAdded, componentHidden, componentMoved, componentRemoved, componentResized, componentShown, confirm, contentsChanged, createConsole, createDialogIO, createMenuBar, createParameterTable, createProgramFrame, destroy, endHook, error, error, error, exit, fileExists, fileExists, fileExistsInsideJAR, fileExistsInsideJAR, fileExistsOnDisk, fileExistsOnDisk, fileSize, fileSize, focusGained, focusLost, getAnimationDelay, getAppletStub, getArgumentArray, getBoolean, getBoolean, getBoolean, getBorder, getBottomY, getCenterLocation, getCenterX, getCenterY, getCentralRegionSize, getConsole, getDialog, getDouble, getDouble, getDouble, getDouble, getHeight, getInputModel, getInteger, getInteger, getInteger, getInteger, getJFrame, getLayout, getLine, getLine, getMainThread, getMenuBar, getOutputColor, getOutputModel, getParameter, getParameterTable, getPreferredSize, getReader, getReal, getReal, getReal, getReal, getRegionPanel, getRightX, getScreenHeight, getScreenSize, getScreenWidth, getStartupObject, getTitle, getWidth, getWindow, getWriter, getYesOrNo, hasConfiguration, init, insertUpdate, intervalAdded, intervalRemoved, isAnimated, isApplet, isAppletMode, isExitOnClose, isInitialized, isStarted, itemStateChanged, keyPressed, keyReleased, keyTyped, killMe, loadConfiguration, loadConfiguration, menuCanceled, menuDeselected, menuSelected, mouseClicked, mouseDragged, mouseEntered, mouseExited, mouseMoved, mousePressed, mouseReleased, mouseWheelMoved, openFile, openFile, openFileFromDisk, openFileFromDisk, openFileFromJAR, openFileFromJAR, pack, pause, pauseTick, popupMenuCanceled, popupMenuWillBecomeInvisible, popupMenuWillBecomeVisible, print, print, print, print, print, print, print, print, print, print, print, print, print, print, print, print, printf, println, println, println, println, println, println, println, println, println, println, println, println, println, println, println, println, println, prompt, readBoolean, readBoolean, readBoolean, readDouble, readDouble, readDouble, readDouble, readInt, readInt, readInt, readInt, readLine, readLine, remove, remove, removeAll, removeFromRegion, removeMenuBar, removeUpdate, repaint, runHook, saveConfiguration, saveConfiguration, setAnimated, setAnimationDelay, setAppletStub, setBackground, setBottomY, setCenterLocation, setCenterX, setCenterY, setConsole, setDialog, setExitOnClose, setFramesPerSecond, setHeight, setInputModel, setInvisible, setJFrame, setJMenuBar, setLayout, setLocation, setLocation, setOutputColor, setOutputModel, setParameter, setParameterTable, setPauseScaleFactor, setResizable, setRightX, setShowPixelGrid, setShowPixelInfo, setSize, setStartupObject, setTitle, setWidth, setX, setY, showErrorMessage, start, startAnimation, startHook, startRun, stateChanged, stopAnimation, textValueChanged, validate, valueChanged, windowActivated, windowClosed, windowClosing, windowDeactivated, windowDeiconified, windowGainedFocus, windowIconified, windowLostFocus, windowOpened, windowStateChanged
createRootPane, getAccessibleContext, getContentPane, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isRootPaneCheckingEnabled, paramString, repaint, setContentPane, setGlassPane, setLayeredPane, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update
getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameterInfo, isActive, isValidateRoot, newAudioClip, play, play, resize, resize, setStub, showStatus, stop
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getListeners, getMaximumSize, getMinimumSize, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, revalidate, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
pause
public void run()
Program
run
method is required for applications that have
a thread of control that runs even in the absence of user actions,
such as a program that uses console interation or that involves
animation. GUI-based programs that operate by setting up an initial
configuration and then wait for user events usually do not specify a
run
method and supply a new definition for init
instead.run
in interface ProgramInterface
run
in interface Runnable
run
in interface KarelInterface
run
in class Program
public void move()
move
in interface KarelInterface
public void turnLeft()
turnLeft
in interface KarelInterface
public void pickBeeper()
pickBeeper
in interface KarelInterface
public void putBeeper()
putBeeper
in interface KarelInterface
public boolean frontIsClear()
frontIsClear
in interface KarelInterface
public boolean frontIsBlocked()
frontIsBlocked
in interface KarelInterface
public boolean leftIsClear()
leftIsClear
in interface KarelInterface
public boolean leftIsBlocked()
leftIsBlocked
in interface KarelInterface
public boolean rightIsClear()
rightIsClear
in interface KarelInterface
public boolean rightIsBlocked()
rightIsBlocked
in interface KarelInterface
public boolean beepersPresent()
beepersPresent
in interface KarelInterface
public boolean noBeepersPresent()
noBeepersPresent
in interface KarelInterface
public boolean beepersInBag()
beepersInBag
in interface KarelInterface
public boolean noBeepersInBag()
noBeepersInBag
in interface KarelInterface
public boolean facingNorth()
facingNorth
in interface KarelInterface
public boolean facingEast()
facingEast
in interface KarelInterface
public boolean facingSouth()
facingSouth
in interface KarelInterface
public boolean facingWest()
facingWest
in interface KarelInterface
public boolean notFacingNorth()
notFacingNorth
in interface KarelInterface
public boolean notFacingEast()
notFacingEast
in interface KarelInterface
public boolean notFacingSouth()
notFacingSouth
in interface KarelInterface
public boolean notFacingWest()
notFacingWest
in interface KarelInterface
public static void main(String[] args)
public boolean menuAction(ActionEvent event)
Program
menuAction
in class Program
public void start(String[] args)
KarelProgram
containing this Karel instance,
passing it the specified arguments.start
in interface ProgramInterface
start
in class Program
args
- An array of strings passed to the programpublic Point getLocation()
getLocation
in interface KarelInterface
getLocation
in class Component
public void setLocation(Point pt)
setLocation
in interface KarelInterface
setLocation
in class Component
public void setLocation(int x, int y)
setLocation
in interface KarelInterface
setLocation
in class Component
public int getDirection()
getDirection
in interface KarelInterface
public void setDirection(int dir)
setDirection
in interface KarelInterface
public int getBeepersInBag()
getBeepersInBag
in interface KarelInterface
public void setBeepersInBag(int nBeepers)
setBeepersInBag
in interface KarelInterface
public KarelWorld getWorld()
getWorld
in interface KarelInterface
public void setWorld(KarelWorld world)
setWorld
in interface KarelInterface
protected void checkWorld(String caller)
public void setDisplayOneFlag(boolean flag)