Introduction to Computer Science Robot Introduction l Programs

Introduction to Computer Science Robot Introduction l Programs as Interacting Objects l Classes, Instances, and Messages l The Robot World l Primitive Instructions and Simple Programs

You ARE NOT ALLOWED To Copy Files!!!

You ARE NOT ALLOWED To Be Told What to Write in the Program

What IS Allowed? l Discussion is allowed, even encouraged – the result of it should be captured in your brains (NOT on paper, nor electronic) l You can talk as much as you want and at any level of detail about a problem, but you are not allowed to take any written material from each other l You are required to report with whom you have discussed the brain draining insurmountable problem (“discussed”, not just fixed a misspelled variable)

You are required to report with whom you have discussed the program l List people with whom you’ve discussed the program in a comment at the beginning of the program

IF SOMEONE COPIES FROM YOU, BOTH OF YOU ARE RESPONSIBLE!

Today: Object Oriented Programming Basics l We start with the fundamentals of Object Oriented Programming l Then, we apply these principles to a particular (robot) programming environment ( “Karel J Robot”)

The World of Objects l The World consists of Objects l Objects are Nouns l. A System of Objects is usually what we want to simulate or implement with Anything we a program on a computer can put a thumb on

Example 1 l Traffic System l Simulate l Objects traffic flow, traffic jams include: u. Cars u. Trucks u. Pedestrians u. Traffic u. The Lights Road itself!

Example 2 l Class Scheduling System l Assign students to classes l Objects include: u. Students u. Classes u. Time slots u. Rooms

Example 3 l Graphical Drawing System l Allow user to draw shapes and manipulate them on the screen l Objects include: u. Circles u. Rectangles u. Lines u. Polygons

Objects have a State — Attributes An attribute is any characteristic of an object

Objects Can Do Things — Methods An object has operations it can perform — built right into it

Objects Can be Sent Messages One object can ask another object for a service, by sending it a message One object asks another to use a particular method

Basic Objects l Objects u Nouns, things in the world l Attributes u Properties each of those things have l Methods u Actions that each of those things can do l Message u Communication from one object to another, asking for a method to be used; the way methods are “triggered”

Example – Bank Accounts Attributes: owner = John Doe balance = $142. 30 kind = Checking Methods: deposit( ) withdraw( ) Attributes: owner = Frank Oz balance = $2301. 47 kind = Savings Methods: deposit( ) withdraw( ) Attributes: owner = alice Budd balance = $29. 88 kind = Checking Methods: deposit( ) withdraw( ) l Bank accounts have a state — attributes, like owner, balance, kind of account l Bank accounts (in OOP) can do things — methods, like deposit( ) and withdraw( ) l Each bank account is represented by an object l Send an object a message to get it to add or subtract money

Let’s Consider Shapes l Shapes have a state — attributes l Shapes can do things — methods l Attributes u Filled, line width, line color, fill color, location l Methods u of a shape: Fill, Empty, Move, Grow

Fun with Shapes Each Shape is an Object l Properties of a shape: – filled – line width – line color – fill color – location Methods of a shape: l – Fill – Empty – Move – Grow

There is a Structure Here l There are certain shapes of a related kind l This prototype is called a Class l Each circle is different, but they are all instances of the class Circle

Each Object is an Instance of a Class l An Instance of the Class “Circle” l Two l An Instances of the Class “Square” Instance of the Class “Line”

Classes A Class is an abstract description of objects having the same attributes and methods l A specific Object is an instance of a Class l A Class is the cookie cutter — An Object is the cookie l Person Class Attributes: Age Height Weight Methods: Move bill Attributes: Age = 47 Height = 177 cm Weight = 68 kg Methods: Move Instance

Many Different Objects from a Single Class bill Attributes: Age = 47 Height = 177 cm Weight = 68 kg Methods: Move Person Class Attributes: Age Height Weight Methods: Move scott Instances Attributes: Age = 52 Height = 182 cm Weight = 75 kg Methods: Move steve Attributes: Age = 50 Height = 180 cm Weight = 71 kg Methods: Move larry Attributes: Age = 54 Height = 179 cm Weight = 67 kg Methods: Move

How Do We Create an Object? l We use a constructor l This takes a Class and creates an Instance of the class, an object, perhaps with certain properties l “Construct an Instance of the Class Person, give it the name “bill”, and make its Age be 47, its Height be 177 cm, and its Weight be 68 kg. ”

