8
Recitation 7 Recitation 7 October 8, 2010 October 8, 2010

Recitation 7

  • Upload
    gizi

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Recitation 7

Recitation 7Recitation 7October 8, 2010October 8, 2010

Page 2: Recitation 7

Today’s Goals:Today’s Goals:

Use inheritance to specialize behavior of a Use inheritance to specialize behavior of a graphics objectgraphics object

Call superclass constructorsCall superclass constructors

Override toString() methodOverride toString() method

Consider class hierarchies in the context Consider class hierarchies in the context of collectionsof collections

Page 3: Recitation 7

Extending a classExtending a classpublic class AStringDatabase extends AStringHistory implements StringDatabase { public void removeElement (String element) { … } int indexOf (String element) { … } void removeElement (int index) { … } void shiftUp (int startIndex) { … } public boolean member(String element) { … } public void clear() { … } }

Use the Use the extends extends keyword to subclasskeyword to subclass

A subclass may have additional methods A subclass may have additional methods in its interfacein its interface

Page 4: Recitation 7

Invoking a super constructorInvoking a super constructor

Use the Use the super super keyword to call the parent keyword to call the parent constructorconstructor

Must be the first line in your constructorMust be the first line in your constructor

public class 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() {…} … }

}

Page 5: Recitation 7

Overriding the toString() methodOverriding 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”

Page 6: Recitation 7

Recitation SpecificationRecitation Specification Copy classes and interfaces created last weekCopy classes and interfaces created last week

Create a subclass for your previous Line Segment class:Create a subclass for your previous Line Segment class: AVerticalLineSegmentAVerticalLineSegment Create a Create a constructor constructor that initializes your vertical line segment that initializes your vertical line segment

Take in three integers: length, x, and yTake in three integers: length, x, and y

Make sure the vertical line segment’s width cannot be changedMake sure the vertical line segment’s width cannot be changed Override the toString() method to return line propertiesOverride the toString() method to return line properties

Create a Create a main class main class to test a to test a AVerticalLineSegmentAVerticalLineSegment Populate your line segment collection from last class with vertical Populate your line segment collection from last class with vertical

and regular line segments (add at least two of each type)and regular line segments (add at least two of each type) Print out the line segments using println()Print out the line segments using println() Name the class Name the class Recitation7.java Recitation7.java and put it in package and put it in package mainmain Use object editor!Use object editor!

Page 7: Recitation 7

Setup and SubmissionSetup and Submission

Please follow the instructions on the course Please follow the instructions on the course website to set up and submit your projects!website to set up and submit your projects! Set up your project:Set up your project:

http://www.cs.unc.edu/~dewan/comp114/f10/Recitations/makeproject/

Submit an assignment:Submit an assignment: http://www.cs.unc.edu/~dewan/comp114/f10/Recitations/sub

mit/

Ask us if you have *ANY* questions on Ask us if you have *ANY* questions on submissionsubmission

Page 8: Recitation 7

Setting up Object EditorSetting up Object Editor

Recall from lecture:Recall from lecture:

Jar must be on build pathJar must be on build path

Import statement necessary!Import statement necessary! import bus.uigen.ObjectEditor;import bus.uigen.ObjectEditor;

Must call ObjectEditor.edit(yourObject);Must call ObjectEditor.edit(yourObject);