1 / 8

Recitation 7

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.

gizi
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 8, 2010

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

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

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

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

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

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

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

More Related