UCL Logo

Problem Class Questions 2006
for COMP1008 (Object-Oriented Programming)

Week 7 (Starting 6th March)

These questions are on the topic of inheritance.

Q1. Consider this UML diagram:

Inheritance Example UML Diagram

Fill in example instance variables and methods for each class, taking into account the inheritance relationships. Then outline step-by-step what should happen when an instance object of each class is created using the new operator. In particular, explain how super should be used in each constructor.

Q2. Following from the lectures on inheritance with the class Shape and class Square example, sketch out an extended class hierarchy using all the differerent kinds of shape you can identify. For each shape subclass determine what instance variables it needs and define the constructor method. Remember a class hierarchy can have multiple levels, so that subclasses can have subclasses.

Q3. Assume an the following abstract methods are added to class Shape:

public abstract void setWidth(int n);
public abstract void setHeight(int n);

These methods allow the width and height of a shape to be changed and should be overridden by subclasses.

a) For each shape class in Q1, define how the methods should be implemented. In particular, consider classes like Square and Circle.

b) Should a class Square be a subclass of class Rectangle? Or class Rectangle be a subclass of class Square? Or some other arrangement?

c) Is a class Oval a subclass of class Circle?

 

Q4. This UML diagram shows the general form of the Composite Pattern. The pattern is typically used for data structures consisting of a collection of objects. All objects are instances of classes that are subclasses of Component, so they all inherit a common set of public methods. In addition, some objects are Composites as they contain a collection of other Component objects.

Composite Pattern Example UML diagram

Note that any number of Leaf and Composite classes can be defined, not just the two shown in the diagram. The Client is a class that uses the composite data structure.

a) The layout of a newspaper is composed of pages, articles, columns, pictures, paragraphs, words and characters. Use the Composite pattern to describe how a newspaper can be represented by a collection of objects and draw a class diagram showing your classes and the use of the pattern. Hint, start by drawing example object diagrams to determine how collections of objects can be used to represent pages in a newspaper.

b) The layout of a window on a computer screen can also be represented by the Composite pattern. A window consists of a collection of Components including panels (Composites that contain other Components), text areas, buttons, check boxes, menu bar, menus and so on. Sketch an example window and show how it can be represented in terms of Component objects. Then draw a class diagram to show your classes and their relationships.

Last updated: September 2, 2006

Computer Science Department - University College London - Gower Street - London - WC1E 6BT - Telephone: +44 (0)20 7679 7214 - Copyright 1999-2006 UCL


 Search by Google