Announcements Announcements • Reading for Wednesday Reading for Wednesday – the rest of Chapter 2 (with a few exceptions) – Check Check http://www.itk.ilstu.edu/faculty/kwsuh/courses/ ITK168/Fall2011/index.htm ITK168/Fall2011/index.htm – http://www.itk.ilstu.edu/itk168 • Quiz • Quiz
Object-oriented vs. non-OO programming • Demo Demo
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
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
Basic Java Patterns Basic Java Patterns • Java Program Pattern Java Program Pattern • Object Instantiation Pattern • Command Invocation Pattern C d I ti P tt • Sequential Execution Pattern
Planting Flowers • You have a square enclosure. You want to have a robot plant flowers (Things) around the enclosure the enclosure. See below. See below Questions: Ho How many walls are there? How are they positioned? man alls are there? Ho are the positioned? Where do the flowers come from?
Beyond Robots Beyond Robots • Objects are used in all contexts in Java Objects are used in all contexts in Java programs. • The same patterns are used to develop • The same patterns are used to develop GUI (Graphical user interface) programs as to develop Robot programs as to develop Robot programs. • However, the classes we use are different.
Sample Simple GUI • Use the JFrame , JLabel , JTextField , and JTextArea classes to produce a program that g creates a window that looks like a browser window (sort of).
End of Chapter End of Chapter • Be sure to study the patterns listed at the Be sure to study the patterns listed at the end of each chapter • You will be expected to understand them • You will be expected to understand them • Likewise, study the concept map at the end of each chapter d f h h t • Download and complete the exercise set posted on the course web site
Extending Classes with Services Extending Classes with Services IT 168 Fall 2011 Robots Learning to Program with Java Learning to Program with Java Byron Weber Becker chapter 2 h
Chapter 2 objectives Chapter 2 objectives • Extend an existing class with new Extend an existing class with new commands • Explain how a message sent to an object • Explain how a message sent to an object is resolved to a particular method • Use inherited services U i h it d i • Override services in the superclass • Java programming convention
Sketch Before and After Sketch Before and After import becker.robots.*; public class Longer p g { public static void main(String[] args) { City austin = new City(); Robot lisa = new Robot(austin, 3, 2, Direction.EAST); li lisa.move(); () lisa.move(); lisa.move(); lisa.turnLeft(); lisa.turnLeft(); lisa.turnLeft(); lisa turnLeft(); lisa.move(); lisa.move(); lisa.move(); lisa.turnLeft(); lisa.turnLeft(); lisa.move(); lisa.move(); lisa.move(); lisa.turnLeft(); lisa.move(); li lisa.move(); () lisa.move(); lisa.turnLeft(); lisa.turnLeft(); } }
Robot vs ExperimentalRobot Robot vs ExperimentalRobot Experimental Robot Robot Robot - street:int - street:int - avenue:int - avenue:int - direction:Direction - direction:Direction - packpack:ThingBag - packpack:ThingBag + Robot(City aCity, int + ExperimentalRobot(City aCity, aStreet, int anAvenue, int aStreet, int anAvenue, Direction aDirection) Direction aDirection) + move():void + mo e() oid + mo e() + move():void oid + turnLeft():void + move3():void + pickThing():void + turnLeft():void + putThing():void + putThing():void + turnRight():void + turnRight():void + turnAround():void + pickThing():void + putThing():void + putThing():void
Recommend
More recommend