announcements announcements
play

Announcements Announcements Reading for next Wednesday (Sep 7) - PowerPoint PPT Presentation

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


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

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

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

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

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

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

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

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

  9. 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(); } }

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

  11. 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(); } }

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

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

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

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

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

  17. 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();

  18. Subclass Methods Subclass Methods • turnAround() turnAround() Ri ht() • turnRight() • move3() t

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

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

  21. 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);

  22. 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() { … } }

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

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

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

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

  27. 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); } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend