Exercises

  1. What happens if the autorelease message is not sent to the Segment and Label objects? Will the program still work if the message is taken out?

  2. Make the GraphView draw the graph when a formula is typed and the user hits Return (instead of clicking the Graph button).

  3. Instead of being implemented as a formal protocol, GraphViewElement could have been implemented as an abstract parent class of both Segment and Label. (We didn’t implement the classes with a common parent class because we wanted to show how to create and implement a protocol.) If we had used a common parent class, we could have factored the common code out of the Segment and Label classes and put it into the parent class.

    Reimplement the Segment and Label classes with a common parent class. Explain the advantages and disadvantages of this approach versus the formal protocol approach. Is one of the approaches better than the other? Why or why not? Can you imagine a situation in which you would want to use both a common parent class and a protocol in the same application?

  4. When the width of the bounds does not equal the height, the line drawing is not square — that is, horizontal lines will not have the same thickness as vertical lines. Explain why this is so, and reimplement GraphView and Segment so that drawing is always square. (Hint: Try this using the same coordinate-transformation technique that the Label class uses.)

  5. Our GraphView class needs a dealloc method that properly releases all of the allocated and ...

Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.