-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEjercicio3-Numeros-.st
686 lines (460 loc) · 20.7 KB
/
Ejercicio3-Numeros-.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
!classDefinition: #NumeroTest category: '03 - Numeros'!
TestCase subclass: #NumeroTest
instanceVariableNames: 'zero one two four oneFifth oneHalf five twoFifth twoTwentyfifth fiveHalfs three eight negativeOne negativeTwo'
classVariableNames: ''
poolDictionaries: ''
category: '03 - Numeros'!
!NumeroTest methodsFor: 'tests' stamp: 'LL 9/13/2020 16:17:50'!
test01isZeroReturnsTrueWhenAskToZero
self assert: zero isZero! !
!NumeroTest methodsFor: 'tests' stamp: 'LL 9/13/2020 16:17:58'!
test02isZeroReturnsFalseWhenAskToOthersButZero
self deny: one isZero! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 20:13'!
test03isOneReturnsTrueWhenAskToOne
self assert: one isOne! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 20:13'!
test04isOneReturnsFalseWhenAskToOtherThanOne
self deny: zero isOne! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 20:14'!
test05EnteroAddsWithEnteroCorrectly
self assert: one + one equals: two! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 20:18'!
test06EnteroMultipliesWithEnteroCorrectly
self assert: two * two equals: four! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 20:20'!
test07EnteroDividesEnteroCorrectly
self assert: two / two equals: one! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 20:38'!
test08FraccionAddsWithFraccionCorrectly
"
La suma de fracciones es:
a/b + c/d = (a.d + c.b) / (b.d)
SI ESTAN PENSANDO EN LA REDUCCION DE FRACCIONES NO SE PREOCUPEN!!
TODAVIA NO SE ESTA TESTEANDO ESE CASO
"
| sevenTenths |
sevenTenths := (Entero with: 7) / (Entero with: 10).
self assert: oneFifth + oneHalf equals: sevenTenths! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 20:52'!
test09FraccionMultipliesWithFraccionCorrectly
"
La multiplicacion de fracciones es:
(a/b) * (c/d) = (a.c) / (b.d)
"
self assert: oneFifth * twoFifth equals: twoTwentyfifth! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 20:56'!
test10FraccionDividesFraccionCorrectly
"
La division de fracciones es:
(a/b) / (c/d) = (a.d) / (b.c)
"
self assert: oneHalf / oneFifth equals: fiveHalfs! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 21:07'!
test11EnteroAddsFraccionCorrectly
"
Ahora empieza la diversion!!
"
self assert: one + oneFifth equals: (Entero with: 6) / (Entero with: 5)! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 21:07'!
test12FraccionAddsEnteroCorrectly
self assert: oneFifth + one equals: (Entero with: 6) / (Entero with: 5)! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 21:50'!
test13EnteroMultipliesFraccionCorrectly
self assert: two * oneFifth equals: twoFifth ! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 21:52'!
test14FraccionMultipliesEnteroCorrectly
self assert: oneFifth * two equals: twoFifth ! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 21:57'!
test15EnteroDividesFraccionCorrectly
self assert: one / twoFifth equals: fiveHalfs ! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 21:59'!
test16FraccionDividesEnteroCorrectly
self assert: twoFifth / five equals: twoTwentyfifth ! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:38'!
test17AFraccionCanBeEqualToAnEntero
self assert: two equals: four / two! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:39'!
test18AparentFraccionesAreEqual
self assert: oneHalf equals: two / four! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:40'!
test19AddingFraccionesCanReturnAnEntero
self assert: oneHalf + oneHalf equals: one! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:42'!
test20MultiplyingFraccionesCanReturnAnEntero
self assert: (two/five) * (five/two) equals: one! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:42'!
test21DividingFraccionesCanReturnAnEntero
self assert: oneHalf / oneHalf equals: one! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:43'!
test22DividingEnterosCanReturnAFraccion
self assert: two / four equals: oneHalf! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:46'!
test23CanNotDivideEnteroByZero
self
should: [ one / zero ]
raise: Error
withExceptionDo: [ :anError | self assert: anError messageText equals: Numero canNotDivideByZeroErrorDescription ]
! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:46'!
test24CanNotDivideFraccionByZero
self
should: [ oneHalf / zero ]
raise: Error
withExceptionDo: [ :anError | self assert: anError messageText equals: Numero canNotDivideByZeroErrorDescription ]
! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:50'!
test25AFraccionCanNotBeZero
self deny: oneHalf isZero! !
!NumeroTest methodsFor: 'tests' stamp: 'HernanWilkinson 5/7/2016 22:50'!
test26AFraccionCanNotBeOne
self deny: oneHalf isOne! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 5/7/2020 17:46:14'!
test27EnteroSubstractsEnteroCorrectly
self assert: three - one equals: two! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:47:41'!
test28FraccionSubstractsFraccionCorrectly
self assert: twoFifth - oneFifth equals: oneFifth.! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:48:00'!
test29EnteroSubstractsFraccionCorrectly
self assert: one - oneHalf equals: oneHalf! !
!NumeroTest methodsFor: 'tests' stamp: 'HAW 9/24/2018 08:48:05'!
test30FraccionSubstractsEnteroCorrectly
| sixFifth |
sixFifth := (Entero with: 6) / (Entero with: 5).
self assert: sixFifth - one equals: oneFifth! !
!NumeroTest methodsFor: 'tests' stamp: 'HAW 9/24/2018 08:48:08'!
test31SubstractingFraccionesCanReturnAnEntero
| threeHalfs |
threeHalfs := (Entero with: 3) / (Entero with: 2).
self assert: threeHalfs - oneHalf equals: one.! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:48:48'!
test32SubstractingSameEnterosReturnsZero
self assert: one - one equals: zero.! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:49:01'!
test33SubstractingSameFraccionesReturnsZero
self assert: oneHalf - oneHalf equals: zero.! !
!NumeroTest methodsFor: 'tests' stamp: 'HAW 9/24/2018 08:48:14'!
test34SubstractingAHigherValueToANumberReturnsANegativeNumber
| negativeThreeHalfs |
negativeThreeHalfs := (Entero with: -3) / (Entero with: 2).
self assert: one - fiveHalfs equals: negativeThreeHalfs.! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:49:23'!
test35FibonacciZeroIsOne
self assert: zero fibonacci equals: one! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:49:32'!
test36FibonacciOneIsOne
self assert: one fibonacci equals: one! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:49:39'!
test37FibonacciEnteroReturnsAddingPreviousTwoFibonacciEnteros
self assert: four fibonacci equals: five.
self assert: three fibonacci equals: three.
self assert: five fibonacci equals: four fibonacci + three fibonacci.! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:49:47'!
test38FibonacciNotDefinedForNegativeNumbers
self
should: [negativeOne fibonacci]
raise: Error
withExceptionDo: [ :anError | self assert: anError messageText equals: Entero negativeFibonacciErrorDescription ].! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:49:55'!
test39NegationOfEnteroIsCorrect
self assert: two negated equals: negativeTwo.
! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:50:03'!
test40NegationOfFraccionIsCorrect
self assert: oneHalf negated equals: negativeOne / two.! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:50:11'!
test41SignIsCorrectlyAssignedToFractionWithTwoNegatives
self assert: oneHalf equals: (negativeOne / negativeTwo)! !
!NumeroTest methodsFor: 'tests' stamp: 'NR 9/23/2018 23:50:17'!
test42SignIsCorrectlyAssignedToFractionWithNegativeDivisor
self assert: oneHalf negated equals: (one / negativeTwo)! !
!NumeroTest methodsFor: 'setup' stamp: 'NR 9/23/2018 23:46:28'!
setUp
zero := Entero with: 0.
one := Entero with: 1.
two := Entero with: 2.
three:= Entero with: 3.
four := Entero with: 4.
five := Entero with: 5.
eight := Entero with: 8.
negativeOne := Entero with: -1.
negativeTwo := Entero with: -2.
oneHalf := one / two.
oneFifth := one / five.
twoFifth := two / five.
twoTwentyfifth := two / (Entero with: 25).
fiveHalfs := five / two.
! !
!classDefinition: #Numero category: '03 - Numeros'!
Object subclass: #Numero
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: '03 - Numeros'!
!Numero methodsFor: 'arithmetic operations' stamp: 'LG 10/25/2021 15:06:18'!
* aMultiplicand
self subclassResponsibility ! !
!Numero methodsFor: 'arithmetic operations' stamp: 'HernanWilkinson 5/7/2016 22:49'!
+ anAdder
self subclassResponsibility ! !
!Numero methodsFor: 'arithmetic operations' stamp: 'NR 9/23/2018 22:21:28'!
- aSubtrahend
self subclassResponsibility ! !
!Numero methodsFor: 'arithmetic operations' stamp: 'HernanWilkinson 5/7/2016 22:49'!
/ aDivisor
self subclassResponsibility ! !
!Numero methodsFor: 'arithmetic operations' stamp: 'NR 9/23/2018 23:37:13'!
negated
^self * (Entero with: -1)! !
!Numero methodsFor: 'testing' stamp: 'NR 9/23/2018 23:36:49'!
isNegative
self subclassResponsibility ! !
!Numero methodsFor: 'testing' stamp: 'HernanWilkinson 5/7/2016 22:49'!
isOne
self subclassResponsibility ! !
!Numero methodsFor: 'testing' stamp: 'HernanWilkinson 5/7/2016 22:49'!
isZero
self subclassResponsibility ! !
!Numero methodsFor: 'private' stamp: 'HernanWilkinson 5/7/2016 22:48'!
invalidNumberType
self error: self class invalidNumberTypeErrorDescription! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!classDefinition: 'Numero class' category: '03 - Numeros'!
Numero class
instanceVariableNames: ''!
!Numero class methodsFor: 'error descriptions' stamp: 'NR 5/7/2020 17:47:38'!
canNotDivideByZeroErrorDescription
^'No se puede Dividir por Cero'! !
!Numero class methodsFor: 'error descriptions' stamp: 'NR 5/7/2020 17:46:56'!
invalidNumberTypeErrorDescription
^ 'Tipo de Número Inválido'! !
!classDefinition: #Entero category: '03 - Numeros'!
Numero subclass: #Entero
instanceVariableNames: 'value'
classVariableNames: ''
poolDictionaries: ''
category: '03 - Numeros'!
!Entero methodsFor: 'arithmetic operations' stamp: 'LG 10/25/2021 15:03:19'!
* aMultiplicand
^aMultiplicand multipliesAnEntero: self.! !
!Entero methodsFor: 'arithmetic operations' stamp: 'srs 10/23/2021 01:13:36'!
+ anAdder
^anAdder beAddedToAnEntero: self.! !
!Entero methodsFor: 'arithmetic operations' stamp: 'srs 10/23/2021 01:48:14'!
- aSubtrahend
^aSubtrahend beSubtractedToAnEntero: self.! !
!Entero methodsFor: 'arithmetic operations' stamp: 'LG 10/25/2021 13:35:02'!
/ aDivisor
^aDivisor dividiesAnEntero: self.! !
!Entero methodsFor: 'arithmetic operations' stamp: 'srs 10/23/2021 01:54:13'!
fibonacci
| one two |
one := Entero with: 1.
two := Entero with: 2.
self isNegative ifTrue: [self error: Entero negativeFibonacciErrorDescription].
(self isZero or: [self isOne]) ifTrue: [^one].
^ (self - one) fibonacci + (self - two) fibonacci
! !
!Entero methodsFor: 'comparing' stamp: 'HernanWilkinson 5/7/2016 21:01'!
= anObject
^(anObject isKindOf: self class) and: [ value = anObject integerValue ]! !
!Entero methodsFor: 'comparing' stamp: 'HernanWilkinson 5/7/2016 20:17'!
hash
^value hash! !
!Entero methodsFor: 'initialization' stamp: 'HernanWilkinson 5/7/2016 20:09'!
initalizeWith: aValue
value := aValue! !
!Entero methodsFor: 'accessing' stamp: 'HernanWilkinson 5/7/2016 21:02'!
integerValue
"Usamos integerValue en vez de value para que no haya problemas con el mensaje value implementado en Object - Hernan"
^value! !
!Entero methodsFor: 'printing' stamp: 'HAW 9/24/2018 08:53:19'!
printOn: aStream
aStream print: value ! !
!Entero methodsFor: 'testing' stamp: 'NR 9/23/2018 22:17:55'!
isNegative
^value < 0! !
!Entero methodsFor: 'testing' stamp: 'HernanWilkinson 5/7/2016 20:14'!
isOne
^value = 1! !
!Entero methodsFor: 'testing' stamp: 'HernanWilkinson 5/7/2016 20:12'!
isZero
^value = 0! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'HernanWilkinson 5/7/2016 21:55'!
// aDivisor
^self class with: value // aDivisor integerValue! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'srs 10/23/2021 01:41:40'!
beAddedToAFraccion: anAugend
| numeratorSum denominatorSum |
numeratorSum := self value * anAugend denominator + anAugend numerator .
denominatorSum := anAugend denominator .
^Fraccion with: numeratorSum over: denominatorSum. ! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'srs 10/23/2021 01:11:36'!
beAddedToAnEntero: anAugend
^Entero with: anAugend integerValue + self integerValue .! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'srs 10/23/2021 01:46:47'!
beSubtractedToAFracction: aMinuend
| numeratorSub denominatorSub |
numeratorSub := aMinuend numerator - (self value * aMinuend denominator) .
denominatorSub := aMinuend denominator.
^Fraccion with: numeratorSub over: denominatorSub. ! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'srs 10/23/2021 01:32:38'!
beSubtractedToAnEntero: aMinuend
^Entero with: aMinuend integerValue - self integerValue .! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 13:33:54'!
dividiesAFraccion: aDividend
| divisionNumerator divisionDenominator |
divisionNumerator := aDividend numerator .
divisionDenominator := aDividend denominator * self value.
^Fraccion with: divisionNumerator over: divisionDenominator.
! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 13:35:02'!
dividiesAnEntero: aDividend
^Fraccion with: aDividend over: self.! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'HernanWilkinson 5/7/2016 21:00'!
greatestCommonDivisorWith: anEntero
^self class with: (value gcd: anEntero integerValue)! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 15:07:07'!
multipliesAFraccion: aMultiplier
| productNumerator productDenominator|
productNumerator := self value * aMultiplier numerator.
productDenominator := aMultiplier denominator.
^Fraccion with: productNumerator over: productDenominator.
! !
!Entero methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 15:07:11'!
multipliesAnEntero: aMultiplier
^Entero with: value * aMultiplier integerValue.! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!classDefinition: 'Entero class' category: '03 - Numeros'!
Entero class
instanceVariableNames: ''!
!Entero class methodsFor: 'instance creation' stamp: 'NR 4/8/2019 02:57:57'!
negativeFibonacciErrorDescription
^ ' Fibonacci no está definido aquí para Enteros Negativos'! !
!Entero class methodsFor: 'instance creation' stamp: 'HernanWilkinson 5/7/2016 22:53'!
with: aValue
"Esta verificacion esta puesta por si se equivocan y quieren crear un Entero pasando otra cosa que un Integer - Hernan"
aValue isInteger ifFalse: [ self error: 'aValue debe ser anInteger' ].
^self new initalizeWith: aValue! !
!classDefinition: #Fraccion category: '03 - Numeros'!
Numero subclass: #Fraccion
instanceVariableNames: 'numerator denominator'
classVariableNames: ''
poolDictionaries: ''
category: '03 - Numeros'!
!Fraccion methodsFor: 'arithmetic operations' stamp: 'LG 10/25/2021 15:06:30'!
* aMultiplicand
^aMultiplicand multipliesAFraccion: self.
! !
!Fraccion methodsFor: 'arithmetic operations' stamp: 'srs 10/23/2021 01:16:51'!
+ anAdder
^anAdder beAddedToAFraccion: self.! !
!Fraccion methodsFor: 'arithmetic operations' stamp: 'srs 10/23/2021 01:48:30'!
- aSubtrahend
^aSubtrahend beSubtractedToAFracction: self.! !
!Fraccion methodsFor: 'arithmetic operations' stamp: 'LG 10/25/2021 13:33:55'!
/ aDivisor
^aDivisor dividiesAFraccion: self.! !
!Fraccion methodsFor: 'comparing' stamp: 'HernanWilkinson 5/7/2016 20:42'!
= anObject
^(anObject isKindOf: self class) and: [ (numerator * anObject denominator) = (denominator * anObject numerator) ]! !
!Fraccion methodsFor: 'comparing' stamp: 'HernanWilkinson 5/7/2016 20:50'!
hash
^(numerator hash / denominator hash) hash! !
!Fraccion methodsFor: 'accessing' stamp: 'HernanWilkinson 5/7/2016 21:56'!
denominator
^ denominator! !
!Fraccion methodsFor: 'accessing' stamp: 'HernanWilkinson 5/7/2016 21:56'!
numerator
^ numerator! !
!Fraccion methodsFor: 'initialization' stamp: 'HernanWilkinson 5/7/2016 22:54'!
initializeWith: aNumerator over: aDenominator
"Estas precondiciones estan por si se comenten errores en la implementacion - Hernan"
aNumerator isZero ifTrue: [ self error: 'una fraccion no puede ser cero' ].
aDenominator isOne ifTrue: [ self error: 'una fraccion no puede tener denominador 1 porque sino es un entero' ].
numerator := aNumerator.
denominator := aDenominator ! !
!Fraccion methodsFor: 'testing' stamp: 'NR 9/23/2018 23:41:38'!
isNegative
^numerator < 0! !
!Fraccion methodsFor: 'testing' stamp: 'HernanWilkinson 5/7/2016 22:51'!
isOne
^false! !
!Fraccion methodsFor: 'testing' stamp: 'HernanWilkinson 5/7/2016 22:51'!
isZero
^false! !
!Fraccion methodsFor: 'printing' stamp: 'HAW 9/24/2018 08:54:46'!
printOn: aStream
aStream
print: numerator;
nextPut: $/;
print: denominator ! !
!Fraccion methodsFor: 'arithmetic operations - private' stamp: 'srs 10/23/2021 01:44:54'!
beAddedToAFraccion: anAugend
| numeratorSum denominatorSum|
numeratorSum := (anAugend numerator * self denominator) + (anAugend denominator * self numerator).
denominatorSum := anAugend denominator * self denominator .
^Fraccion with: numeratorSum over: denominatorSum. ! !
!Fraccion methodsFor: 'arithmetic operations - private' stamp: 'srs 10/23/2021 01:42:34'!
beAddedToAnEntero: anAugend
| numeratorSum denominatorSum |
numeratorSum := anAugend * self denominator + self numerator .
denominatorSum := self denominator.
^Fraccion with: numeratorSum over: denominatorSum. ! !
!Fraccion methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 14:59:23'!
beSubtractedToAFracction: aMinuend
| numeratorSub denominatorSub |
numeratorSub := (aMinuend numerator * self denominator) - (aMinuend denominator * self numerator).
denominatorSub := aMinuend denominator * self denominator.
^Fraccion with: numeratorSub over: denominatorSub.
! !
!Fraccion methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 14:59:27'!
beSubtractedToAnEntero: aMinuend
| numeratorSub denominatorSub |
numeratorSub := (aMinuend * self denominator) - self numerator.
denominatorSub := self denominator.
^Fraccion with: numeratorSub over: denominatorSub.
! !
!Fraccion methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 13:33:55'!
dividiesAFraccion: aDividend
| divisionNumerator divisionDenominator |
divisionNumerator := aDividend numerator * self denominator.
divisionDenominator := aDividend denominator * self numerator.
^Fraccion with: divisionNumerator over: divisionDenominator .! !
!Fraccion methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 13:35:02'!
dividiesAnEntero: aDividend
| divisionNumerator divisionDenominator |
divisionNumerator := aDividend value * self denominator.
divisionDenominator := self numerator.
^Fraccion with: divisionNumerator over: divisionDenominator.
! !
!Fraccion methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 15:06:53'!
multipliesAFraccion: aMultiplier
| productNumerator productDenominator |
productNumerator := self numerator * aMultiplier numerator.
productDenominator := self denominator * aMultiplier denominator.
^Fraccion with: productNumerator over: productDenominator.! !
!Fraccion methodsFor: 'arithmetic operations - private' stamp: 'LG 10/25/2021 15:06:58'!
multipliesAnEntero: aMultiplier
| productNumerator productDenominator |
productNumerator := Entero with: aMultiplier integerValue * self numerator integerValue.
productDenominator := Entero with: self denominator integerValue.
^Fraccion with: productNumerator over: productDenominator.
! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!classDefinition: 'Fraccion class' category: '03 - Numeros'!
Fraccion class
instanceVariableNames: ''!
!Fraccion class methodsFor: 'intance creation' stamp: 'NR 9/23/2018 23:45:19'!
with: aDividend over: aDivisor
| greatestCommonDivisor numerator denominator |
aDivisor isZero ifTrue: [ self error: self canNotDivideByZeroErrorDescription ].
aDividend isZero ifTrue: [ ^aDividend ].
aDivisor isNegative ifTrue:[ ^aDividend negated / aDivisor negated].
greatestCommonDivisor := aDividend greatestCommonDivisorWith: aDivisor.
numerator := aDividend // greatestCommonDivisor.
denominator := aDivisor // greatestCommonDivisor.
denominator isOne ifTrue: [ ^numerator ].
^self new initializeWith: numerator over: denominator
! !