1 / 19

import becker.robots.City; import becker.robots.Direction; import becker.robots.RobotSE;

import becker.robots.City; import becker.robots.Direction; import becker.robots.RobotSE; import becker.robots.Thing; import becker.robots.Wall; public class NoBrainerRobot { public static void main(String[] args) { City ny = new City();

carolbryant
Télécharger la présentation

import becker.robots.City; import becker.robots.Direction; import becker.robots.RobotSE;

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. import becker.robots.City; import becker.robots.Direction; import becker.robots.RobotSE; import becker.robots.Thing; import becker.robots.Wall; publicclass NoBrainerRobot { publicstaticvoid main(String[] args) { City ny = new City(); Thing parcel = new Thing(ny, 1, 6); Wall blockade1 = new Wall(ny,1,4,Direction.WEST); Wall blockade2 = new Wall(ny,1,4,Direction.EAST); Wall blockade3 = new Wall(ny,1,6,Direction.NORTH); Wall blockade4 = new Wall(ny,1,6,Direction.SOUTH); Wall blockade5 = new Wall(ny,1,6,Direction.WEST); RobotSE mark = new RobotSE(ny, 1, 0, Direction.EAST); 0 1 2 3 4 5 6 7 0 1 2 3 ITK 168

  2. import .... publicclass NoBrainerRobot { publicstaticvoid main(String[] args) { .... .... mark.move(); mark.move(); mark.move(); mark.move(); mark.move(); mark.move(); mark.move(); mark.move(); 0 1 2 3 4 5 6 7 ITK 168

  3. Rrobot V2 ITK 168

  4. import becker.robots.*; publicclass RobotV2 extends RobotSE { public RobotV2(City theCity, int aStreet, int anAvenue, Direction aDirection) { super(theCity, aStreet, anAvenue, aDirection); } publicvoid walkAroundToEast() { this.turnRight(); this.move(); this.turnLeft(); this.move(); this.turnLeft(); this.move(); this.turnRight(); } publicvoid walkAroundToWest() { this.turnLeft(); this.move(); this.turnRight(); this.move(); this.turnRight(); this.move(); this.turnLeft(); } } ITK 168

  5. import .... publicclass NoBrainerRobot { publicstaticvoid main(String[] args) { .... mark.move(); mark.move(); mark.move(); mark.walkAroundToEast(); mark.walkAroundToEast(); mark.move(); mark.pickThing(); mark.turnAround(); mark.move(); mark.walkAroundToWest(); mark.walkAroundToWest(); mark.move(); mark.move(); mark.move(); mark.putThing(); } } 0 1 2 3 4 5 6 7 ITK 168

  6. Rrobot V3 If there is a blockade, then walk around; otherwise move along. ITK 168

  7. Branching Condition Statement list 1 T Condition Statement list T F F Statement list 2 ITK 168

  8. The Syntax of if and if-else statements A Boolean expression (logical expression). In Java, there are two possible values: true and false. if ( condition ) { statement list; } Reserved words A reserved word can’t be used as an identifier if ( condition ) { statement list1; } else { statement list2; } Indentation indicates that the statements in the statement list are at the level next to the if-else statement. ITK 168

  9. if – else statement if (Condition) { } else { } // The rest of the // program Statement list 1 Condition Statement list 1 T Could have another if F Statement list 2 Statement list 2 The rest of the program ITK 168

  10. More services provided by Robots ITK 168

  11. import becker.robots.*; publicclass RobotV3 extends RobotV2 { public RobotV3(City theCity, int aStreet, int anAvenue, Direction aDirection) { super(theCity, aStreet, anAvenue, aDirection); } publicvoid moveEast() { if (this.frontIsClear()) { this.move(); } else { this.walkAroundToEast(); } } publicvoid moveWest() { if (this.frontIsClear()) { this.move(); } else { this.walkAroundToWest(); } } If there is a blockade, then walk around; otherwise move along. ITK 168

  12. import .... publicclass UsingRobotV3 { publicstaticvoid main(String[] args) { .... RobotV3 mark = new RobotV3(ny, 1, 0, Direction.EAST); mark.moveEast(); mark.moveEast(); mark.moveEast(); mark.moveEast(); mark.moveEast(); mark.moveEast(); mark.pickThing(); mark.turnAround(); mark.moveWest(); mark.moveWest(); mark.moveWest(); mark.moveWest(); mark.moveWest(); mark.moveWest(); } } 0 1 2 3 4 5 6 7 ITK 168

  13. Measure the kid’s height, h Disney Roller Coaster Ride h < 4 No Yes Ride the roller coaster Boy or Girl? boy girl Give a Mickey Give a Mini Next Stop ITK 168

  14. h = Integer.parseInt( JOptionPane.showInputDialog(“How tall is the kid?”)); // IsBoy = true or false; if (h < 4) { if (IsBoy)// Same asif (IsBoy == true) { System.out.println(“Take a Mickey”); } else { System.out.println(“Take a Mini.”); } } // end true part of if (h < 4) else // else part of if (h < 4) { System.out.println(“You can ride!!”); } // end else part of if (h < 4) System.out.println(“Go to the next stop.”); Disney Ride in Java ITK 168

  15. import .... publicclass UsingRobotV3 { publicstaticvoid main(String[] args) { .... RobotV3 mark = new RobotV3(ny, 1, 0, Direction.EAST); mark.moveEast(); mark.moveEast(); mark.moveEast(); mark.moveEast(); mark.moveEast(); mark.moveEast(); mark.pickThing(); mark.turnAround(); .... } } They are repeated operations 0 1 2 3 4 5 6 7 This is still to specific!! ITK 168

  16. Loops Condition Statement list T F ITK 168

  17. while when there is nothing to pick Condition while (Condition) { Statement list } Statement list T F move to next pos. ITK 168

  18. import .... publicclass UsingRobotV3 { publicstaticvoid main(String[] args) { .... RobotV3 mark = new RobotV3(ny, 1, 0, Direction.EAST); while (!mark.canPickThing()) { mark.moveEast(); } mark.pickThing(); mark.turnAround(); .... } } 0 1 2 3 4 5 6 7 ITK 168

  19. Definite Loop • In programming a definite loop is more welcome. • I.e., number of iterationsisknown before the loop begins, at least the upper bound is known. • I.e., repeat the loop 100 times. • Precisely speaking, there is no definite loop in Java • We will talk about it in Chapter 5 ITK 168

More Related