An enhanced robot Robot int street int avenue
An enhanced robot Robot int street int avenue Direction direction Thing. Bag back Experiment. Robot(City a. City, int a. Street, int an. Avenue, Direction a. Dir) void move() void turn. Left() void pick. Thing() void put. Thing() Experiment. Robot int street int avenue Direction direction Thing. Bag back = Experiment. Robot(City a. City, int a. Street, int an. Avenue, Direction a. Dir) void turn. Around(); void turn. Right(); void move 3(); 1/28/2008 ITK 168 Experiment. Robot(City a. City, int a. Street, int an. Avenue, Direction a. Dir) void move() void turn. Left() void pick. Thing() void put. Thing() void turn. Around(); void turn. Right(); void move 3();
import becker. robots. *; public class Experiment. Robot extends Robot { // Construct a new Experiment. Robot. public Experiment. Robot(City the. City, int a. Street, int an. Avenue, Direction a. Direction) { super(the. City, a. Street, an. Avenue, a. Direction); } public void turn. Around() { this. turn. Left(); } public void move 3() { this. move(); } public void turn. Right() { this. turn. Around(); this. turn. Left(); } } 1/28/2008 ITK 168 2
import becker. robots. *; public class Deliver. Parcel { 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); Experiment. Robot mark = new Experiment. Robot(prague, 1, 4, Direction. EAST); . . . . k. turn. Around(); k. move 3(); . . . . } } Note: the new robot class must be defined in the same project We can extend City, Thing, and any Classes. . . 1/28/2008 ITK 168 3
A class diagram can be very simple, flexible, and abstract Object Robot Shape {abstract} Experiment. Robot void turn. Around(); void turn. Right(); void move 3(); 1/28/2008 Rectangle Circle Rectangle. Prism Cylinder ITK 168 4
A class diagram can be very simple and abstract implementation details may not necessary Thing Lamb void: turn. On() void: turn. Off 1/28/2008 ITK 168 5
import becker. robots. *; import becker. robots. icons. *; import java. awt. Color; You need to study Documents public class Lamp extends Thing { public Lamp(City a. City, int a. Street, int an. Avenue) { super(a. City, a. Street, an. Avenue); this. turn. Off(); } public void turn. On(){ Color on. Color = new Color(255, 200); Circle. Icon on. Icon = new Circle. Icon(on. Color); on. Icon. set. Size(0. 75); on. Icon. set. Transparency(0. 5); this. set. Icon(on. Icon); } public void turn. Off() { Color off. Color = new Color(0, 0, 0); Circle. Icon off. Icon = new Circle. Icon(off. Color); off. Icon. set. Size(0. 25); this. set. Icon(off. Icon); } } 1/28/2008 ITK 168 6
import becker. robots. *; public class Main { public static void main(String[] args) { // Construct the initial situation. City paris = new City(); Lamp lamp 1 = new Lamp(paris, 1, 1); Lamp lamp 2 = new Lamp(paris, 2, 1); Robot lamp. Mover = new Robot(paris, 1, 0, Direction. EAST); // Turn one lamp on and the other off. lamp 1. turn. On(); lamp 2. turn. Off(); // Use the robot to move one of the lamps. lamp. Mover. move(); lamp. Mover. pick. Thing(); lamp. Mover. move(); lamp. Mover. put. Thing(); lamp. Mover. move(); } } 1/28/2008 ITK 168 7
If a problem is computable, then we can solve it with an algorithm. When we have an algorithm, then we implement with a program. 1/28/2008 ITK 168
An algorithm: A finite sequence of operations that will lead to a solution A step by step operations 1/28/2008 ITK 168
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. 1. Stepwise refinement: 2. that makes our solution 3. 4. 5. 1/28/2008 more logical, easier to understand easier to debug easier to modify easier to analyze more portable ITK 168
Message to the space 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 0 1 2 3 4 5 1/28/2008 ITK 168 11
Write. Bot: a robot that can light Thing 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 We need a robot that can write an H 1/28/2008 write an E ITK 168 write an L write an O 12
enhanced robots Robot int street int avenue Direction direction Thing. Bag backpack void move() void turn. Left() void pick. Thing() void put. Thing() Write. Bot Robot. SE void put. Bonfire() void write. H() void write. E(); void write. L(); void write. O(); void next. Position(); void turn. Around(); void turn. Right(); 1/28/2008 ITK 168
Specifying an operation (service) void put. Bonfire() void write. H() void write. E(); void write. L(); void write. O(); void next. Position(); For each operation: 1. 2. 3. 4. 5. void write. H() void next. Position() operation description pre-conditions post-conditions returns exceptions 1/28/2008 ITK 168 14
import becker. robots. *; public class Write. Bot extends Robot. SE { // Constructor. . . public void put. Bonfire(){ this. put. Thing(); }. . . } How do I know the backpack has Things (Bonfire) to put down? Make it enough in the constructor. . . 1/28/2008 ITK 168 15
import becker. robots. *; public class Write. Bot extends Robot. SE { // Construct a new Write. Bot with 50 Things in // it backpack. Robot. public Write. Bot(City the. City, int avenue, int street, Direction a. Direction) { super(the. City, avenue, street, a. Direction, 50); }. . . } Again, we learn from Documents Constructor Summary Robot(City a. City, int a. Street, int an. Avenue, Direction a. Direction) Construct a new Robot at the given location in the given city with no. Thing in its backpack. Robot(City a. City, int a. Street, int an. Avenue, Direction a. Direction, int num. Things) Construct a new Robot at the given location in the given city with the given number of Things in its backpack. 1/28/2008 ITK 168 16
import becker. robots. *; public class Write. Bot extends Robot. SE {. . . public void next. Position() { this. move(); }. . public void write. H(){ turn. Right(); put. Bonfire(); move(); put. Bonfire(); turn. Left(); move(); turn. Left(); put. Bonfire(); move(); put. Bonfire(); turn. Around(); move(); turn. Right(); move(); put. Bonfire(); move(); turn. Right(); }. . } 1/28/2008 ITK 168 17
import becker. robots. *; public class Message { public static void main(String[] args) { // Set up the initial situation City houston = new City(); Write. Bot mark = new Write. Bot(houston, 1, 1, Direction. EAST); mark. write. H(); mark. next. Position(); mark. write. E(); mark. next. Position(); mark. write. L(); mark. next. Position(); mark. write. O(); } } 1/28/2008 ITK 168 18
import becker. robots. *; public class Message { public static void main(String[] args) { // Set up the initial situation City houston = new City(); Write. Bot mark. H = new Write. Bot(houston, Write. Bot mark. E = new Write. Bot(houston, Write. Bot mark. L 2= new Write. Bot(houston, Write. Bot mark. O = new Write. Bot(houston, 1, 1, 5, 5, 9, 9, 1, 13, 1, 17, Direction. EAST); mark. H. write. H(); mark. E. write. E(); mark. L. write. L(); mark. L 2. write. L(); mark. O. write. O(); } } 1/28/2008 ITK 168 19
Example of refinement Harvesting task 0 1 2 3 4 5 6 0 1 2 2 3 3 4 0 1 2 3 4 5 6 4 5 5 6 6 7 7 This is the field to harvest 1/28/2008 ITK 168 20
Pseudo code 1. 2. 3. 4. for the harvesting task create a harvester (a kind of robot) step in the field harvest the field 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. 1/28/2008 ITK 168 21
import becker. robots. *; /***************************/ /* A program to harvest a 6 by 5 field of things */ /* wide and 6 rows high. */ /***************************/ public class Harvest. Task { public static void main (String[] args) { City st. Louis = new City("Field. txt"); Harvester mark = new Harvester (st. Louis, 1, 0, Direction. EAST); mark. move (); mark. harvest. Field(); mark. move (); } } 1/28/2008 ITK 168 22
Robot int street int avenue Direction direction Thing. Bag backpack Inheritance again! void move() void turn. Left() void pick. Thing() void put. Thing() This harvester is a bit silly, since it can only work on a 6 by 5 field. Robot. SE Harvester void turn. Around(); void turn. Right(); 1/28/2008 void harvest. Field(); ITK 168
Harvesting a 6 by 5 field: Plan A 0 1 2 3 4 5 6 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? 1/28/2008 ITK 168 24
Harvesting a 6 by 5 field: Plan B 0 1 2 3 4 5 6 1. 2. 3. 4. 5. 2 3 4 5 harvest two rows position next row harvest two rows 6 This is a better plan since we do generate fewer subtasks. (there are only two involved) 1/28/2008 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. ITK 168 25
import becker. robots. *; public class Harvester extends Robot. SE { public Harvester(City a. City, int str, int ave, Direction dir) { super(a. City, str, ave, dir); } public void harvest. Field() { this. harvest. Two. Rows(); this. position. For. Next. Harvest(); this. harvest. Two. Rows(); }. . . } 1/28/2008 We now turn to refine the subtasks; there are two. ITK 168 26
import becker. robots. *; public class Harvester extends Robot. SE {. . . . private void position. For. Next. Harvest() { this. turn. Left(); this. move(); 0 1 0 this. turn. Left(); } 1. . 2. . } 3 2 3 4 5 6 1/28/2008 ITK 168 27
import becker. robots. *; public class Harvester extends Robot. SE {. . Refine harvest two rows. protected void harvest. Two. Rows() { this. harvest. One. Row(); this. go. To. Next. Row(); this. harvest. One. Row(); } private void go. To. Next. Row() { this. turn. Right(); this. move(); this. turn. Right(); }. . . } 1/28/2008 ITK 168 28
import becker. robots. *; public class Harvester extends Robot. SE {. . protected void harvest. One. Row() { this. harvest. Intersection(); this. move(); this. harvest. Intersection(); } Refine harvest one row. protected void harvest. Intersection() { this. pick. Thing(); } } 1/28/2008 ITK 168 29
Harvesting with three harvesters public 0 1 2 3 4 5 6 2 static void main(String[] args) { City st. Louis = new City("Field. txt"); Harvester mark = new Harvester(st. Louis, 1, 0, Direction. EAST); Harvester lucy = new Harvester(st. Louis, 3, 0, Direction. EAST); Harvester greg = new Harvester(st. Louis, 5, 0, Direction. EAST); mark. move(); mark. harvest. Two. Rows(); mark. move(); 3 4 5 lucy. move(); lucy. harvest. Two. Rows(); lucy. move(); 6 greg. move(); greg. harvest. Two. Rows(); greg. move(); } 1/28/2008 ITK 168 30
Multi. Tasking in Java 0 1 2 3 4 5 6 public class Sim. Harvester extends Robot. SE implements Runnable {. . . . 2 3 4 5 public void run() { this. move(); this. harvest. Two. Rows(); this. move(); } 6 } 1/28/2008 ITK 168 31
Multi. Tasking in Java 0 1 2 2 3 4 import becker. robots. *; public class Sim. Harvest. Task extends Object { public static void main (String[] args) { 5 City st. Louis = new City("Field. txt"); Sim. Harvester mark = new Sim. Harvester(st. Louis, 1, 0, Direction. EAST); Sim. Harvester tom = new Sim. Harvester(st. Louis, 3, 0, Direction. EAST); Sim. Harvester john = new Sim. Harvester(st. Louis, 5, 0, Direction. EAST); 5 Thread t 1 = new Thread(mark); t 1. start(); Thread t 2 = new Thread(tom); t 2. start(); Thread t 3 = new Thread(john); t 3. start(); 6 } } 1/28/2008 ITK 168 32
- Slides: 32