1 12
A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENTExercises
4.1 This is an exercise to do in tutorial groups. Each group should be supplied with a large piece of paper such as one from a flip chart and a couple of marker pens.
a Get into groups of three or four students.
b Place the contents of your pockets and bags on the desk in front of you.
Arrange the items in categories such as stationery (pens, notebooks, etc.), communication (mobile phones, etc.), garments (hats, gloves, etc.).
d Study the categories and try to refine them into subcate- gories, e.g. within the stationery group there might be writing implements, electronic devices, etc.
e Repeat d) until you can refine no longer.
See if any of the items on the desk can be taken apart (without breaking them). For example, a pen can have its top taken off, a pair of gloves can be split into the right- hand glove and the left-hand glove.
g On the large piece of paper or a board draw a diagram to represent your categories such as the one in Figure 4.35.
The categories are arranged in a hierarchy with the top rectangle representing the original group of items, the next down your initial categorization, etc. Draw a line
Top
OBJECTS AND CLASSES" THE BASIC CONCEPTS EXERCISES
113
4.2
joining each subcategory to the category above it. Draw the final categorization (the one where you took items apart) in a different colour.
h Discuss how your diagram relates to what you know about objects and classes.
Identify class and attribute names for the following groups of objects.
Sarah Porter 039777657 Systems Modelling CS2oo45
53%
Rakesh Patel 039765869 Programming 2 CS2oo46 65%
Lyn Michaels 039654344 Programming 2 CS2oo46 41%
WMo73COC Linen Trousers Cocoa
8,1o,12,14,16,18 s
WL123YLW Floral Cardigan Yellow
8,1o,12,14,16,18 s
MAo23BL Friday Shirt Blue 15,16,17,18 s
0333695275 Into Thin Air Jon Krakauer Macmillan 1997
0434287466 Grace Maggie Gee Heinemann 1998
0099268558 Music & Silence Rose Tremain
Vintage 2000
4.3
4.4
4.5
Suggest classes you would expect to find in the following systems:
a A banking system.
b A drawing package.
c A library.
d University h u m a n resource department.
e Mail order.
Objects and classes come in different categories: people, organizations, physical things, conceptual things. Identify the categories of the classes you suggested in your answer to Question 4.3.
Items a - n in Table 4.6 list object-oriented concepts. Items 1- 14 list short definitions. Match the concept to the definition that best describes it.
1 14 A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENT
Table 4.6:
Concept
Items and concepts for Exercise 4.5 Definition
a aggregation b association
c attribute d class e data hiding
f encapsulation g generalization h inheritance
a instatiation b message
c method d object
e operation f polymorphism
1 a relationship between two classes where one is a specialization of another
2 the ability of one operation to be implemented by different methods 3 abstracting common features into a
superclass
4 code implementing an operation 5 concealing internal details of an object 6 creation of an object
7 data item defined as part of a class or object 8 instance of a class
9 interface of a method
l o packaging together data and operations 11 relationship between classes
12 request for a service to be executed 13 template for objects
14 whole-part relationship
4.6
4.7
This question relates to the rugby counter example in the section on encapsulation and data hiding. Give a list of all the messages that the object blueSide :Counter can understand.
Draw diagrams to link the following classes using association, aggregation, inheritance and multiplicity where appropriate.
a hotel room, booking, guest
b club member, adult member, junior member c exam paper, instruction, question, solution d animal, mammal, bird, reptile, dog, horse, parrot e sentence, word, letter, punctuation
f academic staff, lecturer, professor, student.
OBJECTS AND CLASSES: THE BASIC CONCEPTS EXERCISES
115
Figure 4.36
VisitorCard I currentDate delete()
Card class hierarchy
Card cardNumber delete()Staffcard I name dept expiryDate delete()
4.8
4-9
a In the diagram in Figure 4.3 6, what will be the attributes of a StaffCard object?
b In the diagram in Figure 4.37, what will be the attributes of a WholesaleCustomer object?
Complete in Table 4.3 (relating to the European hierarchy) which shows object names, classes, attribute values and responses to the greet() message. As well as the information in Table 4.2, you can assume the following:
9 the Briton object is called george 9 the Italian object is called antonio 9 all Britons speak English
9 all Italians speak Italian.
Customer name address phone#
AcountCustomer account#
creditLimit
WholesaleCustomer contactName
Figure 4.3 7 Customer class hierarchy
1 16 A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENT
4.1o If a CatRobot object, from the Robot hierarchy defined in the Technical points section on polymorphism, is called jojo, what message would you send to get him to perform? Hint:
remember you have to address the object as well as the operation.
4.11 Complete Table 4.5 (relating to the Robot hierarchy) which shows object names, classes, attribute values and responses to the perform() message.
As well as the information in Table 4.4, you can assume the following:
9 all DomesticRobots and their descendants have 2 legs 9 all AlienRobots and their descendants have wheels 9 all WierdoRobots have several heads
. all AnimalRobots and their descendants have 4 legs.
Where attribute values are unspecified (e.g. names, lan- guages, no.OfWheels, noise, etc.)you can choose whatever value you like for the objects.
4.12 Which of the classes in the Robot hierarchy are abstract and why? (Hint: Table 4.4 will help you here.)
4.13 What would the output be from the code sections in Figures 4.31 and 4.32? (Hint: Table 4.4 and the answers to Question 4.11 will also help you here.)
4.14 Amend the Robot hierarchy in Figure 4.29 by adding a class HorseRobot as a subclass of AnimalRobot, do not redefine the perform() method. (Hint: remember that for a class to be a specialization of its parent, it must have some distinguishing feature.)
i What attributes would the HorseRobot class inherit?
ii What implementation of perform() would it inherit?
4.15 a Add an object of the HorseRobot class to the array of Robot objects in Figure 4.31 and adjust the code to accommodate it.
b Adjust the code in Figure 4.3 2 to process the updated array.