M206 - TMA03 Question 4
(i) Comment for BirthMonth class:
Class BirthMonth Subclass of Object: An instance of BirthMonth corresponds to a specific month (with monthName) and accumulates the sum of the weights of newborns registered in that month and their total number.
The instance variables totalWeightMonth, total BirthsMonth and monthName record respectively the sum of weights (in grams) of registered births for that month, the number of births for that month, and the month name. When instances of BirthMonth are created, they are created with 0 totalWeightMonth and 0 totalBirthsMonth, and a monthName value. The class variables TotalBirths and TotalWeight are used to store the total number of births registered and the total weighlt of the newborns registered. These class variables change every time a new birth is registered.
Comment for class variable TotalBirths:
"The class variable TotalBirths records the accumulated sum of births for all instances of BirthMonth."
Comment for class variable TotalWeight..
"The class variable TotalBirths records the accumulated sum of births
for all instances of BirthMonth."
The instance variables: totalWeightMonth , totalBirthMonth, monthName
(ii) Class accessor methods and the class method initialize.
- totalBirths
- "Answer the total number of births."
- ^TotalBirths
- total Births:aNumber
- "Increase TotalBirths by aNumber"
- TotalBirths: = (aNumber).
- totalWeight
- "Answer the total weight of all newborn babies."
- ^TotalWeight
- totalWeight:aWeight
- Increase TotalWeight by the new weight(aWeight)."
- TotalWeight:= aWeight
- initialize
- "Initialize Class Variables TotalBirths and TotalWeight to 0 and 0 respectively."
- TotallBirths:= 0.
- TotalWeight:=0.
Screen capture proving class initialize worked.

(iv) Instance methods.
- registerWeight: aWeight
- "Class method registerWeight updates the class variable TotalWeight with the newborns weight (aWeight) and increments the class variable TotalBirths by one."
- TotalWeight:= TotalWeight + aWeight.
- TotalBirths:= (TotalBirths + 1)
- averageMonthWeight
- " Calculate the average weight of all births in month."
- | aveweight |
- aveweight:= (self totalWeightMonth / self totalBirthsMonth) asFloat.
- Dialog warn:(('The average weight of newborns this month is: ' concatenate:aveweight printString)con concatenate:' grammes')
- initialize
- "Initialize new instances of BirthMonth by setting the instance variables totalBirthsMonth and totalWeightMonth to 0. Answer the receiver."
- totalWeightMonth:=0.
- total BirthsMonth:= 0. monthName
- monthName
- "Answer the month of the receiver
- ^monthName
- monthName: aMonthName
- "Set the month of the receiver"
- monthName: = aMonthName
- registerBirth:aWeight
- "Register weight of newborn (aWeight) and increment the total births this month by 1. Also update Class variables TotalWeight and TotalBirths "
- self totalWeightMonth: (self totalWeightMonth + aWeight)
- self total BirthsMonth: (selfBirthsMonth + 1)
- (self class)registerWeight:
- total BirthsMonth
- "Answer the total number of births this month."
- ^totalBirthsMonth
- total BirthsMonth: aNumber
- Increase totalBirthsMonth by aNumber"
- totalBirthsMonth:= aNumber
- totalWeightMonth
- "Answer the total weight of newborns this month."
- ^totalWeightMonth
- totalWeightMonth: aWeight
- Increase totalWeightMonth by the new weight (aWeight)."
- totalWeightMonth.=aWeight
- newWithMonth Name: value
- "Creates a newly initialized instance of BirthMonth and set instance variable monthName."
- ^((super new) initialize monthName:value).
- averageWeight
- "Calculates the average weight of all recorded births and displays it. Answer the receiver"
- | aveweight |
- aveweight:= (TotalWeight/TotalBirths) asFloat.
- Dialog warn:(('The average weight of all registered births is: concatenate:aveweight printString)concatenate:' grammes')
(vi)Firstly to test the code 1 initialised the BirthMonth class, then checked the class variables TotalWeight and TotalBirths were both set to zero.
I then created and instance of BirthMonth using xxx: - BirthMonth newWithMonthName: #aaa where xxx was the name 1 was giving to the new instance and #aaa was the short month name e.g. #,Jan. I then inspected the class and instance variables and confirmed that they were as expected. I then registered a few births using instance method registerBirth:aWeight using 100 as the weight to begin with, I entered 5 values of 100 and then inspected the totalMonthWeight and totalMonthBirth instances variables to confirm that they were 500 and 5 respectively.
I also checked that the accessor, methods totalWeightMonth and totalBirthsMonth also worked correctly. I then tested that totaiWeightMonth:aWeight and totalBirthsMonth:aNumber also worked correctly by using easy values and then inspecting, both functioned as planned. I then inspected totalWeightMonth and total BirthsMonth noting their values before testing instance method averageMonthWeight month, averageMonthWeight month was then checked and the value shown in the dialog as the average weight so far was found to be correct, so averageWeight functioned as planned. Then two class variables were nowchecked and their values were found to be as expected i.e. equal to instance variables totalWeightMonth and total BirthsMonth (this was correct at this stage as only one instance of BirthMonth had been created. Class method averageWeight was now tested and found to be Ok. The values of the instance and class variables were noted for future use.
A new instance of BirthMonth was created using newWithMonth Name: aMonthName in for the instance methods I used easy weight values, checking as with the previous instance. Now when checking the class methods and instances this time the TotalWeight and TotalBirth class variables should equal the sum of the instance variables of both of the instances of BirthMonth created so far, this was confirmed.
A further three instances were created and treated in the manner described above between each creation. All seems to work as the specification requires.