930 likes | 1.07k Views
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
E N D
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 onlinehttp://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 JavaKamin, 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
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 • 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 ShapesEach 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 Instance Class
bill Attributes: Age = 41 Height = 177 cm Weight = 68 kg Methods: Move steve Attributes: Age = 43 Height = 180 cm Weight = 71 kg Methods: Move larry scott Attributes: Age = 47 Height = 179 cm Weight = 67 kg Methods: Move Attributes: Age = 55 Height = 182 cm Weight = 75kg Methods: Move Many Different Objects from a Single Class Person Attributes: Age Height Weight Methods: Move Class 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.”
bill Attributes: Age = 41 Height = 177 cm Weight = 68 kg Methods: Move How Do We Create an Object? Person • 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” Attributes: Age Height Weight Methods: 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 e t 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 S t r e e t s S t r e e t s 5 5 4 4 3 3 2 2 1 1 1 2 3 4 5 1 2 3 4 5 Avenues Avenues After move Before move
Execution of move causesError Shutoff S t r e e t s S t r e e t s 5 5 4 4 3 3 2 2 1 1 1 2 3 4 5 1 2 3 4 5 Avenues Avenues After move: Error Shutoff Before move
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 S t r e e t s S t r e e t s 5 5 4 4 3 3 2 2 1 1 1 2 3 4 5 1 2 3 4 5 Avenues Avenues After turnLeft Before 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 S t r e e t s S t r e e t s 5 5 4 4 3 3 2 2 1 1 1 2 3 4 5 1 2 3 4 5 Avenues Avenues 3 beepers in bag 2 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 S t r e e t s S t r e e t s 5 5 4 4 3 3 2 2 1 1 1 2 3 4 5 1 2 3 4 5 Avenues Avenues 1 beeper in bag 2 beepers 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 alsofour attributes • x-location • y-location • direction • num-beepers • Each has the obviousmeaning BasicRobot x-locationy-locationdirectionnum-beepers move turnLeft pickBeeper putBeeper turnOff