OU Courese Banner ADBH Web - M206 TMA3 question 3
ADBH Web 22-Nov-2008

M206 - TMA3 question 3

(i) Class comment:

SlantedFrog is a subclass of Frog. Instances of class SlantedFrog have an additional instance variable height and an additional protocol to initialise and use height similarly to HoverFrog. An instance of SlantedFrog also responds to the messages up and down in the following way: when it moves up it also moves to the right, and when it moves down it also moves to the left (it moves up and down on a slope).

Example usage:

  • The following expression series creates an instance aHoverFrog, with the message new including initialisation of height to 0. Answer the receiver."
  • aTreeFrog := TreeFrog new.
  • aTreeFrog hover: Up by: 3
  • Instance variable height comment;
  • "The height of the SlantedFrog ranging from height 0 to height 6'

(ii) up
"Increment height of receiver by 1 and move right by 1. Answer the receiver."

  • self hover: Up by: 1.

down
Decrement height of receiver by 1 and move left by 1. Answer the receiver."

  • self hover: Down by: 1.

hover: aDirection by: aNumber

"Set height of receiver to height plus (for aDirection Up) aNumber and position plus aNumber or minus (for aDirection Down) aNumber and position minus aNumber "

  • (aDirection = Down)
  • ifTrue: [self height: (self height - aNumber).
  •   self position: (self position - aNumber)]
  • ifFalse: [self height: (self height + aNumber).
  •   self position:( self position +aNumber )]

(iii) SlantedFrog and HoverFrog have the following methods with identical code: height, height: and initialize.

(iv) To improve the design of the classes in the Frog hierarchy, a new abstract class SuspendedFrog should be created as a subclass of Frog, and the classes SlantedFrog and HoverFrog should be made to inherit from SuspendedFrog.

(a) The abstract class SuspendedFrog would improve the class hierarchy by allowing all common code and instance variables to be contained within it thus reducing the amount of duplicated code the subclasses. The code now held in SuspendedFrog only needs to be overridden in the subclass if required.

(b) List for the new design:

Class SuspendedFrog Subclass of Frog

  • Instance variables: height
  • Instance methods: hover: by: height, height: initialize

Class HoverFrog Subclass of SuspendedFrog

  • Instance variables: None
  • Instance methods: hover: by: PrintOn:

Class SlantedFrog Subclass of SuspendedFrog

  • instance variables: None
  • Instance methods: hover: by:

(c) New methods for class SuspendedFrog

  • hover: aDirection by: aNumber
  • "Method of Abstract class"
    ^self subclassResponsibility


(d) An instance of SlantedFrog is diferent from an instance of HoverFrog because they both direct subclasses of SuspendedFrog i.e SlantedFrog is not a subclass of HoverFrog

 

Question 2 - Question 4

Top of Page

 

 

 

Page built in 0.0017 seconds