Lab 1 City, Robot, Thing, Wall
Documentation of Classes and Methods
City Class Constructs a city that the robot will move in it. City newYork = new City();
Robot Class Constructs a Robot to move in the city. Robot hal = new Robot(newYork, 1, 2, Direction.SOUTH);
Thing Class Constructs a thing that can be put in a city. Thing t1 = new Thing(newYork, 2, 2);
Wall Class Constructs walls in the city to block robots. Wall wa = new Wall(newYork, 3, 3, Direction.WEST);
Methods in Robot Class
Methods in Thing Class
Classes in becker Library
Example 1 package rob; import becker.robots.*; public class Rob extends Object { public static void main(String[] args) { // Declare the objects needed City newYork = new City(); Robot hal = new Robot(newYork, 1, 2, Direction.SOUTH); Thing t1 = new Thing(newYork, 2, 2); Wall wa = new Wall(newYork, 3, 3, Direction.WEST); Thing t2 = new Thing(newYork, 3, 2); Thing t3 = new Thing(newYork, 4, 2); hal.move(); hal.pickThing(); hal.move(); hal.pickThing(); hal.move(); hal.pickThing(); hal.move(); hal.putThing(); hal.move(); }
Example 2 package rob; import becker.robots.*; import becker.robots.icons.*; import java.awt.Color; public class Lamp extends Thing {public Lamp(City aCity, int aStreet, int anAvenue) {super(aCity, aStreet, anAvenue); this.turnOn(); } public void turnOn() { Color onColor = new Color(255, 255, 200); CircleIcon onIcon = new CircleIcon(onColor); onIcon.setSize(0.75); this.setIcon(onIcon); } public void turnOff() { Color offColor = new Color(0, 0, 0); CircleIcon offIcon = new CircleIcon(offColor); this.setIcon(offIcon); }
Cont.. Example 2 package rob; import becker.robots.*; public class Main { public static void main(String[] args) { // Construct the initial situation. City paris = new City(); Lamp lamp1 = new Lamp(paris, 1, 1); Robot lampMover = new Robot(paris, 1, 0, Direction.EAST); // Use the robot to move one of the lamps. lampMover.move(); lampMover.pickThing(); lampMover.move(); lampMover.putThing(); lampMover.move(); lamp1.turnOff(); }