120 likes | 127 Views
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:.
E N D
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: • Use inheritance and overriding to specialize the behavior of variable-sized collections of graphics objects • Call superclass constructors • More test-first programming
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
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
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; } . . . }
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:”
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
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
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.)
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.)
Test extra challenge To test each part of the extra challenge, uncomment the appropriate line in main() Test each component individually before trying TransparentStackSwapperTester