Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright, Joseph Bergin

Similar presentations


Presentation on theme: "Copyright, Joseph Bergin"— Presentation transcript:

1 Copyright, Joseph Bergin
Karel J. Robot A Gentle Introduction to the Art of Object-Oriented Programming in Java Copyright, Joseph Bergin

2 Karel's World A flat World Horizontal streets running East-West
Vertical avenues running North-South Infinitely long impenetrable walls run along the South and West boundaries of Karel's world. Karel and other robots like him can move around anywhere North and East of the boundary walls. We will deal with only one robot which we will call Karel. Note. The name Karel is used in recognition of the Czechoslovakian dramatist Karel Capek, who popularized the word robot in his play R.U.R. (Rossum's Universal Robots). The word robot is derived from the Czech word robota, meaning forced labor. Karel J. Robot is an OO adaptation of Richard Pattis’ book Karel the Robot: A Gentle Introduction to the Art of Programming. Intersection 2, 3 Walls

3 Karel's World walls beepers
impenetrable structures that robots can't get through beepers objects which robots can put down, pick up and carry Infinitely long walls can be constructed from sections. Walls can only be placed midway between streets and avenues. Wall sections are used to represent obstacles, such as mountains and lakes, around which robots must navigate. Enclosed rooms, mazes, and other barriers can also be constructed from wall sections. Beepers can be put down at, and picked up from, intersections. There can be any number of beepers on any given intersection. They do not interfere with the movement of robots. Beepers Walls

4 moves to 2,4 (still facing East)
Robot Abilities Move move only in the direction it is facing moves one block at a time, from one intersection to the next robot at 2, 3 facing East moves to 2,4 (still facing East)

5 turns to face North (still at 2,4)
Robot Abilities Turn left  rotate 90 degrees anti-clockwise robot at 2, 4 facing East turns to face North (still at 2,4) Activity: Karel has no instruction for turning right. How can Karel be made to turn right?

6 Beepers can be picked up or put down at 2, 3
Robot Abilities pick up a beeper put down a beeper Beepers can be picked up or put down at 2, 3 Robots have a beeper bag. Beepers are picked up and put into the beeper bag. Beepers can only be picked up from, and put down at, the intersection a robot is currently on. Robots have other capabilities, but we will not discuss them yet.

7 Karel J. Robot Instructions in Java
Robot karel; karel = new Robot(2, 3, East, 1); karel.turnOff(); // indicate that there is a robot called karel // create the robot and describe its initial state as: // karel at corner of 2nd St & 3rd Ave, facing East, 1 beeper in its bag You will write instructions in Java to tell Karel what to do. Statements in Java are terminated by a semicolon. Text after the // characters are called comments. Comments are for humans to read – they explain the Java code. They are not part of the instructions which tell Karel what to do. The general form of the statement which creates a robot and describes its initial state: karel = new Robot(<street>,<avenue>,<facing direction>, <no. of beepers in beeper bag>); // when finished turn the robot off

8 Karel J. Robot Instructions in Java
karel.move(); karel.turnLeft(); karel.pickBeeper(); karel.putBeeper(); // move one block in the facing direction // turn left on the spot // pick up a beeper from current intersection // put down a beeper at current intersection

9 Simple Karel J. Robot Program
// Move robot in a 1 x 1 block square (anti-clockwise) Robot karel; // robot named karel // starting on corner of 2nd St & 3rd Ave, facing // East, with 1 beeper in it's beeper bag karel = new Robot(2, 3, East, 1); karel.move(); karel.turnLeft(); Karel.turnOff(); Where is karel initially? What direction is karel facing? How many beepers does karel have in his beeper bag? What size square will karel move in? Note that the above Java code gives the instructions for a Karel J. Robot program to be run in the JJ environment. It is not a complete Java program.

10 Activity 1 – Check/Run Karel Program
Go to the Karel JJ website: Top left pane contains the karel program of the previous slide. click Checkit to ascertain that the program has been written correctly click Runit to run the program click Start

11 Activity 2 – Square Dance with Beeper
Change the code so that Karel performs the same square dance and puts a beeper at the 3, 4 intersection (i.e. diagonally opposite his starting point). As it is originally written at the website, karel has one beeper in his bag (who knows why?) so the students don’t have to be concerned with changing the initial state. Robot karel; // robot named karel, // starting on the corner of 2nd Street and 3rd Avenue, facing East, with 1 beeper in his beeper bag karel = new Robot(2, 3, East, 1); karel.move(); karel.turnLeft(); karel.putBeeper(); karel.turnOff();

