CodeHS Unit 1 (ANSWERS)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

1.7.4: The Two Towers + Comments

/* * This program has Karel run a race by jumping * two hurdles. */ public class TwoTowersKarel extends Karel { public void run() { move(); buildTower(); comeDown(); move(); move(); buildTower(); move(); turnRight(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } private void turnAround() { turnLeft(); turnLeft(); } private void comeDown() { turnAround(); move(); move(); turnLeft(); } private void buildTower() { turnLeft(); putBall(); move(); putBall(); move(); putBall(); } }

1.3.4: Tower Karel

public class TowerKarel extends Karel { public void run() { move(); turnLeft(); putBall(); move(); putBall(); move(); putBall(); move(); turnLeft(); turnLeft(); turnLeft(); } }

1.9.5: Marathon Karel

class MarathonKarel extends SuperKarel { public void run() { move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); move(); } }

1.1.4: Your First Karel Program

move(); move(); move(); move(); takeBall();

1.1.5: Short Stack

move(); putBall(); putBall(); move();

1.2.4: Make a Tower

move(); turnLeft(); putBall(); move(); putBall(); move(); putBall(); move(); turnLeft(); turnLeft(); turnLeft();

1.9.7: Dizzy Karel

public class DizzyKarel extends SuperKarel { public void run() { for(int i = 0; i < 32; i++) { turnLeft(); } } }

1.16.5: Double Tennis Balls

