Object Oriented Software Engineering: Practical Software

advertisement
Object Oriented
Software Engineering:
Practical Software Development
Using UML and Java
Solution Manual - CHAPTER 2
By: Paul Holden
Exercise 2.1: Which of the following items do you think should be a class, and which should be
an instance? For any item which should be an instance, name a suitable class for it. If you think
an item could be a class or an instance, depending on circumstances, explain why.
a) General Motors: This should be an instance of class AutomobileCompany
Common Mistake: Saying that this should be a class with instances’car’, ‘truck’, ‘van’. This
would be correct if the name of the class was General Motor Vehicle.
b) Automobile Company: This should be a class. An instance would be ‘The Automobile
company General Motors’
c) Student: This could be a class. Instances would be ‘John Smith’ and ‘The student with student
number 1234567’
d) Computer Science Student: This is a class and it would have instances such as ‘John Smith’
and ‘The computer Science Student with student number 1234567’
Common Mistake: Saying that this is an instance. For this to be an instance, the name should be
just ‘Computer Science’ and a suitable class would be Student.
e) Mary Smith: This is an instance and a suitable class for it would be Employee or Person.
f) Game: This would be a class and a suitable instance would be ‘The game of Chess’
g) Board Game: This is a class. A suitable instance would be ‘The board game Snakes and
Ladders’
h) Chess: This would be an instance and a suitable class would be Game.
Common Mistake: To say that this is a class. To be a class, it would be preferable to name the
class ChessGame.
i) The game of Chess between Tom and Jane which started at 2:30 p.m. yesterday: This would be
an instance and a suitable class would be ChessGame.
j) Car: This would be a class and instances would be ‘John’s Porsche’ and ‘The Ferrari with
License Number ABC 123’
k) Mazda Car: This would be a class and a suitable instance would be ‘The Mazda Car with VIN
number 12345’
Common Mistake: Mazda Car should not be considered an instance. It could be however an
instance if the name was changed to Mazda.
l) The car with serial number JM198765T4: This is an instance and a suitable class would be
Car
.
Exercise 2.3: The following words in English have different meanings. In some cases, one might
want to create several different classes to represent each of the different meanings. Name and
describe as many classes as you could think of that might be derived from these words.
a) Video: VideoRecording: this class would describe a video recording and could have such
instances as ‘The video (recording) made on Jan 1, 2000’. VideoShowing: this class would
describe the showing of a video and an instance would be ‘The video that will be shown Jan 1
2003’. Note that there can be many showings of a VideoRecording.
b) Flight: RegularFlight: This would describe a flight which has a regular schedule and an
instance would be ‘The flight that leaves from Ottawa to Toronto everyday at 8 a.m.’.
CharteredFlight: This would describe a chartered flight and an instance would be ‘The
chartered flight which left 01/01/99 from Toronto to Anchorage’. SpecificFlight. This would
describe a particular occurrence of a RegularFlight.
c) Program: TelevisionProgram: this class would describe a television program and instances
could ‘Frasier’ or ‘Law and Order’. EpisodeOfProgram: This class could describe an episode of
a television program that played and an instance would be ‘The final episode of Seinfeld’.
ShowingOfEpisode: An example instance of this might be: The rerun of the final episode of
Seinfeld that was played on Dec 20, 2000.
d) Book: BookTitle: This would describe a certain title and an instance would be ‘The fiction
book Catcher in the Rye’ CopyOfBook: This would describe a copy of a book and an instance
would be ‘The copy of Catcher of the Rye with library reference number 123456’.
Exercise 2.4: Identify the attributes that might be present in the following classes. Try to be
reasonably exhaustive.
d) Room: numberOfDucts, roomTemperature, idealRoomTemperature, hasColdAirReturn,
hasAirConditioner.
e) PhoneCall: timeOfCall, durationOfCall, idOfCallReceiver, idOfCaller, frequencyOfCall.
f) AssemblyLine: numberOfRobots, timeToMakePart, materialUsed, numberOfPartsToday
numberOfErrorsToday.
Common Mistake: Not using proper naming for classes and attributes. Classes begin with a
capital and attributes begin with a non-capital. Also, Each new word in the name uses a capital.
Exercise 2.7: Identify the operations that might be present in the following sets of classes;
highlight the polymorphic operations (operations that would be done differently in different
classes).
Here, underlined operations are polymorphic.
a) Square, Circle, Rectangle: getVertices(), getArea(), getCenter().
b) Doctor, Nurse, Patient: getIdNumber(), determineSchedule(), determineTask().
c) PeriodicalIssue, Book, VideoTape: getSerialNumber(), calculateLoanTime(),
calculateLateFee().
Exercise 2.8: Which of the following superclass-subclass pairs are clearly invalid, and why?
Note: This question was a bit confusing, since it implies that all of the following are classes,
when some appear to be instances. We apologise for the confusion!
e) Account - Account12876: This would be valid if Account2876 is a special type of account.
f) People - Customer: This should be invalid because ‘People’ ought to be called ‘Person’, but
the question led you to believe that ‘People’ is a valid class, hence this was accepted as valid.
g) Student - GraduateStudent: This is valid because a graduate student is clearly a student.
Later on in the course, we show that there may be a problem if you actually design a system like
this, since an undergraduate student may have to change to a graduate student at some point.
However, in some kinds of systems, the above could be valid.
h) Continent - Country: This is invalid because a country is not a continent.
Exercise 2.9: Organize each set of classes into inheritance hierarchies, bearing the following
points in mind:
Remember to use the isa rule to help make and validate your decisions.
Add some attributes to help you make decisions, but for this exercise you do not need to
exhaustively include all attributes.
You will normally find you have several distinct and unrelated hierarchies for each set of
classes.
Some classes may not fit in a hierarchy with others; write a simple note indicating when
this is the case.
You may add additional classes to your hierarchies to act as superclasses.
You may change the names of classes to make them better fit your hierarchies, or to deal
with cases where words have more than one meaning. If you do this explain why.
c)
Bus: busNumber, numberOfPassengers
CharteredBus: charteringOrganization
LuxuryBus: levelOfLuxury.
TourBus: sightViewed, tourGuideNamed
ExpressBus: frequency
Note that in the above, it would have been even better to have split up the hierarchy into one
hierarchy of types of vehicle: LuxuryBus, and another hierarchy of types of Route:
CharteredBus, TourBus, ExpressBus.
Route: routeNumber.
BusRoute: typeOfBus.
Trip: departureFrom, arrivingTo, actualDepartureDate, actualDepartureTime
UnscheduledTrip: reason
ScheduledTrip: plannedDepartureDate, plannedDepartureTime
Schedule: Note: This should normally not be a class in a system
d)
MediaPlayer: inputFormat
Television: hasRemoteControl, diagonalViewableArea
VCR: tapeFormat, .numberOfHeads
TapeDeck: hasEditingFacilities
MediaStorageDevice: capacity.
CD: numberOfTracks.
Cassette: length
Movie: title, director, genre
Audio: volume.
Video: numberOfColors.
RemoteControl: numberOfbuttons.
VersionOfSong: version, songTitle.
SongOnCD: songTitle, cdTitle, length
CopyOfCD: title.
SoundOfMusic: This is an instance of movie.
e)Currency: valueInUSDollars.
CanadianDollars: Should really be an instance
USDollars: Should really be an instance
FinancialInstitution: name
Bank:
CreditUnion.
BankBranch: employees.
FinancialInstrument: referenceNumber.
BankAccount: maxWithdrawal.
Cheque: serviceCharge.
CreditCard: limit
VISACard
MasterCardCard:
DebitCard: dailyLimit
Some other arrangements of the above would also be possible,
BankMachine: maxStoredCurrency.
Loan: loanAmount.
ExchangeRate: rateToTheUSDollar
f)
HotelRoom: roomNumber, rate
Suite
ConferenceRoom: numberOfSeats.
MeetingRoom: numberOfSeats.
Ballroom: roomCapacity.
HotelEvent: timeOfEvent, numberOfPeopleAttending.
Conference: subject
Meeting: nameOfCompany.
CateredFunction: mealToBeServed
Person: name, address.
MeetingOrganizer: rateOfPay.
Guest: roomNumber.
Hilton: address.. Should really be an Instance or called HiltonHotel (which has specific
instances)
Reservation: startDate, endDate
Booking: roomBooked, client
OttawaHilton: This is an instance of Hilton.
Exercise 2.10: This question requires knowledge of very basic geometry. Describe in one line
how different polymorphic implementations of the following operations from Figure 2.8 would
work in classes Rectangle, RegularPolygon, Circle and their superclasses.
c)
area in Rectangle: The area operation defined in Rectangle would override the one in
TwoDShape.
area in RegularPolygon: The area operation defined in RegularPolygon would override the
one in TwoDShape.
area in Circle: The area operation defined in Circle would override the one in TwoDShape.
d)
center in Rectangle: The center operation in the superclass SimplePolygon would be used. This
overrides the center in TwoDShape.
center in RegularPolygon: The center operation in the superclass SimplePolygon would be
used. This overrides the center in TwoDShape.
center in Circle: The center operation defined in Circle would override the one in TwoDShape.
Exercise 2.12: In which of the following situations would dynamic binding be needed:
c) Variable of type Polygon invokes boundingRectangle: Yes, dynamic binding would be needed
because Rectangle overrides boundingRectange.
d) Variable of type Ellipsoid invokes scale: No, dynamic binding would not be needed because
none of its subclasses overrides scale.
e) Variable of type RegularPolygon invokes translate: No, because RegularPolygon is a leaf
class therefore it is known at runtime which translate it will use.
f) Variable of type TwoDShape invokes boundingRectangle: Yes, dynamic binding will be
needed because many subclasses override boundingRectangle.
Download