Presentation is loading. Please wait.

Presentation is loading. Please wait.

Karel J. Robot A Gentle Introduction to the Art of Object-Oriented Programming in Java html.

Similar presentations


Presentation on theme: "Karel J. Robot A Gentle Introduction to the Art of Object-Oriented Programming in Java html."— Presentation transcript:

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

2 Karel's World A flat World Horizontal streets running East-West Vertical avenues running North-South Intersection 2, 3 Walls

3 Karel's World walls impenetrable structures that robots can't get through beepers objects which robots can put down, pick up and carry Walls Beepers

4 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 Eastmoves to 2,4 (still facing East)

5 Robot Abilities Turn left  rotate 90 degrees anti-clockwise robot at 2, 4 facing Eastturns to face North (still at 2,4)

6 Robot Abilities pick up a beeper put down a beeper Beepers can be picked up or put down at 2, 3

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 // 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.move(); karel.turnLeft(); karel.move(); karel.turnLeft(); karel.move(); karel.turnLeft(); Karel.turnOff();

10 Activity 1 – Check/Run Karel Program Go to the Karel JJ website: bin/sfjj.cgi?freeschool=KarelJRobot&file=intr oToKarel.java bin/sfjj.cgi?freeschool=KarelJRobot&file=intr oToKarel.java 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).

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?

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(); }

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

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 }

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.

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)?


Download ppt "Karel J. Robot A Gentle Introduction to the Art of Object-Oriented Programming in Java html."

Similar presentations


Ads by Google