12 Activity 3 – Square Dance with 4 Beepers
How could this code be changed so that Karel performs his square dance and puts beepers at all points of the square? If they forget to increase the number of beepers in the beeper bag at initialisation, the program will stop when the robot runs out of beepers. However, the error functionality under JJ isn't that good (tracing robot actions doesn't work and there is no other obvious indication that a run-time error has occurred – other than Karel not doing what he’s supposed to). Robot karel; // robot named karel, // starting on the corner of 2nd Street and 3rd Avenue, facing East, with 4 beepers in his bag karel = new Robot(2, 3, East, 4); karel.move(); karel.turnLeft(); karel.putBeeper(); karel.turnOff();

13 More Robot Abilities – Is the Robot next to a Beeper?
// determine if there is a beeper on the // corner karel is on if (karel.nextToABeeper()) { // if next-to-a-beeper karel instructions } Example // if karel is next to a beeper, pick it up if (karel.nextToABeeper()) { karel.pickBeeper(); } Assuming that a Robot, karel, has been created and initialised. If you write a program where a robot attempts to pick up a beeper from a corner where there are no beepers, the program will terminate immediately – an error has occurred. The logic of your program would be incorrect. nextToABeeper() can be used to ensure that karel does not attempt to pick up beepers that aren’t there. Any number of Karel instructions can be placed inside the curly braces. Note that the Java if statement has the condition, in this case karel.nextToABeeper(), enclosed in round brackets.

14 Activity 4 – Square Dance and Pick up any Beepers
Write a program in which Karel performs the same square dance and picks up any beepers he finds on corners. To test the program: on Runit screen: click show World Builder (make sure the world screen is visible) Click Beeper add some beepers on corners of the square Click set new world defn // robot named karel Robot karel; // Karel starts on the corner of 2nd Street and 3rd Avenue, facing East, with no beepers in his bag karel = new Robot(2, 3, East, 0); // Karel moves in a square and picks up any beepers he finds karel.move(); karel.turnLeft(); if (karel.nextToABeeper()) { karel.pickBeeper(); } karel.turnOff(); To test the program, create a world for Karel with beepers on some of the corners on which he does his dance.

15 Java if/else Statement
// determine if there is a beeper on the // corner karel is on if (karel.nextToABeeper()) { // next-to-a-beeper karel instructions } else // NOT next-to-a-beeper karel instructions There are two forms of the Java if statement: The one we have covered determines if some condition is true, and if it is, executes the statements inside the curly braces. An if/else statement is similar, except that it has an else part, which is executed if the condition is NOT true. The else part of the above if statement is executed if karel is NOT next to a beeper.

16 Activity 5 – Square Dance and Move Beepers
Write a program in which Karel performs the same square dance and picks up a beeper from any corner on which there is one puts a beeper on any corner on which there is no beeper Hint: How many beepers should Karel initially have in his beeper bag? Note: He does not know how many beepers he will find on corners. // robot named karel Robot karel; // karel, starting on the corner of 2nd Street and 3rd Avenue, // facing East, with 4 beepers in his beeper bag karel = new Robot(2, 3, East, 4); if (karel.nextToABeeper()) { karel.pickBeeper(); } else karel.putBeeper(); karel.move(); karel.turnLeft(); karel.turnOff();

17 Java - repeat instruction Activity 1
When we know how many times that we are repeating, we can use the for loop // repeat activities 4 times for (int i = 0; i < 4; i++) { //insert activity }

18 Java – repeat activity 2 We can also repeat until a certain condition is false // condition here is how many beepers left // in the beeper bag given at the start while (karel.anyBeepersInBeeperBag()) { //activity to be repeated // ensure that you put the beepers } Note new logical function: anyBeepersInBeeperBag()

19 Activity 6 To test the repeat action, write two separate programs that
(1) gets Karel to drop a 4 beeper along a straight line (2) gets Karel to drop a beeper along a straight line, until there are no more beepers left

20 Activity 7 – Escape from Maze
A program which has Karel escape from a maze. Example maze world given in file: maze.txt copy contents into pane below use text world defn click use text world defn Simple version of program in file: maze.jj.txt copy contents into code area Check and run the program What is the program algorithm (design)? Students not meant to be able to write (or understand) this code. requires use of the Java while statement Probably only time to let them see that it works. Robot karel; karel = new Robot(2,2,North,0); // iterate until the beeper found while (!karel.nextToABeeper()) { // see if right clear for (int i = 0; i < 3; i++) karel.turnLeft(); // turn right if (!karel.frontIsClear()) // if not clear // turn back to initial position karel.turnLeft(); // while front is blocked, turn left while (!karel.frontIsClear()) } // either right was clear or turned left until path was clear karel.move(); } // beeper found // pick up beeper and turn karel off karel.pickBeeper(); karel.turnOff();


Download ppt "Copyright, Joseph Bergin"

Similar presentations


Ads by Google