Announcements Announcements • Reading for next Wednesday (Sep 7) Reading for next Wednesday (Sep 7) – Chapter 3 – 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#1 • Quiz#1 – Clean up your desk and get read for quiz#1
Recap: Class Details Recap: 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
Recap: Messages Recap: 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
Recap: Reading Documentation Recap: 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
Recap: Basic Java Patterns Recap: 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?
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
Recap: Sketch Before and After Recap: 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
Recap: Sketch Before and After Recap: Sketch Before and After import becker.robots.*; public class Shorter { public static void main(String[] args) p ( g[] g ) { City austin = new City(); ExperimentalRobot lisa = new ExperimentalRobot(austin, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); lisa.move3(); lisa.turnLeft(); lisa.move3(); (); lisa.turnAround(); } }
Abstraction Abstraction • Raising the level of abstraction creates a Raising the level of abstraction creates a more natural programming language – Makes programs easier to write Makes programs easier to write – Allows us to understand programs better – Makes programs easier to debug Makes programs easier to debug – Makes programs easier to modify – Creates reusable code C t bl d
Vocabulary Vocabulary • Extends or inherits Extends or inherits • Superclass or parent class • Subclass or child class S b l hild l • Superclasses (plural) Grandparent or great-grandparent • In a class diagram the Superclass is g p shown above the subclass with arrow pointing from subclass to superclass p g p • This represents an “IS-A” relationship
Extending Robot Extending Robot Robot - street:int - avenue:int - avenue:int - direction:Direction - packpack:ThingBag + Robot(City aCity, int aStreet, int anAvenue, Direction aDirection) + move():void + turnLeft():void + pickThing():void + putThing():void E ExperimentalRobot i t lR b t + ExperimentalRobot(City aCity, int aStreet, int anAvenue, Direction aDirection) + turnAround():void + turnRigth():void + move3():void
Form of an Extended Class Form of an Extended Class import <<importedPackage>>; public class <<ClassName>> extends <<SuperClass>> { <<list of attributes used by this class>> <<list of attributes used by this class>> <<list of constructors for this class>> <<list of services provided by this class>> }
Constructor of Subclass Constructor of Subclass public ExperimentRobot(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } • super refers to the superclass – In this case Robot
this this • Every method has at least one automatic Every method has at least one automatic parameter – The calling object The calling object – Not passed in the parameter list • Inside a method, this represents the Inside a method thi represents the calling object // move the calling object this.move();
Subclass Methods Subclass Methods • turnAround() turnAround() Ri ht() • turnRight() • move3() t
Flow of a Java Program Flow of a Java Program • Write a main method to declare an Write a main method to declare an ExperimentalRobot and do the following: – Move one block Move one block – Turn around – Move 3 blocks Move 3 blocks – Turn left – Move two blocks M t bl k – Turn right
Extension vs Modification Extension vs Modification • The Robot class was created by the The Robot class was created by the author and only the compiled class (byte code) is shared code) is shared – The Robot class is closed for modification • The Robot class is programmed in a way The Robot class is programmed in a way to allow users to extend the class – The Robot class is open for extension Th R b t l i f t i
The Thing Class The Thing Class • Explore the documentation – http://www.learningwithrobots.com/doc/index.html • Change the appearance of a Thing Thing deceptiveThing = new Thing(aCity, 3, 4); WallIcon anIcon = new WallIcon(); WallIcon anIcon new WallIcon(); deceptiveThing.setIcon(anIcon);
In Sec2.3 you are asked to Complete this Cl Class import becker.robots.*; public class Lamp extends Thing{ public Lamp(City myCity public Lamp(City myCity, int street, int ave) { int street int ave) { … } public void turnOn() { … } public void turnOff() { … } }
The Constructor The Constructor • Call to super Call to super • Might add additional information • A street light is off during the day and on at night – you need to decide which should be default • Code it Code it
The Lamp Class The Lamp Class • What does a Thing look like? What does a Thing look like? • What should a Lamp look like? • What should be the difference between a Wh t h ld b th diff b t Lamp that is on and one that is off? • How do you change the appearance of a Thing?
Creating a Color Object Creating a Color Object • public Color (int r int g int b int a) public Color (int r, int g, int b, int a) – Creates an sRGB color with the specified red, green blue and alpha values in the range (0 - green, blue, and alpha values in the range (0 255). – Parameters: Parameters: • r - the red component • g - the green component • b - the blue component • a - the alpha component Color onColor = new Color(255, 255, 200, 150);
Is the Lamp On or Off? Is the Lamp On or Off? public void turnOn() { Color onColor = new Color(255, 255, 200, 150); CircleIcon onIcon = new CircleIcon(onColor); this.setIcon(onIcon); } • How would the turnOff method differ? • How would the turnOff method differ?
The Lamp Class The Lamp Class import java.awt.Color; import becker robots *; import becker.robots. ; import becker.robots.icons.CircleIcon; public class Lamp extends Thing{ public Lamp(City myCity, int ave, int street) { super (myCity, ave, street); this .turnOff (); } public void turnOn() { Color onColor = new Color(255, 255, 200, 150); CircleIcon onIcon = new CircleIcon(onColor); this .setIcon(onIcon); } public void turnOff() { Color offColor = new Color(0, 0, 0); CircleIcon offIcon = new CircleIcon(offColor, .25); this .setIcon(offIcon); } }
Recommend
More recommend