Introduction to Computer Science

Slides:



Advertisements
Similar presentations
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Advertisements

1 of 3 Karel Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Copyright, Joseph Bergin
Nested If Statements While Loops
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The.
Murphy’s Laws. Things are more complex than they seem to be. Things take longer than expected. Things cost more than expected. If something can go wrong,
Chapter 3 Extending the Robot Programming Language.
You ARE NOT ALLOWED To Copy Files!!!. You ARE NOT ALLOWED To Be Told What to Write in the Program.
Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4.
Lecturer: Fintan Costello Welcome to Hdip 001 Introduction to Programming.
Karel The Robot In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3.
Introduction to Computer Science Extending Robots’ Vocabularies –New Methods –Top-Down Design –Multiple Robots Unit 2.
Introduction to a Programming Environment
Robot? What’s a Robot? Introducing Karel-the-Robot.
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
Karel JRobot Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Chapter 5 Conditionally Executing Instructions
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Chapter 1 What is Programming? Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming.
Karel the Robot A Gentle Introduction to the Art of Programming.
Programming Errors Lexical errors – occur whenever Karel reads a word that is not in his vocabulary. Example in English: We are asking directions and instead.
Thanks to Dr. Kris Schindler for this (and all Karel the Robot slides)
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
Georgia Institute of Technology Simulations Barb Ericson Jan 2005.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Compiler Errors Syntax error Lexical Can not resolve/find symbol Can not be applied Execution error Oh wait, a run time error Intent error It ran, but.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Karel – Primitive Instructions
Introduction to Computer Science / Procedural – 67130
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
In the beginning… software
Eric Roberts and Jerry Cain
Copyright © 2008 by Helene G. Kershner
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
Karel J Robot.
Karel – Primitive Instructions
Karel J Robot.
CS 106A, Lecture 2 Programming with Karel
Karel J Robot OOP approach to learning computer science
Karel the Robot – Making Decisions
slides courtesy of Eric Roberts
A Gentle Introduction to the Art of Object Oriented Programming
Karel J Robot OOP approach to learning computer science
Nested If Statements While Loops
CSE 111 Karel the Robot.
Karel – Primitive Instructions
Presentation transcript:

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

Course Mechanics Lectures: Monday/Tuesday, 12:00 – 13:45 and 14:00 – 15:45, Chimia 7 Sections (Dan and Moshe): On Tuesdays (17:00), Wednesdays (12:00, 14:00, and 18:00) and Thursdays (8:00), Sprinzak 29 / 26 / 214 / 217 / 214 Extra Help Problem Sets, Final Exam, Office Hours

Extra Help Course for first year students – learning the basics of Unix on HU computers 3 Fridays: 18.10.02, 25.10.02, 1.11.02 8:00 to 12noon Rothberg Hall (used to be Kaplan A) From 11:00 to 12noon you will have “face-to-face” time with computers in another room

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

Course Mission Statement “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

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

Entering Students’ Prior Knowledge in Computer Science Course is aimed here Number of Students Amount of Background

Entering Students’ Prior Knowledge in Computer Science Course is aimed here Number of Students Amount of Background Will get less out of course

Entering Students’ Prior Knowledge in Computer Science Course is aimed here Number of Students Amount of Background Have to invest more time, but all material is presented Will get less out of course

Today: Object Oriented Programming Basics We start with the fundamentals of Object Oriented Programming Then, we apply these principles to a particular (robot) programming environment In two weeks, we move on to programming in Java

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

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

Example 2 Checkout Counter System Simulate throughput at grocery store Objects include: Customers Cashiers Groceries

Example 3 Class Scheduling System Assign students to classes Objects include: Students Classes Time slots Rooms

Example 4 Graphical Drawing System Allow user to draw shapes and manipulate them on the screen Objects include: Circles Rectangles Lines 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 Objects Attributes Methods Message Nouns, things in the world Attributes Properties each of those things have Methods Actions that each of those things can do Message Communication from one object to another, asking for a method to be used; the way methods are “triggered”

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

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

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

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

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

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

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

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

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

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

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

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

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

Tasks and Situations 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 A situation is an exact description of the Robot’s world What corner are Robots on? What directions are they facing? What is the location and size of each wall section in the world? 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 11 12 13 5 Beepers in the Beeper Bag

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

The BasicRobot Class This is the class that can be used to create instances of actual Robots in the robot world It has five methods: move turnLeft pickBeeper putBeeper turnOff Let’s look at each of them BasicRobot move turnLeft pickBeeper putBeeper turnOff

move When a robot executes a move instruction, it moves forward one block It continues to face the same direction 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

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

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

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

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

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

pickBeeper A robot executes a pickBeeper by picking up a beeper from the corner he is on and putting it in his bag If there is no beeper there, he performs an error shutoff If there is more than one beeper, he randomly picks up one and puts it in the bag beepers are small; they never block movement

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

putBeeper A robot executes putBeeper by taking out beeper from bag and putting it on current street corner If beeper bag is empty, a robot executes putBeeper by performing an error shutoff

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

turnOff When a robot executes a turnOff, he turns himself off and does not execute any more instructions until he is restarted on another task The last message in every robot program must be a turnOff message

The BasicRobot Class This class has not only five methods, but also four attributes x-location y-location direction num-beepers Each has the obvious meaning BasicRobot x-location y-location direction num-beepers move turnLeft pickBeeper putBeeper turnOff

