fork download
  1. Object subclass: MyCalculator [
  2.  
  3. MyCalculator class >> getInput: prompt [
  4. | userInput |
  5. userInput := FileStream stdin nextLine.
  6. ^userInput isEmpty ifTrue: [nil] ifFalse: [userInput asInteger].
  7. ]
  8.  
  9. MyCalculator class >> isPositive: number [
  10. ^number > 0.
  11. ]
  12.  
  13. MyCalculator class >> isNegative: number [
  14. ^number < 0.
  15. ]
  16.  
  17. MyCalculator class >> areNumbersEqual: num1 and: num2 [
  18. ^num1 = num2.
  19. ]
  20.  
  21. MyCalculator class >> calculate: num1 and: num2 [
  22. | result |
  23. result := Dictionary new.
  24. result at: 'Addition' put: (num1 + num2).
  25. result at: 'Subtraction' put: (num1 - num2).
  26. result at: 'Multiplication' put: (num1 * num2).
  27. result at: 'Division' put: (num1 / num2).
  28. ^result.
  29. ]
  30.  
  31. MyCalculator class >> main [
  32. | num1 num2 |
  33. FileStream stdout nextPutAll: 'Enter the first integer: '; flush.
  34. num1 := self getInput: 'Enter the first integer: '.
  35.  
  36. FileStream stdout nextPutAll: 'Enter the second integer: '; flush.
  37. num2 := self getInput: 'Enter the second integer: '.
  38.  
  39. FileStream stdout nextPutAll: 'First number is ', (self isPositive: num1) ifTrue: ['positive'] ifFalse: ['negative'], '.'; nl; flush.
  40. FileStream stdout nextPutAll: 'Second number is ', (self isPositive: num2) ifTrue: ['positive'] ifFalse: ['negative'], '.'; nl; flush.
  41. FileStream stdout nextPutAll: 'Numbers are equal: ', (self areNumbersEqual: num1 and: num2) asString, '.'; nl; flush.
  42.  
  43. | results |
  44. results := self calculate: num1 and: num2.
  45.  
  46. FileStream stdout nextPutAll: 'Results:'; nl; flush.
  47. results keysAndValuesDo: [ :key :value |
  48. FileStream stdout nextPutAll: key, ': ', value asString; nl; flush.
  49. ].
  50. ]
  51. ]
  52.  
  53. MyCalculator main.
  54.  
Success #stdin #stdout #stderr 0.01s 7732KB
stdin
10
12
stdout
Standard output is empty
stderr
./prog:43: parse error, expected ']'