Learning Center
Plans & pricing Sign in
Sign Out

Object Oriented Programming Object Oriented


  • pg 1
									Object Oriented Programming Exam January 2002 2 hours

Answer ALL the questions The marks total 92 You may use the Smalltalk Survival Guide during this exam.




"A subclass may override one or more methods." (a) Explain (with an example in each case) the words underlined in the above statement. (8 marks) (b) Explain what happens if a message is sent to a subclass object that does not have a method with this name defined in the subclass. (4 marks) The following Smalltalk expression series is to be executed:
| accA accB accC | accA := Account new. accB := Account new. accA balance: 200. accB balance: 300. accA transfer: 150 to: accB. accC := accB. MessageBox message: 'Done'.    


(a) (b)


Explain what each of the four lines indicated by arrows does; (8 marks) Draw a diagram to show the state of the computer's main memory and variable table at the point where the message box is being displayed. (8 marks) Explain what happens after the message box has been dismissed (by the OK button being clicked on). (4 marks)

continued ....




Given the following Smalltalk expression series,
| a b c [

a b c | := 7. := 12. := 0. a > b ] whileFalse: [ a := a + 1. (a > 9) ifTrue: [ c := c + 1 ] ]

(i) (ii)

what are the values of a and c at the end of the second iteration ? what are the values of a and c at the end of the final iteration ? (12 marks)

continued ....




A bank system makes use of the BankAccount class. It is decided to create two new classes: Branch this records details of branches such as address and manager's name Loyalty Account is the same as a BankAccout but also records the number of years the account has been open. (a) Complete the class hierarchy diagram shown below to show where you would put the two new classes for the two new types of account in relation to the Object class and the BankAccount class. Object BankAccount (b) (c) (2 marks) Explain your reasoning for your diagram for part (i). (2 marks) Suppose the BankAccount class has an initialize method. (i) What would be the purpose of such a method ? (1 mark) (ii) How might it be used inside an initialize method for the LoyaltyAccount class to save unnecessary programming work ? (3 marks)




A display on a computer screen consists of points of light laid out in a two-dimensional array. Each point is called a pixel - an abbreviation derived from the words picture element. Each pixel lies at a place that can be referred to by its coordinates x and y.  x measures the distance from the left side of the screen  y measures the distance from the top of the screen. 0 1 2 3 4 5 6 7 8 0 1 2 3 4 Hence the white dot in the above array of pixels has x = 5 and y = 3 In Smalltalk we can represent points as pairs of coordinates using the @ symbol. Hence the above white dot with x = 5 and y = 3 would be
5 @ 3

Suppose we have a Pen class in Smalltalk which is able to draw lines on the computer screen. Below on the left is a code fragment that draws a horizontal black line across the screen as shown below on the right:
P1 P1 P1 P1 := Pen new. colour: ClrBlack. moveTo: 30 @ 50. lineTo: 100 @ 50
50 30 100



The method headings and initial comments for the methods in the Pen protocol are:
new colour: aColour colour moveTo: aPoint Create a new Pen object. Change the pen's ink to aColour. Answer the receiver. Answer the current colour value. Move the pen to the point aPoint without drawing on the screen. Answer the receiver. aPoint becomes the current point. Draw a line from the current point to the point aPoint. Answer the receiver. aPoint becomes the current point. Answer the point which is half way between aPoint1 and aPoint2.

lineTo: aPoint

midWayFrom: aPoint1 to: aPoint2

Colours available are ClrBlack, ClrRed, ClrBlue and ClrGreen. (a) What letter will be drawn on the screen by the following fragment.
P2 := Pen new. P2 moveTo: 60 @ 100; lineTo 120 @ 100. P2 moveTo: 90 @ 100; lineTo: 90 @ 200


(5 marks) Write the Smalltalk statements to complete each method for the Pen class whose method heading and initial comment are given below. (i)
lineFrom: aStartPoint to: anEndPoint "Draws a line from aStartPoint to anEndPoint. Answer the receiver. anEndPoint becomes the current point."

(5 marks) (ii)
twoToneLineFrom: aPoint1 to: aPoint2 "Draws a line coloured red from aPoint1 to the mid point and coloured blue from the mid point to aPoint2. Answer the receiver. aPoint2 becomes the current point. The original pen colour should be restored after drawing the line"

(10 marks)




A partially completed design document for a system to record information about the forms in the schools in a local education authority (LEA) is shown below: Class Association Diagram

LEASystem * School Use cases 1. Add a new school 2. Add a new form 3. Display total number of pupils in a school Sequence diagrams See next page * Form



Display total number of pupils in a school
Display total pupils Description Display total pupils Get school :UserInterface :LEASystem :School :Form

displayTotalPupils( aSchool)

getSchool(aSchool) getPupilCount

Get pupil count Initialize a count For each form Get form Get number of pupils Add to count Next form Display the count

getNextForm getNumberPupils






Redraw the class association diagram adding: (i) an interface class showing how it is associated with the other classes in the domain model; (2 marks) (ii) suitable role names on the associations taking into account the naviagation indicated. (3 marks) Explain how the associations will be implemented in Smalltalk. (3 marks) Create a method for the School class that will be used in implementing the use case: 3. Display total number of pupils in a school The method header and comment are:
getPupilCount “Answer the total number of students in the school.” You may assume that the Form class has an instance variable numberPupils with suitable setter and getter methods.


(6 marks) Create a method for the LEASystem class to implement the use case: 3. Display total number of pupils in a school The method header and comment are:

displayTotalPupils: aSchool “Display in a MessageBox the total number of pupils in the school identified by aSchool. Answer the receiver.”

(6 marks)



To top