None of these Attributes or Methods Exist in Isolation We use the Class BasicRobot to create an instance of a robot 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) We send messages to the instance, telling it what to do

Creating an Instance x-location y-location direction num-beepers move BasicRobot bill x-location y-location direction num-beepers x-location = ... y-location = ... direction = ... num-beepers = ... Class Instance move turnLeft pickBeeper putBeeper turnOff move turnLeft pickBeeper putBeeper turnOff Constructor

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

BasicRobot bill = new BasicRobot(2, 6, East, 1) x-location = 2 y-location = 6 direction = East num-beepers = 1 8 7 6 5 move turnLeft pickBeeper putBeeper turnOff 4 3 2 1 1 2 3 1 Beeper in the Beeper Bag

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

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

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

A Robot Program for the Task main { BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move; bill.pickBeeper; bill.turnLeft; bill.putBeeper; bill.turnOff; } The actual program you will enter for the targil will be written in Java and have a few minor differences…

A Robot Program for the Task import intro2cs.utils.*; class MoveRobot { public static void main(String[ ] args) { BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move( ); bill.pickBeeper( ); bill.turnLeft( ); bill.putBeeper( ); bill.turnOff( ); } The actual program you will enter for the targil will be written in Java and have a few minor differences…

A Robot Program for the Task import intro2cs.utils.*; class MoveRobot { public static void main(String[ ] args) { BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move( ); bill.pickBeeper( ); bill.turnLeft( ); bill.putBeeper( ); bill.turnOff( ); } The actual program you will enter for the targil will be written in Java and have a few minor differences…

Steps to Nirvana Type the program into a file named MoveRobot.java (case matters; really) Compile it (you’ll learn how in the tirgul) Execute it (you’ll learn how in the tirgul) The program runs

Compilation Program Source code Compilation 1001110110101101 1101110001011010 Machine code for some specific machine

Executing a (Theoretical) Karel Program Turn the Controller on 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!!! The Controller memorizes the program We set up the initial situation (walls and beepers) Push Controller’s “execute-program” button

Executing a Karel program The Controller executes the program Finds the main method Sequentially executes each instruction in the main method, in strict top to bottom order The Controller continues executing instructions until all robots either execute turnOff methods or someone performs an error shutoff

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 We can hand-simulate a robot program on paper We can use a computer simulator The ability to simulate robot behavior is an important skill we must acquire.

Simulating our Program e s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag After Constructor Is Called

Simulating our Program (1) 2 3 4 5 S t r e s main { BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move; bill.pickBeeper; bill.turnLeft; bill.putBeeper; bill.turnOff; } 1 2 3 4 5 Avenues 0 beepers in bag

Simulating our Program (2) e s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag

Simulating our Program (3) e s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (4) e s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (5) e s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (6) e s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (7) e s 5 4 3 2 1 1 2 3 4 5 Avenues 1 beeper in bag

Simulating our Program (8) e s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag

Simulating our Program (9) e s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag

Simulating our Program (10) e s 5 4 3 2 1 1 2 3 4 5 Avenues 0 beepers in bag Final Situation The program is verified.

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

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

Delimiters Matching pairs of { and } are called delimiters The first { tells the Controller where to start execution 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 turnOff does that If the Controller reaches the final }, and not all robots have received turnOff messages, then the Controller executes an error shutoff (the wrong way to finish executing a program)

Semicolons Semicolons terminate messages (instructions) main { BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move; bill.pickBeeper; bill.turnLeft; bill.putBeeper; bill.turnOff; }

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

Error Shutoffs Executing an error shutoff is equivalent to executing a turnOff; it is used rather than let a robot continue carrying out tasks after an unexpected situation arose. Error shutoffs can occur after a move message a pickBeeper message a putBeeper message

Error Shutoffs To avoid error shutoffs: Send a robot a move message only when his path is clear Send a robot a pickBeeper message only when he is on the same corner as at least one beeper Send a robot a putBeeper only when his beeper-bag is not empty Send a robot a turnOff message as the last instruction of the program

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

Lexical Errors A lexical error occurs whenever we read the Controller a word not in the vocabulary: main { BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move; bill.pickBeeper; bill.turngqxLeft; bill.putBeeper; bill.turnOff; } Lexical Error

Syntactic Errors A syntactic error occurs when we use incorrect grammar or punctuation: main BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move; bill.pickBeeper; bill.turnLeft; bill.putBeeper; bill.turnOff; } Syntactic Error, missing first {

Execution Errors Execution errors occur when the Controller or a robot is unable to execute a method successfully and an error shutoff occurs: main { BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move; bill.pickBeeper; bill.turnLeft; bill.putBeeper; } An Execution Error will occur (no turnoff message)

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 { BasicRobot bill = new BasicRobot(2, 2, East, 0); bill.move; bill.pickBeeper; bill.turnLeft; bill.putBeeper; bill.turnOff; } The final move message was forgotten; the task was not completed.

Bugs and Debugging All types of errors are known as “bugs” Debugging means removing errors from a program We’ll learn a lot about debugging…

An Example of Simulation main { BasicRobot scott = new BasicRobot(4, 2, West, 0); scott.move; scott.turnLeft; scott.pickBeeper; scott.turnOff; } Initial Situation S t r e s 5 4 3 2 1 1 2 3 4 5 Avenues Task is to find beeper, pick it up, and turn himself off. 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?