import import becker robots City becker robots Direction
import import becker. robots. City; becker. robots. Direction; becker. robots. Robot. SE; becker. robots. Thing; becker. robots. Wall; public class No. Brainer. Robot { public static void main(String[] args) { City ny = new City(); Thing parcel = new Thing(ny, 1, 6); Wall blockade 1 = new Wall(ny, 1, 4, Direction. WEST); Wall blockade 2 = new Wall(ny, 1, 4, Direction. EAST); Wall blockade 3 = new Wall(ny, 1, 6, Direction. NORTH); Wall blockade 4 = new Wall(ny, 1, 6, Direction. SOUTH); Wall blockade 5 = new Wall(ny, 1, 6, Direction. WEST); Robot. SE mark = new Robot. SE(ny, 1, 0, Direction. EAST); 0 1 2 3 4 5 6 7 0 1 2 9/9/2021 3 ITK 168 1
import. . public class No. Brainer. Robot { public static void main(String[] args) {. . . . mark. move(); 0 1 2 3 mark. move(); 9/9/2021 ITK 168 4 5 6 7 2
Rrobot V 2 Robot int street int avenue Direction direction Thing. Bag backpack void move() void turn. Left() void pick. Thing() void put. Thing() Robot. V 2 Robot. SE void walk. Around. To. East(); void walk. Around. To. West(); void turn. Around(); void turn. Right(); 9/9/2021 ITK 168 3
import becker. robots. *; public class Robot. V 2 extends Robot. SE { public Robot. V 2(City the. City, int a. Street, int an. Avenue, Direction a. Direction) { super(the. City, a. Street, an. Avenue, a. Direction); } public void walk. Around. To. East() { this. turn. Right(); this. move(); this. turn. Left(); this. move(); this. turn. Right(); } public void walk. Around. To. West() { this. turn. Left(); this. move(); this. turn. Right(); this. move(); this. turn. Left(); } } 9/9/2021 ITK 168 4
import. . public class No. Brainer. Robot { public static void main(String[] args) {. . mark. move(); mark. walk. Around. To. East(); 0 mark. walk. Around. To. East(); mark. move(); mark. pick. Thing(); mark. turn. Around(); mark. move(); mark. walk. Around. To. West(); mark. move(); mark. put. Thing(); } 1 2 3 4 5 6 7 } 9/9/2021 ITK 168 5
Rrobot V 3 Robot. V 2 void walk. Around. To. East(); void walk. Around. To. West(); Robot. V 3 If there is a blockade, then walk around; otherwise move along. void move. East(); void move. West(); 9/9/2021 ITK 168 6
Branching Condition T Statement list 1 F F Statement list 2 9/9/2021 ITK 168 7
The Syntax of if and if-else statements Reserved words A reserved word can’t be used as an identifier if ( condition ) { statement list; } A Boolean expression (logical expression). In Java, there are two possible values: true and false. if ( condition ) { statement list 1; } else { statement list 2; } Indentation indicates that the statements in the statement list are at the level next to the if-else statement. 9/9/2021 ITK 168 8
if – else statement if (Condition) { Statement Condition list 1 } else { Could have another if T Statement list 1 F Statement list 2 } // The rest of the // program 9/9/2021 The rest of the program ITK 168 9
More services provided by Robots + public # protected - private Robot int street int avenue Direction direction Thing. Bag backbag +Robot(City a. City, int a. Street, int an. Avenue, Direction a. Dir) +boolean can. Pick. Thing() +int count. Things. In. Backpack() +boolean front. Is. Clear() +int get. Avenue() +Direction get. Direction() +String get. Label() +double get. Speed() +int get. Street() 9/9/2021 ITK 168 10
import becker. robots. *; public class Robot. V 3 extends Robot. V 2 { public Robot. V 3(City the. City, int a. Street, int an. Avenue, Direction a. Direction) { super(the. City, a. Street, an. Avenue, a. Direction); } public void move. East() { if (this. front. Is. Clear()) { this. move(); } else { this. walk. Around. To. East(); } } public void move. West() { if (this. front. Is. Clear()) { If there is a blockade, then this. move(); } walk around; otherwise else { move along. this. walk. Around. To. West(); } } 9/9/2021 ITK 168 11
import. . public class Using. Robot. V 3 { public static void main(String[] args) {. . Robot. V 3 mark = new Robot. V 3(ny, 1, 0, Direction. EAST); mark. move. East(); mark. pick. Thing(); mark. turn. Around(); mark. move. West(); 0 1 2 3 4 5 6 7 } } 9/9/2021 ITK 168 12
Disney Roller Coaster Ride Measure the kid’s height, h h<4 No Yes boy Boy or Girl? Give a Mickey girl Ride the roller coaster Give a Mini Next Stop 9/9/2021 ITK 168 13
Disney Ride in Java h = Integer. parse. Int( JOption. Pane. show. Input. Dialog(“How tall is the kid? ”)); // Is. Boy = true or false; if (h < 4) { if (Is. Boy)// Same as if (Is. Boy == 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. ”); 9/9/2021 ITK 168 14
import. . public class Using. Robot. V 3 { public static void main(String[] args) {. . Robot. V 3 mark = new Robot. V 3(ny, 1, 0, Direction. EAST); mark. move. East(); mark. pick. Thing(); mark. turn. Around(); . . They are repeated operations 0 1 2 3 4 5 6 7 } } This is still to specific!! 9/9/2021 ITK 168 15
Loops Condition T Statement list F 9/9/2021 ITK 168 16
while when there is nothing to pick Condition T Statement list while (Condition) { Statement list } F move to next pos. 9/9/2021 ITK 168 17
import. . public class Using. Robot. V 3 { public static void main(String[] args) {. . Robot. V 3 mark = new Robot. V 3(ny, 1, 0, Direction. EAST); while (!mark. can. Pick. Thing()) { mark. move. East(); } mark. pick. Thing(); mark. turn. Around(); . . 0 1 2 3 4 5 6 7 } } 9/9/2021 ITK 168 18
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 9/9/2021 ITK 168 19
- Slides: 19