Download presentation
Presentation is loading. Please wait.
1
1 2 3 4 5 6 7 1 2 3 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(); 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); 1 2 3 4 5 6 7 1 2 3 12/1/2018 ITK 168
2
1 2 3 4 5 6 7 import .... public class NoBrainerRobot {
public static void main(String[] args) { .... mark.move(); 1 2 3 4 5 6 7 12/1/2018 ITK 168
3
Rrobot V2 Robot RobotV2 RobotSE void walkAroundToEast();
int street int avenue Direction direction ThingBag backpack void move() void turnLeft() void pickThing() void putThing() RobotV2 void walkAroundToEast(); void walkAroundToWest(); RobotSE void turnAround(); void turnRight(); 12/1/2018 ITK 168
4
import becker.robots.*; public class RobotV2 extends RobotSE {
public RobotV2(City theCity, int aStreet, int anAvenue, Direction aDirection) { super(theCity, aStreet, anAvenue, aDirection); } public void walkAroundToEast() { this.turnRight(); this.move(); this.turnLeft(); this.move(); this.turnRight(); public void walkAroundToWest() { this.turnLeft(); 12/1/2018 ITK 168
5
1 2 3 4 5 6 7 import .... public class NoBrainerRobot {
public static void main(String[] args) { .... mark.move(); mark.walkAroundToEast(); mark.pickThing(); mark.turnAround(); mark.walkAroundToWest(); mark.putThing(); } 1 2 3 4 5 6 7 12/1/2018 ITK 168
6
Rrobot V3 RobotV2 void walkAroundToEast(); void walkAroundToWest(); RobotV3 void moveEast(); void moveWest(); If there is a blockade, then walk around; otherwise move along. 12/1/2018 ITK 168
7
Branching Statement Condition Statement Condition list 1 list
F F Statement list 2 12/1/2018 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. 12/1/2018 ITK 168
9
if – else statement Statement list 1 Statement list 1
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 12/1/2018 ITK 168
10
More services provided by Robots
int street int avenue Direction direction ThingBag backbag +Robot(City aCity, int aStreet, int anAvenue, Direction aDir) +boolean canPickThing() +int countThingsInBackpack() +boolean frontIsClear() +int getAvenue() +Direction getDirection() +String getLabel() +double getSpeed() +int getStreet() + public # protected - private 12/1/2018 ITK 168
11
If there is a blockade, then walk around; otherwise move along.
import becker.robots.*; public class RobotV3 extends RobotV2 { public RobotV3(City theCity, int aStreet, int anAvenue, Direction aDirection) { super(theCity, aStreet, anAvenue, aDirection); } public void moveEast() { if (this.frontIsClear()) { this.move(); else { this.walkAroundToEast(); public void moveWest() { this.walkAroundToWest(); If there is a blockade, then walk around; otherwise move along. 12/1/2018 ITK 168
12
1 2 3 4 5 6 7 import .... public class UsingRobotV3 {
public static void main(String[] args) { .... RobotV3 mark = new RobotV3(ny, 1, 0, Direction.EAST); mark.moveEast(); mark.pickThing(); mark.turnAround(); mark.moveWest(); } 1 2 3 4 5 6 7 12/1/2018 ITK 168
13
Disney Roller Coaster Ride
Measure the kid’s height, h h < 4 No Yes Ride the roller coaster Boy or Girl? boy girl Give a Mickey Give a Mini Next Stop 12/1/2018 ITK 168
14
Disney Ride in Java h = Integer.parseInt(
JOptionPane.showInputDialog(“How tall is the kid?”)); // IsBoy = true or false; if (h < 4) { if (IsBoy)// Same as if (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.”); 12/1/2018 ITK 168
15
This is still to specific!!
import .... public class UsingRobotV3 { public static void main(String[] args) { .... RobotV3 mark = new RobotV3(ny, 1, 0, Direction.EAST); mark.moveEast(); mark.pickThing(); mark.turnAround(); } They are repeated operations 1 2 3 4 5 6 7 This is still to specific!! 12/1/2018 ITK 168
16
Loops Condition Statement list T F 12/1/2018 ITK 168
17
while when there is nothing to pick Statement Condition list
while (Condition) { Statement list } Statement list T F move to next pos. 12/1/2018 ITK 168
18
1 2 3 4 5 6 7 import .... public class UsingRobotV3 {
public static void main(String[] args) { .... RobotV3 mark = new RobotV3(ny, 1, 0, Direction.EAST); while (!mark.canPickThing()) { mark.moveEast(); } mark.pickThing(); mark.turnAround(); 1 2 3 4 5 6 7 12/1/2018 ITK 168
19
Definite Loop In programming a definite loop is more welcome.
I.e., number of iterations is known 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 12/1/2018 ITK 168
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.