80 likes | 222 Views
Recitation 7. October 8, 2010. Today’s Goals:. Use inheritance to specialize behavior of a graphics object Call superclass constructors Override toString() method Consider class hierarchies in the context of collections. Extending a class.
E N D
Recitation 7 October 8, 2010
Today’s Goals: • Use inheritance to specialize behavior of a graphics object • Call superclass constructors • Override toString() method • Consider class hierarchies in the context of collections
Extending a class public class AStringDatabase extendsAStringHistory implements StringDatabase { publicvoid removeElement (String element) { … } int indexOf (String element) { … } void removeElement (int index) { … } void shiftUp (int startIndex) { … } publicboolean member(String element) { … } publicvoid clear() { … } } • Use the extends keyword to subclass • A subclass may have additional methods in its interface
Invoking a super constructor • publicclass ABoundedPoint extends ACartesianPoint implements BoundedPoint { • Point upperLeftCorner, lowerRightCorner; • public ABoundedPoint (int initX, int initY, • Point initUpperLeftCorner, Point initLowerRightCorner) { • super(initX, initY); • upperLeftCorner = initUpperLeftCorner; • lowerRightCorner = initLowerRightCorner; • } • public Point getUpperLeftCorner() {…} • … • } • } • Use the super keyword to call the parent constructor • Must be the first line in your constructor
Overriding the toString() method • public String toString() { • String retVal = “”; • for (int i = 0; i < size; i++) • retVal += “:” + contents[i]; • return retVal; • } stringSet.toString() “James Dean:John Smith”
Recitation Specification • Copy classes and interfaces created last week • Create a subclass for your previous Line Segment class: • AVerticalLineSegment • Create a constructor that initializes your vertical line segment • Take in three integers: length, x, and y • Make sure the vertical line segment’s width cannot be changed • Override the toString() method to return line properties • Create a main class to test a AVerticalLineSegment • Populate your line segment collection from last class with vertical and regular line segments (add at least two of each type) • Print out the line segments using println() • Name the class Recitation7.java and put it in package main • Use object editor!
Setup and Submission • Please follow the instructions on the course website to set up and submit your projects! • Set up your project: • http://www.cs.unc.edu/~dewan/comp114/f10/Recitations/makeproject/ • Submit an assignment: • http://www.cs.unc.edu/~dewan/comp114/f10/Recitations/submit/ • Ask us if you have *ANY* questions on submission
Setting up Object Editor • Recall from lecture: • Jar must be on build path • Import statement necessary! • import bus.uigen.ObjectEditor; • Must call ObjectEditor.edit(yourObject);