M206 - TMA02 Question 3
(i) Default method for Account
(a) default "Set the instance variables balance, overLimit and holder to 0, 0 and 'A Person' respectively. Answer the receiver."
- self balance:0
self overLimit:0.
self holder:'A Person'
(b) Modified comments and code for new FlexiAccount class:
(1) Comments for FlexiAccount class are:
"FlexiAccount is a subclass of Account. The class differs slightly from Account in that it has an overdraft facility of 200. An additional protocol is used to initialise this account.
Example use: To setup a new account use the following:
- myFlexiAccount:= FlexiAccount new.
mvFlexiAccount default
(2) Modified default method for FlexiAccount class:
default
"Default as for the superclass (Account) and set overLimit to
200. Answer the receiver."
- super default.
self overlimit:200.
(3) To test my implementation of FlexiAccount 1 first created an the new account "myFlexiAccount" using "myFlexiAccount:= FlexiAccount new". I then inspected the newly created myFlexiAccount to confirm that balance, overLimit and holder were all set to nil. this was confirmed. 1 then evaluated "mvFlexiAccount default' and reinspected myFlexiAccount , balance now set to 0, overLimit set to 200 and holder set to 'A Person'. I repeated the process several times thus proving my implementation of the FlexiAccount initialization works to the given specification. I also checked that all inherited methods produced expected behaviours, which they did.
(ii) The new TreeFrog class:
Initial comments for TreeFrog
TreeFrog is a subclass of HoverFrog. TreeFrog has the added properties in that if its height is set to 6 then its colour will changed to green. Also if it is already at height 6, then it's colour cannot be changed from green.
Example usage:
"The following expression series creates an instance aTreeFrog, with the message new including initialisation of height to 0. The object then uses a message in its protocol to show 'hovering' in an associated graphical interface Answer the receiver."
- aTreeFrog := TreeFrog new.
aTreeFrog hover: Up by: 3
colour: aColour
"Set the colour to aColour, however if the height is 6, set Colour to Green. Answer the receiver."
- (self height =6)
ifFalse: [super colour:aColour]
height: aNumber
"Height as for superclass (HoverFrog). Additionally if the new Height is 6 change the colour to green. Answer the receiver.
- super height:aNumber.
(self height = 6)
ifTrue: [super colour: Green]