public class DoubleTennisBallsKarel extends SuperKarel { public void run() { move(); doubleBalls(); move(); turnAround(); putBack(); goHome(); } //this doubles the pile of balls private void doubleBalls() { while(ballsPresent()) { takeBall(); move(); putBall(); putBall(); turnAround(); move(); turnAround(); } } private void putBack() { while(ballsPresent()) { takeBall(); move(); putBall(); //putBall(); turnAround(); move(); turnAround(); } } private void goHome() { move(); move(); turnAround(); } }

1.16.1: Fetch

public class FetchKarel extends SuperKarel { public void run() { //comment// turnLeft(); for (int i = 0; i < 4; i++) { move(); } turnRight(); move(); move(); takeBall(); move(); turnRight(); for (int i = 0; i < 4; i++) { move(); } turnRight(); move(); move(); move(); turnAround(); putBall(); } //more bellow! private void ff() { move(); } private void ee() { move(); } private void gg() { move(); } }

1.4.5: Fireman Karel

public class FiremanKarel extends Karel { public void run() { turnRight(); move(); move(); move(); turnLeft(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } }

1.3.5: Gold Medal Karel

public class GoldMedalKarel extends Karel { public void run() { move(); putBall(); move(); putBall(); move(); putBall(); turnLeft(); move(); turnLeft(); move(); putBall(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft(); turnLeft(); turnLeft(); } }

1.9.9: Lots of Hurdles

public class HurdlesKarel extends SuperKarel { public void run() { for(int i = 0; i < 5; i++) { move(); move(); jumpHurdle(); } } private void jumpHurdle() { turnLeft(); move(); turnRight(); move(); turnRight(); move(); turnLeft(); } }

1.5.5: Mario Karel

public class MarioKarel extends Karel { public void run() { move(); turnLeft(); move(); move(); move(); collectCoins(); move(); turnRight(); move(); move(); turnRight(); move(); collectCoins(); move(); turnLeft(); move(); move(); turnLeft(); move(); collectCoins(); move(); turnRight(); move(); move(); turnRight(); move(); collectCoins(); move(); move(); move(); turnLeft(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } private void collectCoins() { takeBall(); takeBall(); } }

1.16.6: Midpoint Karel

public class MidpointKarel extends SuperKarel { public void run() { turnLeft(); while(frontIsClear()) { move(); if(frontIsClear()) { move(); turnRight(); move(); turnLeft(); } } turnAround(); while(frontIsClear()) { move(); } turnLeft(); putBall(); } }

1.5.4: Pancakes

public class PancakeKarel extends Karel { public void run() { makePancakes(); makePancakes(); makePancakes(); } private void makePancakes() { move(); putBall(); putBall(); putBall(); move(); } }

1.16.2: Racing Karel

public class RacingKarel extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { put8(); race(); } } //this puts 8 balls private void put8() { for(int i = 0; i < 8; i++) { putBall(); } } private void race() { while(frontIsClear()) { move(); } turnLeft(); } }

1.13.4: Random Hurdles

public class RandomHurdles extends SuperKarel { public void run() { for(var i = 0; i < 13; i++) { if (frontIsClear()) { move(); } else { jumpHurdle(); } } } private void jumpHurdle() { turnLeft(); move(); turnRight(); move(); turnRight(); move(); turnLeft(); } }

1.12.4: Right Side Up

public class RightSideUp extends SuperKarel { public void run() { if (facingSouth()) { turnLeft(); } else { checkNorthAndWest(); } } private void checkNorthAndWest() { if(facingNorth()) { turnRight(); } if(facingWest()) { turnAround(); } } }

1.4.4: Slide Karel

public class SlideKarel extends Karel { public void run() { putBall(); move(); turnLeft(); turnLeft(); turnLeft(); move(); putBall(); move(); turnLeft(); move(); putBall(); } }

1.9.8: For Loop Square

public class SquareKarel extends SuperKarel { public void run() { for(int i = 0; i < 4; i++) { putBall(); move(); turnLeft(); } } }

1.15.5: Staircase

public class Staircase extends SuperKarel { public void run() { putBall(); while(frontIsClear()) { turnLeft(); while (ballsPresent()) { move(); } turnRight(); move(); createStep(); } } private void createStep() { turnRight(); putBall(); while (frontIsClear()) { move(); putBall(); } turnLeft(); } }

1.16.4: Super Cleanup Karel

public class SuperCleanupKarel extends SuperKarel { //comment public void run() { while (leftIsClear()) { cleanRow(); comeBack(); moveUp(); } cleanRow(); comeBack(); if (leftIsBlocked()) { returnHome(); } } private void moveUp() { turnLeft(); move(); turnRight(); } private void comeBack() { turnAround(); while(frontIsClear()) { move(); } turnAround(); } private void cleanRow() { while(frontIsClear()) { cleanSpot(); move(); } cleanSpot(); } private void cleanSpot() { if (ballsPresent()) { takeBall(); } } private void returnHome() { cleanRow(); comeBack(); } }

1.9.6: Take 'em All

public class TakeEmAllKarel extends SuperKarel { public void run() { move(); for(int i = 0; i < 100; i++) { takeBall(); } move(); } }

1.16.3: Tower Builder

public class TowerBuilderKarel extends SuperKarel { public void run() { while(frontIsClear()) { tower(); move(); if(frontIsClear()) { move(); } else { turnRight(); } } //this helps karel figure out what world she is in if(facingSouth()) { turnLeft(); } else { tower(); } } private void tower() { turnLeft(); putBall(); move(); putBall(); move(); putBall(); turnAround(); move(); move(); turnLeft(); } }

1.10.7: Big Tower

/* This program draws a big tower from Karel's starting spot */ public class BigTowerKarel extends SuperKarel { public void run() { turnNorth(); buildTower(); } // This method has Karel face north, no matter what direction // Karel starts facing. private void turnNorth() { while(notFacingNorth()) { turnLeft(); } } // This method builds a tower all the way to the top of the world. private void buildTower() { while(frontIsClear()) { putBall(); move(); } putBall(); } }

1.15.4: Diagonal

/* This program has karel lay a diagonal row of tennis balls. * However, the indenting is all wrong. Can you properly * indent this program? */ public class Diagonal extends SuperKarel { public void run() { while(frontIsClear()) { putBall(); move(); turnLeft(); move(); turnRight(); } putBall(); } }

1.10.6: Lay Row of Tennis Balls

/* Write a program to lay a row of tennis balls * across first street. Make sure to test your * program on multiple worlds. */ public class TennisBallRowKarel extends SuperKarel { public void run() { while(frontIsClear()) { putBall(); move(); } putBall(); } }

1.10.4: Follow The Yellow Ball Road

// Follow the yellow ball road! // Karel moves until it's not on a tennis ball. public class YellowBallRoadKarel extends SuperKarel { public void run() { while(ballsPresent()) { move(); } } }

1.11.5: Is There a Ball?

// Karel should put a ball on the first spot // if there isn't one already there and then move. public class IsThereABallKarel extends SuperKarel { public void run() { if(noBallsPresent()) { putBall(); } move(); } }

1.3.6: Maze Karel

// This program has Karel solve the maze and find the ball class MazeKarel extends Karel { public void run() { move(); move(); move(); move(); turnLeft(); move(); move(); move(); move(); turnLeft(); move(); move(); move(); turnLeft(); move(); move(); move(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft(); turnLeft(); turnLeft(); move(); move(); move(); move(); move(); turnLeft(); turnLeft(); turnLeft(); move(); move(); move(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft(); move(); turnLeft(); move(); turnLeft(); turnLeft(); turnLeft(); move(); move(); } }

1.10.5: Just Keep Spinning

// This program has Karel turn left one time for // each tennis ball that is present class JustKeepSpinning extends SuperKarel { public void run() { while(ballsPresent()) { takeBall(); turnLeft(); } } }

1.6.4: The Two Towers

public class TwoTowersKarel extends Karel { public void run() { move(); turnLeft(); buildTower(); threeSixty(); downThree(); turnLeft(); move(); move(); turnLeft(); buildTower(); move(); turnRight(); } public void turnRight() { turnLeft(); turnLeft(); turnLeft(); } public void buildTower() { putBall(); move(); putBall(); move(); putBall(); } public void threeSixty() { turnLeft(); turnLeft(); } public void downThree() { move(); move(); } }

1.8.4: The Two Towers + SuperKarel

public class TwoTowersKarel extends SuperKarel { public void run() { move(); turnLeft(); buildTower(); threeSixty(); downThree(); turnLeft(); move(); move(); turnLeft(); buildTower(); move(); turnRight(); } public void buildTower() { putBall(); move(); putBall(); move(); putBall(); } public void threeSixty() { turnLeft(); turnLeft(); } public void downThree() { move(); move(); } }

1.2.5: Pyramid of Karel

putBall(); move(); putBall(); move(); putBall(); turnLeft(); move(); putBall(); move(); putBall(); turnLeft(); move(); turnLeft(); move(); putBall(); turnLeft(); move(); turnLeft(); move(); turnLeft(); turnLeft(); turnLeft();


Kaugnay na mga set ng pag-aaral

Ch. 16 Bushong Challange Questions - Digital Radiolgraphy

View Set

Anatomy & Physiology Blood, Ch 14

View Set

World History Honors unit 2: Ancient Greece

View Set

Ch 25 - Employment Discrimination

View Set