Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
| 1: | Add a new class called ClassC, which is a subclass of ClassB, to Program 8.1. Make an initVar method that sets the value of its instance variable x to 300. Write a test routine that declares ClassA, ClassB, and ClassC objects and invokes their corresponding initVar methods. |
| 2: | When dealing with higher-resolution devices, you might need to use a coordinate system that enables you to specify points as floating-point values, rather than as simple integers. Modify the Point and Rectangle classes from this chapter to deal with floating-point numbers. The rectangle's width, height, area, and perimeter should all work with floating-point numbers as well. |
| 3: | Modify Program 8.1 to add a new class called ClassB2 that, like ClassB, is a subclass of ClassA.
What can you say about the relationship between ClassB and ClassB2? Identify the hierarchical relationship between the Object class, ClassA, ClassB, and ClassB2. What is the superclass of ClassB? What is the superclass of ClassB2? How many subclasses can a class have, and how many superclasses can it have? |
| 4: | Write a Rectangle method called translate: that takes a vector Point (xv, yv) as its argument. Have it translate the rectangle's origin by the specified vector. |
| 5: | Define a new class called GraphicObject, and make it a subclass of Object. Define instance variables in your new class as follows:
90 int fillColor; // 32-bit color BOOL filled; // Is the object filled? int lineColor; // 32-bit line color Write methods to set and retrieve the variables defined previously. Make the Rectangle class a subclass of GraphicObject. Define new classes, Circle and Triangle, that are also subclasses of GraphicObject. Write methods to set and retrieve the various parameters for these objects and also to calculate the circle's circumference and area and the triangle's perimeter and area. |
| 6: | Write a Rectangle method called intersect: that takes a rectangle as an argument and returns a rectangle representing the overlapping area between the two rectangles. So, for example, given the two rectangles shown in Figure 8.9, the method should return a rectangle whose origin is at (400, 380), whose width is 50, and whose height is 60.
Figure 8.9. Intersecting rectangles.If the rectangles do not intersect, return one whose width and height are zero and whose origin is at (0,0). |
| 7: | Write a method for the Rectangle class called draw that draws a rectangle using dashes and vertical bar characters. The following code sequence
90 Rectangle *myRect = [[Rectangle alloc] init]; [myRect setWidth: 10 andHeight: 3]; [myRect draw]; [myRect free]; would produce the following output: 90 ---------- | | | | | | ---------- |