Introduction to Computer Science Unit 1 l Programs













































































































- Slides: 109

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

Two Courses, One Goal l 67130 starts with Procedural Programming, moves in Week 5 to Object Oriented Programming l 67101 starts with Object Oriented Programming l 67130 has one extra hour of lecture each week, and at least one extra targil at beginning l The two share targilim from middle of courses 67130 l Same Final Exams in both courses! 67101 1 - 2

Entering Students’ Prior Knowledge in Computer Science Number of Students Amount of Background 1 - 3

Entering Students’ Prior Knowledge in Computer Science Previous Years: Course was always aimed here Number of Students Amount of Background 1 - 4

Entering Students’ Prior Knowledge in Computer Science Previous Years: Course was always aimed here Number of Students Amount of Background Too much time spent on known material 1 - 5

Entering Students’ Prior Knowledge in Computer Science Previous Years: Course was always aimed here Number of Students Course moved very fast, not enough time on fundamentals Amount of Background Too much time spent on known material 1 - 6

This Year: 2 Courses, Each Tuned to Background 67130 67101 Number of Students More time spent on programming basics Amount of Background Move directly to object oriented programming 1 - 7

Again: Two Courses, One Goal l The two share targilim from middle of courses 67130 l Same 67101 Final Exams in both courses! l The goal is to have students at the same point for DAST, 2 nd Semester 1 - 8

Course Mission Statement “To give the student the tools to develop correct, efficient, wellstructured, and stylish programs, and to build a foundation for further studies in Computer Science. ” -An Introduction to Computer Science with Java Kamin, Mickunas, and Reingold 1 - 9

Course Mission Statement “To give the student the tools to develop correct, efficient, wellstructured, and stylish programs, and to build a foundation for further studies in Computer Science. ” -An Introduction to Computer Science with Java Kamin, Mickunas, and Reingold 1 - 10

Division of Units Between Course Goals (roughly ½ and ½ in lecture time) 3 2 1 14 6 5 4 8 7 10 15 11 16 18 9 17 12 19 20 13 “To give the student the tools to develop correct, efficient, well-structured, and stylish programs, and to build a foundation for further studies in Computer Science. ” -An Introduction to Computer Science with Java Kamin, Mickunas, and Reingold 1 - 11

Course Mechanics l All information can be found at: http: //www. cs. huji. ac. il/~intro 2 cs l Lectures, Feldman Bet: u Monday: 14: 00 – 14: 45 u Tuesday: 14: 00 – 15: 45 l Tirgulim: Tuesdays (18: 00), or Wednesdays (14: 00 or 16: 00), or Thursdays (15: 00) – check rooms online l TAs: Ezra and Lior do tirgulim. Simon is in charge of targilim graders. l Dana will teach part of course l Extra Help (lab assistants) l Problem Sets, Final Exam, Office Hours 1 - 12

Resources l Slides, can be bought in the Acadamon, and viewed online http: //www. cs. huji. ac. il/~intro 2 cs l Books u. An Introduction to Computer Science with Java, Kamin, Mickunas, and Reingold u(“Karel++” and “Karel”, Bergin, Stehlik, Roberts, Pattis) 1 - 13

1 - 14

Course Structure Each layer of the course has a different role High-level concepts, overall structure Lectures Detailed coverage of concepts, examples Tirgulim Very detailed problems, practical solutions Targilim YOU! 1 - 15

On Problem Sets (and Exams) NO COPYING! 1 - 16

On The Information Sheet You Received Today “Every student must solve the targil on their own. ” ". "על כל סטודנט לפתור את התרגיל בעצמו 1 - 17

You ARE NOT ALLOWED To Copy Files!!! 1 - 18

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

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 targil (“discussed”, not just fixed a misspelled variable) 1 - 20

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

IF SOMEONE COPIES FROM YOU, BOTH OF YOU ARE RESPONSIBLE! 1 - 22

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 (based on Richard Pattis’ book “Karel the Robot”) l After four units, we move on to programming in Java 1 - 23

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 1 - 24

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! 1 - 25

Example 2 l Checkout l Simulate l Objects Counter System throughput at grocery store include: u. Customers u. Cashiers u. Groceries 1 - 26

Example 3 l Class Scheduling System l Assign students to classes l Objects include: u. Students u. Classes u. Time slots u. Rooms 1 - 27

Example 4 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 1 - 28

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

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

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 1 - 31

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” 1 - 32

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 = Bill 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 1 - 33

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 1 - 34

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 1 - 35

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 1 - 36

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” 1 - 37

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 1 - 38

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 1 - 39

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. ” 1 - 40

How Do We Create an Object? Person Class Attributes: Age Height Weight Methods: Move bill Attributes: Age = 47 Height = 177 cm Weight = 68 kg Methods: Move Instance l Presto! 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” 1 - 41

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 1 - 42

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

Robot Capabilities Any mobile robot u. Can move forward u. Can turn in place u. Can pick up and put down beepers into his “beeper bag” 1 - 44

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 1 - 45

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 1 - 46

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

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 1 - 48

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 11 12 13 5 Beepers in the Beeper Bag 1 - 49

Initial and Final Situation The final situation is the ending state of the world (Robot turns itself off): 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 2 Beepers in the Beeper Bag 1 - 50

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

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 1 - 52

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 1 - 53

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 1 - 54

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 1 - 55

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 1 - 56

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? 1 - 57

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 there, he performs an error shutoff l If there is more than one beeper, he randomly picks up one and puts it in the bag l beepers are small; they never block movement 1 - 58

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 4 3 beepers in bag 5 Avenues After pick. Beeper 1 - 59

