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.AccessibleJAppletApplet.AccessibleAppletPanel.AccessibleAWTPanelContainer.AccessibleAWTContainerComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategyCONFIG_FILE_NAME, invisibleObjectsaccessibleContext, rootPane, rootPaneCheckingEnabledBOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTCENTERABORT, 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, windowStateChangedcreateRootPane, getAccessibleContext, getContentPane, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isRootPaneCheckingEnabled, paramString, repaint, setContentPane, setGlassPane, setLayeredPane, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, updategetAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameterInfo, isActive, isValidateRoot, newAudioClip, play, play, resize, resize, setStub, showStatus, stopadd, 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, validateTreeaction, 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, transferFocusUpCycleclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitpausepublic void run()
Programrun 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 ProgramInterfacerun in interface Runnablerun in interface KarelInterfacerun in class Programpublic void move()
move in interface KarelInterfacepublic void turnLeft()
turnLeft in interface KarelInterfacepublic void pickBeeper()
pickBeeper in interface KarelInterfacepublic void putBeeper()
putBeeper in interface KarelInterfacepublic boolean frontIsClear()
frontIsClear in interface KarelInterfacepublic boolean frontIsBlocked()
frontIsBlocked in interface KarelInterfacepublic boolean leftIsClear()
leftIsClear in interface KarelInterfacepublic boolean leftIsBlocked()
leftIsBlocked in interface KarelInterfacepublic boolean rightIsClear()
rightIsClear in interface KarelInterfacepublic boolean rightIsBlocked()
rightIsBlocked in interface KarelInterfacepublic boolean beepersPresent()
beepersPresent in interface KarelInterfacepublic boolean noBeepersPresent()
noBeepersPresent in interface KarelInterfacepublic boolean beepersInBag()
beepersInBag in interface KarelInterfacepublic boolean noBeepersInBag()
noBeepersInBag in interface KarelInterfacepublic boolean facingNorth()
facingNorth in interface KarelInterfacepublic boolean facingEast()
facingEast in interface KarelInterfacepublic boolean facingSouth()
facingSouth in interface KarelInterfacepublic boolean facingWest()
facingWest in interface KarelInterfacepublic boolean notFacingNorth()
notFacingNorth in interface KarelInterfacepublic boolean notFacingEast()
notFacingEast in interface KarelInterfacepublic boolean notFacingSouth()
notFacingSouth in interface KarelInterfacepublic boolean notFacingWest()
notFacingWest in interface KarelInterfacepublic static void main(String[] args)
public boolean menuAction(ActionEvent event)
ProgrammenuAction in class Programpublic void start(String[] args)
KarelProgram containing this Karel instance,
passing it the specified arguments.start in interface ProgramInterfacestart in class Programargs - An array of strings passed to the programpublic Point getLocation()
getLocation in interface KarelInterfacegetLocation in class Componentpublic void setLocation(Point pt)
setLocation in interface KarelInterfacesetLocation in class Componentpublic void setLocation(int x,
int y)
setLocation in interface KarelInterfacesetLocation in class Componentpublic int getDirection()
getDirection in interface KarelInterfacepublic void setDirection(int dir)
setDirection in interface KarelInterfacepublic int getBeepersInBag()
getBeepersInBag in interface KarelInterfacepublic void setBeepersInBag(int nBeepers)
setBeepersInBag in interface KarelInterfacepublic KarelWorld getWorld()
getWorld in interface KarelInterfacepublic void setWorld(KarelWorld world)
setWorld in interface KarelInterfaceprotected void checkWorld(String caller)
public void setDisplayOneFlag(boolean flag)