![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Once you understand dates, you can revise the rabbit-simulation program,
shown in Segment 523, to use Date
instances,
rather than Integer
instances.
Each revision is marked by a
change bar, , except the changes in variable names from
month orientation to date orientation. Note that the principal changes are
the replacement of 0
by Date today
and the replacement of
+ 1
and + 2
by addDays: 30
and addDays: 60
.
Rabbit class definition Object subclass: #Rabbit instanceVariableNames: 'deliveryDate' classVariableNames: '' poolDictionaries: '' Rabbit method definition instance deliveryDate: aDate deliveryDate := aDate Rabbit method definition instance deliveryDate ^ deliveryDate RabbitApplication class definition Object subclass: #RabbitApplication instanceVariableNames: 'rabbits date history' classVariableNames: '' poolDictionaries: '' RabbitApplication method definition instance initialize date := Date today. history := OrderedCollection new. rabbits := SortedCollection sortBlock: [:x :y | x deliveryDate < y deliveryDate]. rabbits add: (Rabbit new deliveryDate: (Date today addDays: 60)) RabbitApplication method definition instance step | mother daughter currentDate | mother := rabbits removeFirst. daughter := Rabbit new. currentDate := mother deliveryDate. mother deliveryDate: (currentDate addDays: 30). daughter deliveryDate: (currentDate addDays: 60). rabbits add: mother. rabbits add: daughter. RabbitApplication method definition instance tickTo: limit [date <= limit] whileTrue: [self updateDate. self step]. history printOrderedCollectionValues RabbitApplication method definition instance updateDate [date < rabbits first deliveryDate] whileTrue: [history addLast: rabbits size. date := date addDays: 30] OrderedCollection method definition instance printOrderedCollectionValues self do: [:element | Transcript show: element printString; space]. Transcript cr Workspace RabbitApplication new initialize tickTo: (Date today addDays: 12 * 30) Transcript 1 1 2 3 5 8 13 21 34 55 89 144 233