put. Beeper l. A robot executes put. Beeper by taking out beeper from bag and putting it on current street corner l If beeper bag is empty, a robot executes put. Beeper by performing an error shutoff 1 - 60

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 1 beeper in bag 4 5 Avenues After put. Beeper 1 - 61

turn. Off l When a robot executes a turn. Off, he turns himself off and does not execute any more instructions until he is restarted on another task l The last message in every robot program must be a turn. Off message 1 - 62

The Basic. Robot Class l This class has not only five methods, but also four attributes v x-location v y-location v direction v num-beepers l Each has the obvious meaning Basic. Robot x-location y-location direction num-beepers move turn. Left pick. Beeper put. Beeper turn. Off 1 - 63

None of these Attributes or Methods Exist in Isolation l We use the Class Basic. 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 1 - 64

Creating an Instance Basic. Robot Class x-location y-location direction num-beepers move turn. Left pick. Beeper put. Beeper turn. Off bill x-location =. . . y-location =. . . direction =. . . num-beepers =. . . move turn. Left pick. Beeper put. Beeper turn. Off Constructor Instance 1 - 65

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

Basic. Robot bill = new Basic. Robot(2, 6, East, 1); bill 8 bill x-location = 2 y-location = 6 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 1 - 67

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 1 - 68

A Complete Program We pose a task and exhibit a complete, correct program l We initialize bill, a Basic. 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, bill moves one block farther north and turns himself off 1 - 69

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 5 Avenues Final Situation 1 - 70

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

A Robot Program for the Task import intro. utils. *; class Move. Robot { public static void main(String[ ] args) { Basic. Robot bill = new Basic. Robot(2, 2, East, 0); bill. move( ); bill. pick. Beeper( ); The actual program bill. move( ); you will enter for the bill. turn. Left( ); targil will be written in bill. move( ); Java and have a few bill. move( ); minor differences… bill. put. Beeper( ); bill. move( ); bill. turn. Off( ); } } 1 - 72

A Robot Program for the Task import intro. utils. *; class Move. Robot { public static void main(String[ ] args) { Basic. Robot bill = new Basic. Robot(2, 2, East, 0); bill. move( ); bill. pick. Beeper( ); The actual program bill. move( ); you will enter for the bill. turn. Left( ); targil will be written in bill. move( ); Java and have a few bill. move( ); bill. put. Beeper( ); minor differences… bill. move( ); bill. turn. Off( ); } } 1 - 73

Steps to Nirvana l Type the program into a file named Move. Robot. java (case matters; really) l Compile u javac Move. Robot. java l Execute u it (you’ll learn how in the tirgul) java Move. Robot l The program runs 1 - 74

Program Development Edit program Compile program Execute program Evaluate results 1 - 75

Compilation Program Source code Compilation 10011101101110001011010 Machine code for some specific machine 1 - 76

Compilation and Execution in Java public class Hello { public static void main. . . Source code (Hello. java file) Compilation: javac Hello. java op 7 op 342 op 213 op 431 Interpreter Byte code (Hello. class file) Execution: java Hello 1 - 77

Executing a (Theoretical) Karel Program l Turn the Controller on l Read in the program, including each word and punctuation mark (the indentation is only for our benefit — the Controller doesn’t hear it) — BUT CASE MATTERS!!! l The Controller memorizes the program l We set up the initial situation (walls and beepers) l Push Controller’s “execute-program” button 1 - 78

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 u The Controller continues executing instructions until all robots either execute turn. Off methods or someone performs an error shutoff 1 - 79

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

Simulating our Program (1) main { Basic. Robot bill = new Basic. Robot(2, 2, East, 0); bill. move( ); bill. pick. Beeper( ); bill. move( ); bill. turn. Left( ); bill. move( ); bill. put. Beeper( ); bill. move( ); bill. 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 1 - 81

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

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

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

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

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

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

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

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

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

Simulating our Program (11) main { Basic. Robot bill = new Basic. Robot(2, 2, East, 0); bill. move( ); bill. pick. Beeper( ); bill. move( ); bill. turn. Left( ); bill. move( ); bill. put. Beeper( ); bill. move( ); bill. turn. Off( ); 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 1 - 91

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 1 - 92

Reserved Words l 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 main { Basic. Robot bill = new Basic. Robot(2, 2, East, 0); bill. move( ); . . . 1 - 93

Delimiters 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, but it does not tell the Controller to stop execution — the final message turn. Off does that l If the Controller reaches the final }, and not all robots have received turn. Off messages, then the Controller executes an error shutoff (the wrong way to finish executing a program) l 1 - 94

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

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

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 1 - 97

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 l Send a robot a turn. Off message as the last instruction of the program 1 - 98

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

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

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

Execution Errors Execution errors occur when the Controller or a robot is unable to execute a method successfully and an error shutoff occurs: main { Basic. Robot bill = new Basic. Robot(2, 2, East, 0); bill. move( ); bill. pick. Beeper( ); bill. move( ); bill. turn. Left( ); bill. move( ); bill. put. Beeper( ); bill. move( ); An Execution Error will occur (no turnoff message) } 1 - 102

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. 1 - 103

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

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 can be discovered when (trying to) compile l Execution errors can be discovered when (trying to) run the program l Intent errors can sometimes be hardest to find 1 - 105

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

An Example of Simulation main { Initial Situation Basic. Robot scott = new Basic. Robot(4, 2, West, 0); S 5 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, pick it up, scott. turn. Off( ); and turn himself off. } What happens? How do we fix it? 1 - 107

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? 1 - 108

Another Example of Simulation Task is to find beeper, pick it up, and bring robot back to starting position (the paper retrieving task). How do we do it? 1 - 109