1 / 92

Introduction to Computer Science

Introduction to Computer Science. Robot Introduction. Programs as Interacting Objects Classes, Instances, and Messages The Robot World 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.

guyhazel
Download Presentation

Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. You ARE NOT ALLOWED To Copy Files!!!

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

  4. What IS Allowed? • Discussion is allowed, even encouraged – the result of it should be captured in your brains (NOT on paper, nor electronic) • 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 • You are required to report with whom you have discussed the brain draining insurmountable problem (“discussed”, not just fixed a misspelled variable)

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

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

  7. 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 ( “Karel J Robot”)

  8. 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

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

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

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

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

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

  14. 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

  15. 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”

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

  17. 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

  18. 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

  19. 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

  20. 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”

  21. Classes Person Attributes: Age Height Weight Methods: Move bill Class Attributes: Age = 47 Height = 177cm Weight = 68 kg Methods: Move Instance • 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

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

  23. 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 47, its Height be 177 cm, and its Weight be 68 kg.”

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

  25. 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

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

  27. 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

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

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

  30. 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

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

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

  33. 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

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

  35. 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 11 12 13 2 Beepers in the Beeper Bag

  36. The Robot Class • This is the class that can be used to create instances of actual Robots in the robot world • It has the methods: • move • turnLeft • pickBeeper • putBeeper • Plus others • Let’s look at each of them Robot move turnLeft pickBeeper putBeeper Plus others

  37. 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 – dies a horrible, miserable death!

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

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

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

  41. 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

  42. 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?

  43. 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, he performs an error shutoff – dies a horrible, miserable death! • If there is more than one beeper, he randomly picks up one and puts it in his BeeperBag • beepers are small; they never block movement.

  44. 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

  45. putBeeper • A robot executes putBeeper by taking a beeper from Beeperbag and putting it on current street corner • If beeperBag is empty, a robot executes putBeeper by performing an error shutoff – dies a horrible, miserable death!

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

  47. The Robot Class • This class has not only methods, but alsohas four attributes • Street • Avenue • direction • num-beepers • Each has the obviousmeaning Robot StreetAvenuedirectionnumBeepers move turnLeft pickBeeper putBeeper others

  48. None of these Attributes or Methods Exist in Isolation • We use the Class Robot 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

  49. Creating an Instance Robot streetavenuedirectionnumBeepers bill Class street = ...avenue = ...direction = ...numBeepers = ... Instance move turnLeft pickBeeper putBeeper others move turnLeft pickBeeper putBeeper others Constructor

  50. An Instance of Robot • 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 • We writeRobot alice = new Robot(6, 2, East, 1); • Now, to get alice to move, we would writealice.move( );That is, we send alice a “move” message, and (s)he moves

More Related