Announcements Announcements • For Monday read Becker sections 1 4-1 8 For Monday, read Becker, sections 1.4-1.8 • Fill out the Questionnaire in MS Word format and then email it to me (kwsuh@ilstu edu) by and then email it to me (kwsuh@ilstu.edu) by Friday • http://blackboard.ilstu.edu h //bl kb d il d • Lab 2 – Course Web Site: http://www.itk.ilstu.edu/itk168 – Section Web Site: http://www.itk.ilstu.edu/faculty/kwsuh/courses/ITK168/Fall2011/index.ht m
Chapter 1 Objectives Chapter 1 Objectives • Describe what models are Describe what models are • Describe the relationship between objects and classes • Understand the syntax and semantics of a simple Java program p p g • Write object-oriented programs that simulate robots • Understand and fix errors in programs • Read/use documentation for classes (API) ( )
Modeling with Objects Model Information Operations Concert Who’s performing Sell a ticket Performance date Count tickets sold Which seats are sold Schedule List of task, each with est. Insert or delete a task time Calc est. finish time Restaurant Restaurant Occupied tables Occupied tables Mark a table Mark a table seating occupied Unoccupied tables Mark a table # of seats at each table # o seats at eac tab e unoccupied
Using Models Using Models • A model is a simplified description of A model is a simplified description of something that is used to – Make decisions – Predict future events – Maintain up-to-date information p – Simulate a process – Answer questions • Change over time • Form an abstraction • Provide operations to be performed
Abstraction Abstraction • The key to dealing with complexity The key to dealing with complexity • Focus only on relevant information • Organize details into useful higher-level O i d t il i t f l hi h l l chunks • Eliminate and hide some details • Group similar details together in chunks p g
Using Software Objects to Create Models Create Models • A program is a detailed set of instructions A program is a detailed set of instructions • Java is an object-oriented language – Uses software objects that correspond to an U ft bj t th t d t identifiable entity in the problem
Software Objects Software Objects • Maintain information called attributes Maintain information called attributes • Answer questions based on that information using queries information using queries • Change the information using commands • Queries and commands are collectively called services • Services are provided to clients
Objects Objects • Based on a “template” called a class Based on a template called a class • Have the same attributes and services • Attributes have different values Att ib t h diff t l • Services are the same for all objects of the same class
Class Definitions Class Definitions • This is what we code in a Java program This is what we code in a Java program • Used to create multiple objects – Called instances of the class C ll d i t f th l • Specifies attributes and services • Class – vs – Object – Blueprint – house p – Pattern – dress – Cookie cutter - cookie
Queries and Attributes Queries and Attributes • A query is always answered by the object A query is always answered by the object – True or false – A number A number – A string of characters • Answers are based on the object’s A b d th bj t’ attributes (therefore possible questions are limited) li it d)
Commands Commands • Change the value of an attribute (or Change the value of an attribute (or attributes) to reflect a new reality • Can be visualized using a state of • Can be visualized using a state of change diagram – State of the object before St t f th bj t b f – State of the object after
Class Diagram Class Diagram • Rectangle with 3 sections Rectangle with 3 sections – Name of class – List of attributes List of attributes – List of services • Sometimes class diagrams do not list the attributes – Black-box programming
Modeling Robots Modeling Robots • Book author provides many classes in a Book author provides many classes in a package called – becker.jar • Basic enough to grasp easily • Basic enough to grasp easily • Complex enough to be interesting • Simple enough to be easy to program • Rich enough to demonstrate OOP g concepts
Karel’s World Karel s World • City – rectangular grid City rectangular grid • Streets – run east and west • Avenues – run north and south Avenues run north and south • Streets and avenues start numbering at zero (typical in programming) (typical in programming) • Robots and other objects are placed at the intersections – listed as ordered pairs te sect o s sted as o de ed pa s – (1, 2) = street 1, avenue 2 – Multiple robots and things can occupy the same intersection
Walls and Things Walls and Things • Intersections may be surrounded by walls Intersections may be surrounded by walls • Walls block robots • Intersections may have “things” (left non- I t ti h “thi ” (l ft descript to be identified as needed – paper, box, plant, etc) b l t t ) • Typically, things do not block robots
Robots Robots • Exist to serve clients Exist to serve clients • Perform 4 services – Move Move – Turn – Pick things up Pick things up – Put things down • Answer queries about their location and Answer queries about their location and direction • Respond to commands – such as Respond to commands such as controlling speed
Incomplete Class Diagram Incomplete Class Diagram Robot - street: int - avenue : int - direction: Direction - backpack: ThingBag + Robot(City aCity, int aStreet, int anAvenue, Direction aDirection) + move(): void + turnLeft(): void t L ft() id + pickThing(): void + putThing(): void + putThing(): void
Class Details Class Details • Attributes – Always private (-) Al i t ( ) – Name – Type • Constructors Constructors – Always public (+) – Used to build objects – No return type – Same name as the class S th l – May or may not have parameter list • Default constructor • Special constructor • Services – Public (+), private (-), or protected (#) – Name – Parameter list Parameter list – Return type
Services Services • A robot performs a service only when it is p y invoked by a corresponding message – turnLeft – turns 90 degrees to the left remaining on the same intersection the same intersection • Turning is a safe activity – move – attempts to move forward one intersection still facing in the same direction – pickThing – attempts to pick up a thing from the current intersection – putThing – attempts to put a thing down in the current intersection • Moving picking up and putting down objects are not always • Moving, picking up, and putting down objects are not always safe activities
Messages Messages “dot” Argument list Argument list aka: parameter list karel.move(); semicolon semicolon Reference to the The service to object j execute aka: variable aka: method
Example Program Example Program • Situation Situation – A delivery robot is to pick up a parcel, represented by a Thing at intersection (1 2) represented by a Thing, at intersection (1,2) and deliver it to (2,3) Initial state Final state
The Java Program The Java Program import becker.robots.*; public class DeliverParcel { pub c c ass e e a ce { public static void main(String[] args) { //Set up the initial situation City prague = new City(); Thing parcel = new Thing(prague, 1, 2); g p g(p g , , ); Robot karel = new Robot(prague, 1, 0, Direction.EAST); //Direct the robot to the final situation karel.move(); (); karel.move(); karel.pickThing(); karel.move(); karel.turnLeft(); //start turning right as 3 lefts (); // g g karel.turnLeft(); karel.turnLeft(); //finished turning right karel.move(); karel.putThing(); p g(); karel.move(); } }
Sketch the initial state and final state of this program import becker.robots.*; public class GoAroundRoadBlock p { public static void main(String[] args) { //set up initial situation City ny = new City(); Wall blockAve0 = new Wall(ny, 0, 2, Direction.WEST); Wall blockAve1 = new Wall(ny, 1, 2, Direction.WEST); ll bl k 1 ll( 1 2 i i ) Robot mark = new Robot(ny, 0, 2, Direction.WEST); Robot ann = new Robot(ny, 0, 1, Direction.EAST); //what does mark do here? mark turnLeft(); mark.turnLeft(); mark.move(); mark.move(); mark.turnLeft(); mark.turnLeft(); mark.turnLeft(); mark.move(); //what does ann do here? ann.turnLeft(); ann.turnLeft(); ann.turnLeft(); t L ft() ann.move(); ann.move(); ann.turnLeft(); } }
Reading Documentation Reading Documentation • www.learningwithrobots.com – www.learningwithrobots.com/doc/index.html • http://download.oracle.com/javase/6/docs/api/ or • http://download.oracle.com/javase/7/docs/api/ p j p
Recommend
More recommend