15-100:
Introductory/Intermediate Programming
(Section F) |
Fall 2006 |
Relevant Reading:
Assignment: (worth 10 exercise points) 1) Start getting the software necessary for this course. In particular, get the Java SDK software from http://java.sun.com/javase/downloads/index.jsp. 2) Java
programs look very much like English paragraphs though written
in a very stilted and awkward style. With some thought, study,
and
reasoning you should be able to read through a Java program and get an
idea of what the program is doing. For this exercise, you
will read 4 Java programs and determine what they do. Trace the execution of the program on
paper very carefully on the scratch sheet. Each
program runs to a
successful
termination – there are no errors or crashes. For
each program, you are given instructions for how to draw your answer. Handin: Bring all of your papers to class on Wednesday. You will turn in a copy of your “final situations” page at the start of class. Be sure your name is on it.
Part A: 4 points Specifications: Draw the following on the sheet marked “final situations”:
package kareltherobot; public class Application implements Directions { public static void main(String [ ] args) { World.setVisible(true); World.readWorld("worldA.txt"); World.setTrace(true); World.setDelay(10); Robot karel = new Robot(1, 1, East, 25); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.turnLeft(); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.turnLeft(); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.turnLeft(); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.turnLeft(); karel.move(); karel.move(); karel.move(); karel.turnOff(); }// end of method main } // end of class Application Part B: 4 points Specifications: Draw the following on the sheet marked “final situations”:
package kareltherobot; public class Application implements Directions { public static void main(String [ ] args) { World.setVisible(true); World.readWorld("worldB.txt"); World.setTrace(true); World.setDelay(10); Robot scott = new Robot(2, 2, North, 10); scott.putBeeper(); scott.move(); scott.putBeeper(); scott.move(); scott.putBeeper(); scott.move(); scott.putBeeper(); scott.move(); scott.turnLeft(); scott.turnLeft(); scott.move(); scott.move(); scott.move(); scott.move(); scott.move(); scott.turnOff(); Robot jim = new Robot(3, 1, East, 0); jim.move(); jim.pickBeeper(); jim.move(); jim.turnLeft(); jim.putBeeper(); jim.move(); jim.move(); jim.move(); jim.move(); jim.turnOff(); }// end of method main } // end of class Application Part C: 1 point This has things not contained in the reading. Read the program as English and try to figure out what it is doing. Specifications: Draw the following on the sheet marked “final situation”:
package kareltherobot; public class Application implements Directions { public static void main(String [ ] args) { World.setVisible(true); World.readWorld("worldC.txt"); World.setTrace(true); World.setDelay(10); Robot boring = new Robot(3, 1, East, 5); boring.move(); boring.move(); while (boring.anyBeepersInBeeperBag() ) { boring.putBeeper(); boring.move(); } boring.turnLeft(); if (boring.frontIsClear() ) { boring.move(); } while (boring.anyBeepersInBeeperBag() ) { boring.move(); } }// end of method main } // end of class Application Part D: also only 1 point More stuff not from the reading. This one could be tough. Read it, think about it, and give it a try. Specifications: For of the program draw the following on the sheet marked “final situation”:
public class Application implements Directions { Robot confusing = new Robot( 4, 1, East, 42); public static void main(String [ ] args) { World.setVisible(true); World.readWorld("worldD.txt"); World.setTrace(true); World.setDelay(10); Application app = new Application( ); app.putBeeperAndMove(7); app.moveToNextRow(); app.putBeeperAndMove(4); }// end of method main public void putBeeperAndMove(int numberOfBlocksToMove) { int i = 0; while ( i < numberOfBlocksToMove) { confusing.putBeeper(); confusing.move(); i = i + 1; } }// end of method putBeeperAndMove public void moveToNextRow() { confusing.turnLeft(); confusing.move(); confusing.turnLeft(); }// end of method moveToNextRow } // end of class Application |