1 / 12

Recitation 7

Recitation 7. October 7, 2011. As you come in. Please sit next to someone with whom you will collaborate for today’s recitation. Choose someone with whom you have NOT collaborated during a previous recitation. Today’s Goals:.

mbiller
Download Presentation

Recitation 7

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. Recitation 7 October 7, 2011

  2. As you come in . . . Please sit next to someone with whom you will collaborate for today’s recitation. Choose someone with whom you have NOT collaborated during a previous recitation.

  3. Today’s Goals: • Use inheritance and overriding to specialize the behavior of variable-sized collections of graphics objects • Call superclass constructors • More test-first programming

  4. Properties of Inheritance All objects in Java have exactly one immediate superclass (Object if none specified) Subclasses inherit all constructors, methods, variables, and constants from their parent classes Subclasses can override methods from any superclass by defining a method with the same signature

  5. Extending a class public classAStringDatabaseextendsAStringHistoryimplementsStringDatabase { publicvoidremoveElement (String element) { … } intindexOf (String element) { … } voidremoveElement (int index) { … } voidshiftUp (intstartIndex) { … } publicboolean member(String element) { … } publicvoid clear() { … } } • Use the extends keyword to subclass • A subclass may have additional methods and implement extra interfaces

  6. Invoking a super constructor • Use the super keyword to call the parent constructor • Must be the first line in your constructor public classAStringDatabaseextendsAStringHistoryimplementsStringDatabase { booleanprintWhenBeyondCapacity; // print when user tries to add to a full database publicAStringDatabase(intmaxCapacity, booleanprintWhenPastCapacity) { super(maxCapacity); printWhenBeyondCapacity = printWhenPastCapacity; } . . . }

  7. Overriding the toString() method • public String toString() { • String retVal = “”; • for (inti = 0; i < size; i++) • retVal += contents[i] + “:”; • returnretVal; • } stringDatabase.toString()  “James Dean:John Smith:”

  8. Recitation Specification • Download Recitation7.zip from the Recitations page • Implement TriangleStack by extending GeneralObjectStack • TriangleStack should have: • A constructor that takes no parameters and simply calls the superclass constructor • A constructor that takes an integer parameter (indicating the desired maximum capacity) and calls the superclass constructor that takes an int • A method called “booleanisTriangle(Object obj)” that returns true if the provided Object is an instance of (i.e. implements the interface) Triangle. Note: this method is not public – it is default. • An overridden version of push(Object obj). If the provided object is a Triangle (try calling isTriangle), then the superclass’s version of push is called on obj. • Your code works if you can successfully run Recitation7.java

  9. Extra challenge After getting checked off for the previous part, implement: AlignedTriangleStack & CorneredTriangleStack Download Extra.zip from the Recitations page, open it, and CLICK & DRAG the files into the appropriate packages

  10. CorneredTriangleStack Create a constructor that takes no parameters and calls the superclass constructor with the constant CAPACITY Whenever a Triangle is pushed onto a non-empty stack, the x and the y of the added triangle are set to be the same as the x and the y of the top triangle on the stack. (Try to reuse as many methods as possible.)

  11. AlignedTriangleStack Create a constructor that takes no parameters and calls the superclass constructor with the constant CAPACITY Whenever a Triangle is pushed onto a non-empty stack, the x is set to be the x of the top triangle, and the y is set to be the y of the bottom of the top triangle. (Try to reuse as many methods as possible.)

  12. Test extra challenge To test each part of the extra challenge, uncomment the appropriate line in main() Test each component individually before trying TransparentStackSwapperTester

More Related