How Do We Create an Object? Person Class Attributes: Age Height Weight Methods: Move l Presto! bill Attributes: Age = 47 Height = 177 cm Weight = 68 kg Methods: Move Instance We now have an object “bill”, with certain attributes, and with the method Move l The object “bill” can now be sent the message “Move”

Object Vocabulary l Classes — Prototypes for objects l Objects — Nouns, things in the world l Constructor — Given a Class, the way to create an Object (that is, an Instance of the Class) and initialize it l Attributes — Properties an object has l Methods — Actions that an object can do l Messages — Communication from one object to another, asking for a method to be used

Relates to Alice l Classes — Object Tree, Add object l Objects — Nouns, things in the world l Constructor — Done automatically when add instance l Attributes — Properties tab l Methods — Methods tab l Messages — Parameters, invoking methods

The Robot World N Robot (facing East) wall beeper 8 S t r e e t s W S 7 6 5 4 3 2 1 Origin E 1 2 3 4 5 6 7 8 9 10 11 12 13 Avenues

World Details l Avenues run north/south u. Helpful reminder that Avenues run north/south is that the top of the A points north and the bottom of the v points south. l Streets u. Your run east/west mental note is that you have a mental note on the direction of avenues

Robot Capabilities Any mobile robot u Can move forward u Can turn (left) in place u Can pick beepers up and put into his “beeper bag” u Can remove beeper from his “beeper bag” and put down on current corner

Programs l When we want a Robot to do something, we send it a detailed sequence of messages — how to do it l There exists a Controller that receives, memorizes, and follows instructions l These instructions are called a program

Programming Language l Instructions are given in a special language l It has a vocabulary, punctuation marks, and rules of grammar l The language allows us to write brief and unambiguous programs

Tasks and Situations l. A task is something we want a Robot to do — move to a particular corner, escape from a maze, find a beeper and deposit it on the origin l. A situation is an exact description of the Robot’s world u. What corner are Robots on? u. What directions are they facing? u. What is the location and size of each wall section in the world? u. What is the location of each beeper (including the number of beepers in robots’ beeper bags)?

Initial and Final Situation The initial situation is the starting state of the world: 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13

Initial and Final Situation The initial situation is the starting state of the world (unless told otherwise, assume beeper bag starts empty): 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 5 Beepers in the Beeper Bag 11 12 13

Initial and Final Situation The final situation is the ending state of the world: 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 2 Beepers in the Beeper Bag 11 12 13

The Robot Class l This is the class that can be used to create instances of actual Robots in the robot world Robot l It has the methods: v move v turn. Left v pick. Beeper v put. Beeper v Plus l Let’s others move turn. Left pick. Beeper put. Beeper Plus others look at each of them

move l When a robot executes a move instruction, it moves forward one block l It continues to face the same direction l The robot will not move forward if there is a wall section or boundary wall between himself and the corner he would move to; instead, he executes the move by turning himself off — performing an error shutoff – dies a horrible, miserable death!

Successful move S t r e e t s 5 4 3 2 1 1 2 3 Before move 4 5 Avenues 1 2 3 After move 4 5 Avenues

Execution of move causes Error Shutoff S t r e e t s 5 4 3 2 1 1 2 3 Before move 4 5 Avenues 1 2 3 4 After move: Error Shutoff 5 Avenues

turn. Left l. A robot executes turn. Left by pivoting 90 degrees to the left l. A robot stays on the same street corner l No wall section can block a robot’s turn, so turn. Left cannot cause an error shutoff

turn. Left S t r e e t s 5 4 3 2 1 1 2 3 4 Before turn. Left 5 Avenues 1 2 3 4 After turn. Left 5 Avenues

move and turn. Left l. A robot always starts on a corner, facing north, south, east or west l He can’t move fractions of a block or turn other than 90 degrees, so after move or turn. Left he is still on a corner and still facing north, south, east or west l What about “turn. Right”, which doesn’t exist as a primitive instruction?

pick. Beeper l A robot executes a pick. Beeper by picking up a beeper from the corner he is on and putting it in his bag l If there is no beeper, he performs an error shutoff – dies a horrible, miserable death! l If there is more than one beeper, he randomly picks up one and puts it in his Beeper. Bag l beepers are small; they never block movement.

successful pick. Beeper S t r e e t s 5 4 3 2 1 1 2 3 2 beepers in bag 4 5 Avenues Before pick. Beeper 1 2 3 3 beepers in bag 4 5 Avenues After pick. Beeper

put. Beeper l A robot executes put. Beeper by taking a beeper from Beeperbag and putting it on current street corner l If beeper. Bag is empty, a robot executes put. Beeper by performing an error shutoff – dies a horrible, miserable death!

successful put. Beeper S t r e e t s 5 4 3 2 1 1 2 3 2 beepers in bag 4 5 Avenues Before put. Beeper 1 2 3 4 1 beeper in bag After put. Beeper 5 Avenues

The Robot Class l This class has not only methods, but also has four attributes v Street v Avenue v direction v num-beepers l Each has the obvious meaning Robot Street Avenue direction num. Beepers move turn. Left pick. Beeper put. Beeper others

None of these Attributes or Methods Exist in Isolation l We use the Class Robot to create an instance of a robot l The instance is created using a constructor; that gives the robot a name and sets it up in an initial situation (that is, with initial attributes) l We send messages to the instance, telling it what to do

Creating an Instance Robot Class street avenue direction num. Beepers move turn. Left pick. Beeper put. Beeper others bill street =. . . avenue =. . . direction =. . . num. Beepers =. . . move turn. Left pick. Beeper put. Beeper others Constructor Instance

An Instance of Robot l So let’s say we want to create an instance of Robot, called alice, standing at Street 6, Avenue 2, facing East, with 1 beeper in his bag l We write Robot alice = new Robot(6, 2, East, 1); l Now, to get alice to move, we would write alice. move( ); That is, we send alice a “move” message, and (s)he moves

Constructor Details l Details of the constructor Robot alice = new Robot(6, 2, East, 1); u Robot is what class Object is being created u alice is the reference to this new Object u new Robot(…. . ) actually makes a new Object u 6 the street u 2 the Avenue u East the Direction u 1 How many Beepers I do not know why it is up and then over, instead of using Cartesian (x, y) order

Robot alice = new Robot(6, 2, East, 1); alice 8 alice street = 6 avenue = 2 direction = East num-beepers = 1 move turn. Left pick. Beeper put. Beeper turn. Off 7 6 5 4 3 2 1 1 2 3 1 Beeper in the Beeper Bag

Primitive Instructions and Simple Programs l We get a robot to execute an instruction by sending it a message with the associated action l We execute a program by executing each instruction in the program

A Complete Program We pose a task and exhibit a complete, correct program l We initialize alice , a Robot instance, to start on 2 nd Street and 2 nd Avenue, and transport a beeper from 2 nd Street and 4 th Avenue to 4 th Street and 5 th Avenue l After depositing the beeper, alice moves one block farther north!

Initial and Final Situations S t r e e t s 5 4 3 2 1 1 2 3 4 Initial Situation 5 Avenues 1 2 3 4 Final Situation 5 Avenues

A Robot Program for the Task main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); The actual program alice. move( ); you will enter will be alice. turn. Left( ); written in Java and alice. move( ); have a differences… alice. move( ); alice. put. Beeper( ); alice. move( ); }

A Sample Robot Program import karel. JRobot. jar; // ignore this class Main. Driver { public static void main(String[ ] args) { Karel 00 Sample alice = } } new Karel 00 Sample(2, 2, East, 0); alice. task( ); You will be given the program shown above. Add code to define the task() method in the class Karel 00 Sample.

The Karel 00 Sample Program import karel. JRobot. jar; // ignore this Yellow code you may ignore! class Karel 00 Sample extends Robot { public Karel 00 Sample(int st, int av, Direction dir, int num. Beeper) { super(st, av, dir, num. Beeper); } public void task() { move(); turn. Left(); } } This defines the task() for Karel 00 Sample This makes the Robot move in a square pattern. Try it.

The Karel 00 Sample Program Notice, no Robot name appears! How does the computer/controller know what Robot to apply these actions to? The answer is: The Karel 00 Sample Robot that invokes the task() method will perform the indicated operations.

Executing a Karel Program l Copy the folder Sample. Karel 00 from: K->FOP->Karel. Sample 00 to your Z drive l Open Blue. J Project Sample. Karel 00: Project->open project and browse/select folder l Select Compile l Right Click Main. Driver, select void main(String[ ] args) l click on OK, Wait, Watch and Be amazed!

The Karel 00 Sample Program Try this: In Blue. J, double-click on the Main. Driver box, opening the editor. Add the following line after alice. task(); Karel 00 Sample rabbit = new Karel 00 Sample(4, 4, East, 0); Rabbit. task(); Now execute the program again, and two robots should appear and perform the same task, in different locations.

Program Development Edit program Compile program Execute program Evaluate results

