{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ReviewSessionPracticeSolutions

ReviewSessionPracticeSolutions - REVIEW SESSION PRACTICE...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
REVIEW SESSION PRACTICE MIDTERM SOLUTIONS RockClimbingKarel public class RockClimbingKarel extends SuperKarel { public void run() { moveToWall(); turnLeft(); climbRock(); putBeeper(); } private void moveToWall(){ while (frontIsClear()){ move(); } } private void climbRock(){ climbToTop(); positionAtEdge(); } private void climbToTop(){ while (rightIsBlocked()){ if (frontIsClear()){ move(); } else { moveToNextEdge(); } } } private void moveToNextEdge(){ turnLeft(); move(); turnRight(); move(); } private void positionAtEdge(){ turnRight(); move(); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
RandomClickDrag public class RandomClickDrag extends GraphicsProgram{ private static final double MAX_LEN = 200; private static final double MIN_LEN = 100; private double lastX ; private double lastY ; private GObject currentRect ; private RandomGenerator rgen = new RandomGenerator(); public void run(){ addMouseListeners(); } public void mouseClicked(MouseEvent e){ currentRect = getElementAt(e.getX(), e.getY()); if ( currentRect != null ){ ((GRect) currentRect ).setFillColor( rgen .nextColor()); } else { GRect rect = new GRect( rgen .nextDouble( MIN_LEN , MAX_LEN ), rgen .nextDouble( MIN_LEN , MAX_LEN )); rect.setLocation(e.getX() - (rect.getWidth() / 2),
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}