Presentation is loading. Please wait.

Presentation is loading. Please wait.

1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction.

Similar presentations


Presentation on theme: "1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction."— Presentation transcript:

1 1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction aDir) void move() void turnLeft() void pickThing() void putThing() ExperimentRobot ExperimentRobot(City aCity, int aStreet, int anAvenue, Direction aDir) void turnAround(); void turnRight(); void move3(); = ExperimentRobot int street int avenue Direction direction ThingBag backback ExperimentRobot(City aCity, int aStreet, int anAvenue, Direction aDir) void move() void turnLeft() void pickThing() void putThing() void turnAround(); void turnRight(); void move3();

2 1/28/2008ITK 1682 import becker.robots.*; public class ExperimentRobot extends Robot { // Construct a new ExperimentRobot. public ExperimentRobot(City theCity, int aStreet, int anAvenue, Direction aDirection) { super(theCity, aStreet, anAvenue, aDirection); } public void turnAround() { this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); }

3 1/28/2008ITK 1683 import becker.robots.*; public class DeliverParcel { public static void main(String[] args) { // Set up the initial situation City prague = new City(); Thing parcel = new Thing(prague, 1, 2); Robot karel = new Robot(prague, 1, 0, Direction.EAST); ExperimentRobot mark = new ExperimentRobot(prague, 1,4, Direction.EAST);.... k.turnAround(); k.move3();.... } Note: the new robot class must be defined in the same project We can extend City, Thing, and any Classes...

4 1/28/2008ITK 1684 Robot ExperimentRobot void turnAround(); void turnRight(); void move3(); A class diagram can be very simple, flexible, and abstract Object Shape CircleRectangle RectanglePrismCylinder {abstract}

5 1/28/2008ITK 1685 Thing Lamb void: turnOn() void: turnOff A class diagram can be very simple and abstract implementation details may not necessary

6 1/28/2008ITK 1686 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.turnOff(); } public void turnOn(){ Color onColor = new Color(255, 255, 200); CircleIcon onIcon = new CircleIcon(onColor); onIcon.setSize(0.75); onIcon.setTransparency(0.5); this.setIcon(onIcon); } public void turnOff() { Color offColor = new Color(0, 0, 0); CircleIcon offIcon = new CircleIcon(offColor); offIcon.setSize(0.25); this.setIcon(offIcon); } You need to study Documents

7 1/28/2008ITK 1687 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); Lamp lamp2 = new Lamp(paris, 2, 1); Robot lampMover = new Robot(paris, 1, 0, Direction.EAST); // Turn one lamp on and the other off. lamp1.turnOn(); lamp2.turnOff(); // Use the robot to move one of the lamps. lampMover.move(); lampMover.pickThing(); lampMover.move(); lampMover.putThing(); lampMover.move(); }

8 1/28/2008ITK 1688 If a problem is computable, then we can solve it with an algorithm. When we have an algorithm, then we implement with a program.

9 1/28/2008ITK 1689 An algorithm: A finite sequence of operations that will lead to a solution A step by step operations

10 1/28/2008ITK 16810 A big problem can be decomposed into a sequence of small problems. We shall refine our problem into smaller problems step by step until we reach the elementary problem. Stepwise refinement: that makes our solution 1.more logical, easier to understand 2.easier to debug 3.easier to modify 4.easier to analyze 5.more portable

11 1/28/2008ITK 16811 Message to the space 0 1 2 3 4 5 12345689101112137141516171819 0 1 2 3 4 5 12345689101112137141516171819

12 1/28/2008ITK 16812 WriteBot: a robot that can light Thing 0 1 2 3 4 5 12345689101112137141516171819 We need a robot that can write an Hwrite an Ewrite an Lwrite an O

13 1/28/2008ITK 16813 enhanced robots Robot int street int avenue Direction direction ThingBag backpack void move() void turnLeft() void pickThing() void putThing() RobotSE void turnAround(); void turnRight(); WriteBot void putBonfire() void writeH() void writeE(); void writeL(); void writeO(); void nextPosition();

14 1/28/2008ITK 16814 Specifying an operation (service) void putBonfire() void writeH() void writeE(); void writeL(); void writeO(); void nextPosition(); For each operation: 1.operation description 2.pre-conditions 3.post-conditions 4.returns 5.exceptions void writeH() void nextPosition()

15 1/28/2008ITK 16815 import becker.robots.*; public class WriteBot extends RobotSE { // Constructor..... public void putBonfire(){ this.putThing(); }..... } How do I know the backpack has Things (Bonfire) to put down? Make it enough in the constructor...

16 1/28/2008ITK 16816 import becker.robots.*; public class WriteBot extends RobotSE { // Construct a new WriteBot with 50 Things in // it backpackRobot. public WriteBot(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection, 50); }..... } Constructor Summary RobotRobot(City aCity, int aStreet, int anAvenue, Direction aDirection) Construct a new Robot at the given location in the given city with noThing in its backpack.CityDirection RobotRobot(City aCity, int aStreet, int anAvenue, Direction aDirection, int numThings) Construct a new Robot at the given location in the given city with the given number of Things in its backpack.CityDirection Again, we learn from Documents

17 1/28/2008ITK 16817 import becker.robots.*; public class WriteBot extends RobotSE {..... public void nextPosition() { this.move();this.move();this.move(); this.move(); }.... public void writeH(){ turnRight(); putBonfire(); move(); putBonfire(); move(); putBonfire(); move(); putBonfire(); turnLeft(); move(); move(); turnLeft(); putBonfire(); move(); putBonfire(); move(); putBonfire(); move(); putBonfire(); turnAround(); move(); move(); turnRight(); move(); putBonfire(); move(); turnRight(); move(); move(); turnRight(); }.... }

18 1/28/2008ITK 16818 import becker.robots.*; public class Message { public static void main(String[] args) { // Set up the initial situation City houston = new City(); WriteBot mark = new WriteBot(houston, 1, 1, Direction.EAST); mark.writeH(); mark.nextPosition(); mark.writeE(); mark.nextPosition(); mark.writeL(); mark.nextPosition(); mark.writeL(); mark.nextPosition(); mark.writeO(); }

19 1/28/2008ITK 16819 import becker.robots.*; public class Message { public static void main(String[] args) { // Set up the initial situation City houston = new City(); WriteBot markH = new WriteBot(houston, 1, 1,Direction.EAST); WriteBot markE = new WriteBot(houston, 5, 5,Direction.EAST); WriteBot markL = new WriteBot(houston, 9, 9,Direction.EAST); WriteBot markL2= new WriteBot(houston, 1,13,Direction.EAST); WriteBot markO = new WriteBot(houston, 1,17,Direction.EAST); markH.writeH(); markE.writeE(); markL.writeL(); markL2.writeL(); markO.writeO(); }

20 1/28/2008ITK 16820 Example of refinement Harvesting task 0 1 2 3 4 5 6 12 7 34560 0 1 2 3 4 5 6 12 7 34560  This is the field to harvest

21 1/28/2008ITK 16821 Pseudo code for the harvesting task 1.create a harvester (a kind of robot) 2.step in the field 3.harvest the field 4.step out the field This way, we have refined the harvesting task into four smaller tasks, and now we are able to translate the pseudo code into Java.

22 1/28/2008ITK 16822 import becker.robots.*; /*****************************************************/ /* A program to harvest a 6 by 5 field of things */ /* wide and 6 rows high. */ /*****************************************************/ public class HarvestTask { public static void main (String[] args) { City stLouis = new City("Field.txt"); Harvester mark = new Harvester (stLouis, 1, 0, Direction.EAST); mark.move (); mark.harvestField(); mark.move (); }

23 1/28/2008ITK 16823 Inheritance again! Robot int street int avenue Direction direction ThingBag backpack void move() void turnLeft() void pickThing() void putThing() RobotSE void turnAround(); void turnRight(); Harvester void harvestField(); This harvester is a bit silly, since it can only work on a 6 by 5 field.

24 1/28/2008ITK 16824 Harvesting a 6 by 5 field: Plan A 0 1 2 3 4 5 6 1234560 1.harvest a row from west to east 2.move south 3.harvest a row from east to west 4.move south 5.harvest a row from west to east 6.move south 7.harvest a row from east to west 8.move south 9.harvest a row from west to east 10.move south 11.harvest a row from east to west Is this a good plan?

25 1/28/2008ITK 16825 Harvesting a 6 by 5 field: Plan B 0 1 2 3 4 5 6 1234560 1.harvest two rows 2.position next row 3.harvest two rows 4.position next row 5.harvest two rows This is a better plan since we do generate fewer subtasks. (there are only two involved) This way, we have refined the task of harvest field into smaller tasks, and now we are able to translate the pseudo code into Java.

26 1/28/2008ITK 16826 import becker.robots.*; public class Harvester extends RobotSE { public Harvester(City aCity, int str, int ave, Direction dir) { super(aCity, str, ave, dir); } public void harvestField() { this.harvestTwoRows(); this.positionForNextHarvest(); this.harvestTwoRows(); this.positionForNextHarvest(); this.harvestTwoRows(); }..... } We now turn to refine the subtasks; there are two.

27 1/28/2008ITK 16827 import becker.robots.*; public class Harvester extends RobotSE {.... private void positionForNextHarvest() { this.turnLeft(); this.move(); this.turnLeft(); }.... } 0 1 2 3 4 5 6 1234560

28 1/28/2008ITK 16828 import becker.robots.*; public class Harvester extends RobotSE {.... protected void harvestTwoRows() { this.harvestOneRow(); this.goToNextRow(); this.harvestOneRow(); } private void goToNextRow() { this.turnRight(); this.move(); this.turnRight(); }..... } Refine harvest two rows.

29 1/28/2008ITK 16829 import becker.robots.*; public class Harvester extends RobotSE {.... protected void harvestOneRow() { this.harvestIntersection(); this.move(); this.harvestIntersection(); this.move(); this.harvestIntersection(); this.move(); this.harvestIntersection(); this.move(); this.harvestIntersection(); } protected void harvestIntersection() { this.pickThing(); } } Refine harvest one row.

30 1/28/2008ITK 16830 Harvesting with three harvesters 0 1 2 3 4 5 6 1234560 public static void main(String[] args) { City stLouis = new City("Field.txt"); Harvester mark = new Harvester(stLouis,1,0,Direction.EAST); Harvester lucy = new Harvester(stLouis,3,0,Direction.EAST); Harvester greg = new Harvester(stLouis,5,0,Direction.EAST); mark.move(); mark.harvestTwoRows(); mark.move(); lucy.move(); lucy.harvestTwoRows(); lucy.move(); greg.move(); greg.harvestTwoRows(); greg.move(); }

31 1/28/2008ITK 16831 MultiTasking in Java 0 1 2 3 4 5 6 1234560 public void run() { this.move(); this.harvestTwoRows(); this.move(); } public class SimHarvester extends RobotSE implements Runnable {..... }

32 1/28/2008ITK 16832 MultiTasking in Java 0 1 2 3 4 5 6 123450 import becker.robots.*; public class SimHarvestTask extends Object { public static void main (String[] args) { City stLouis = new City("Field.txt"); SimHarvester mark = new SimHarvester(stLouis,1,0,Direction.EAST); SimHarvester tom = new SimHarvester(stLouis,3,0,Direction.EAST); SimHarvester john = new SimHarvester(stLouis,5,0,Direction.EAST); Thread t1 = new Thread(mark); t1.start(); Thread t2 = new Thread(tom); t2.start(); Thread t3 = new Thread(john); t3.start(); }


Download ppt "1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction."

Similar presentations


Ads by Google