Executing a Karel program l The Controller executes the program u Finds the main method u Sequentially executes each instruction in the main method, in strict top to bottom order The Controller continues executing instructions until all robots commands have been executed or Robot performs an error shutoff. u

Simulating a Robot Program Simulating a robot program means that we execute the program exactly as the Controller, recording each action that takes place l We can hand-simulate a robot program on paper l The ability to simulate robot behavior is an important skill we must acquire.

Simulating our Program (1) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag After Constructor Is Called

Simulating our Program (2) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag

Simulating our Program (3) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag

Simulating our Program (4) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (5) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (6) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (7) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (8) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (9) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); } S t r e e t s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag

Simulating our Program (11) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); S t r e e t s 5 4 3 2 1 } The program is verified. 1 2 3 4 5 Avenues 0 beepers in bag Final Situation

The Form of Robot Programs The Grammar Rules of Robot Programs l The Controller cares a lot about grammar and punctuation rules l The symbols the Controller understands are divided into three classes: u Punctuation Marks (the semicolon — ; ) u Messages (we’ve seen those) u Reserved Words

Reserved Words Used to structure and organize the primitive instructions in the robot language l We have already seen reserved words in our program — main and new l main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); . . .

Delimiters l Matching pairs of { and } are called delimiters l The first { tells the Controller where to start execution l The matching } marks the end of the instructions the Controller will execute.

Semicolons terminate messages (instructions) main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); alice. turn. Off( ); }

Indentation l Indentation makes programs easier to read, so get in the habit. l Delimiters and the part they delimit are indented

Error Shutoffs Executing an error shutoff is equivalent to executing a turn. Off; it is used rather than let a robot continue carrying out tasks after an unexpected situation arose. Error shutoffs can occur after l a move message l a pick. Beeper message l a put. Beeper message

Error Shutoffs To avoid error shutoffs: l Send a robot a move message only when his path is clear l Send a robot a pick. Beeper message only when he is on the same corner as at least one beeper l Send a robot a put. Beeper only when his beeper-bag is not empty

Programming Errors Programming errors can be classified into four broad categories: l Lexical Errors l Syntactic Errors l Execution Errors l Intent Errors

Lexical Errors A lexical error occurs whenever we read the Controller a word not in the vocabulary: main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turngqx. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); Lexical Error }

Syntactic Errors A syntactic error occurs when we use incorrect grammar or punctuation: main Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); Syntactic Error, missing first { }

Syntactic Errors A syntactic error occurs when we use incorrect grammar or punctuation: main Robot alice = new Robot(2, 2, East, 0); alice. move( ); Syntactic Error, alice. pick. Beeper( ); missing ; alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); Realize the compiler is patient, it waits to till the line before indicating the syntax error }

Execution Errors Execution errors occur when the Controller or a robot is unable to execute a method successfully and an error shutoff occurs: main { Robot alice = new Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( ); alice. move( ); An Execution Error will occur (no Beepers in Beeper Bag when put. Beeper command given) }

Intent Errors The worst of all. The robot successfully completes his program, but not his task. An intent error may occur early in a program and lead to an execution error later on. Then, we must trace backward from the instruction that caused an error shutoff, to find the instruction that started the robot in the wrong direction.

Intent Error A simple intent error: main { The final move message was forgotten; the task was not completed. } Ur. Robot alice = new Ur. Robot(2, 2, East, 0); alice. move( ); alice. pick. Beeper( ); alice. move( ); alice. turn. Left( ); alice. move( ); alice. put. Beeper( );

Bugs and Debugging l All types of errors are known as “bugs” l Debugging means removing errors from a program l Lexical and Syntactic errors are discovered at compile time l Execution errors are discovered at run time l Intent find errors can sometimes be hardest to

Program Development Edit program Lexical and Syntactic errors Compile program Execution errors Execute program Evaluate results Intent errors

An Example of Simulation main { Initial Situation Robot scott = new S 5 Robot(2, 4, West, 0); t scott. move( ); r 4 scott. turn. Left( ); e 3 scott. turn. Left( ); e t 2 scott. move( ); s scott. turn. Left( ); 1 scott. move( ); scott. turn. Left( ); 1 2 3 4 5 scott. move( ); Avenues scott. pick. Beeper( ); Task is to find beeper and pick it up. } What happens? How do we fix it?

Another Example of Simulation 8 7 Initial Situation 6 5 4 3 2 1 1 2 3 4 5 6 7 8 Task is to find beeper, pick it up, and bring robot back to starting position (the paper retrieving task). How do we do it?
- Slides: 92