(** * This COOL program emulates a simple calculator * Provides addition, subtraction, multiplication and integer division only * Also provides memory for the session *) Class PrettyIO inherits IO { print(operation: String,x :Int, y:Int,result :Int) :Object { { out_string("-------Operation------- -----Result---------\n"); out_string(" "); out_int(x); out_string(" ".concat(operation).concat(" ")); out_int(y); out_string("----------->>>>>> "); out_int(result); out_string("\n"); } }; printLR(operation: String,x :Int, y:Int,result :Int) :Object { { out_string(" -------Operation------- -----Result---------\n"); out_string("Last Result--> "); out_int(x); out_string(" ".concat(operation).concat(" ")); out_int(y); out_string("----------->>>>>> "); out_int(result); out_string("\n"); } }; }; class ArithMeticOperations inherits PrettyIO { addTwoNumbers (x :Int, y : Int) : SELF_TYPE {self}; subtractTwoNumbers(x :Int, y : Int) : SELF_TYPE {self}; divideTwoNumbers(x :Int, y:Int) :SELF_TYPE {self}; multiplyTwoNumbers(x :Int, y: Int) : SELF_TYPE {self}; }; class Calculator inherits ArithMeticOperations { init() : SELF_TYPE {self}; -- variable to act as memory storage for the session memoryItem : Int; lastResult :Int; operationString : String; addTwoNumbers (x:Int, y:Int) :SELF_TYPE { { print("+",x,y,x + y); lastResult <- (x + y); self; } } ; addToResult (x :Int) :SELF_TYPE { { printLR("+",lastResult, x, lastResult + x); lastResult <- lastResult + x; self; } }; subtractTwoNumbers (x:Int, y:Int) :SELF_TYPE { { print ("-",x,y,x - y); lastResult <- (x - y); self; } }; subtractFromResult (x : Int) :SELF_TYPE { { printLR("-",lastResult, x, lastResult - x); lastResult <- lastResult - x; self; } }; subtractResultFrom (x : Int) :SELF_TYPE { { printLR("-", x,lastResult, x - lastResult); lastResult <- x - lastResult; self; } }; divideTwoNumbers (x:Int, y:Int) :SELF_TYPE { { print ("/",x,y,x/ y); lastResult <- (x /y); self; } }; divideResultBy (x:Int) :SELF_TYPE { { printLR("/",lastResult, x, lastResult / x); lastResult <- lastResult / x; self; } }; divideWithResult (x:Int) :SELF_TYPE { { printLR("/",x, lastResult, x / lastResult ); lastResult <- x / lastResult ; self; } }; multiplyTwoNumbers (x:Int, y:Int) :SELF_TYPE { { print ("*",x,y,x * y); lastResult <- (x * y); self; } }; multiplyResultWith (x: Int) :SELF_TYPE { { printLR("*",lastResult, x, lastResult * x); lastResult <- lastResult * x; self; } }; setMemoryItem (mem :Int) : SELF_TYPE { { memoryItem <- mem; self; } }; getMemoryItem () : Int {memoryItem}; }; class Main { calci: Calculator; main() : Object { { (new Calculator).init().addTwoNumbers(30,70).subtractTwoNumbers(90,45); (new Calculator).init().addTwoNumbers(30,70).addToResult(50); (new Calculator).init().multiplyTwoNumbers(10,20).addToResult(100).subtractFromResult(25).divideResultBy(25); calci <- (new Calculator).init().divideTwoNumbers(10,100); calci.setMemoryItem(100); calci.addTwoNumbers(calci.getMemoryItem(),12); true; } }; };