Question: Write an interface Calculator that represents a single calculator. This interface should contain: A method input that takes a single character as its only argument.

Write an interface Calculator that represents a single calculator. This interface should contain:
A method input that takes a single character as its only argument. This method should return a Calculator object as a result of processing the input.
A method getResult that does not take any arguments and returns the current "result" of the calculator (i.e. the message that we would normally see on the screen) as a String object.
The SimpleCalculator implementation
A simple calculator takes straightforward inputs. Due to limited processing power it cannot work with whole numbers longer than 32 bits. This calculator has the following characteristics:
A correct basic sequence of inputs is the first operand, followed by the operator, followed by the second operand, followed by "="(e.g.32+24=). Note that each operand may have multiple digits.
A valid sequence can contain a sequence of operands and operators (e.g.32+24-10=,32+24=-10=, etc.).
The result at any point should show either what was entered thus far, or the result. For example, for the sequence of inputs 32+24= the result after each input should be "3","32","32+","32+2","32+24","56" in that order. For the sequence of inputs 32+24-10= the result after each input should be "3","32","32+","32+2","32+24","56-","56-1","56-10","46" in that order. Before entering any inputs, the result should be the blank string.
The only valid operand characters are 0-9, and the only valid operators are +,- and *.
The input 'C' will clear the calculator inputs. The result after clearing should be the blank string.
The calculator does not "infer" any missing inputs. For example, although 32+=,+12+3, etc. is valid input on a normal calculator, this calculator will report that as an error.
The calculator does allow inputting "=" multiple times. In this case it will return the same result. For example the result after 32+24= and 32+24=== is the same: 56. This is not what a normal calculator will do.
The calculator does not allow inputting negative numbers, although it can handle negative results.
If an operand overflows, it should throw an IllegalArgumentException and the operand's value before the input that caused it to overflow should be retained.
It throws an IllegalArgumentException for all invalid inputs and sequences. However it throws a RuntimeException if a valid input causes an operation to overflow. If the operand does not overflow but the result of the arithmetic does, then the result reported should be 0. For example, a + b -10= should result in -10 if a+b will overflow.
The input method is not expected to change the calling object.
Implement this in a class called SimpleCalculator . Write tests to thoroughly test your class.
The SmartCalculator implementation
A smart calculator accepts inputs like a normal calculator. This calculator is backward compatible with the simple calculator (i.e. it can handle everything the simple calculator can). Due to limited processing power it too cannot work with whole numbers longer than 32 bits. However this calculator can also handle the following "smart" inputs:
Input "=" multiple times: 32+24= produces 56 as before. However 32+24== and 32+24=== are also valid input sequences, and produce 80 and 104 respectively.
Skipping the second operand: the input 32+= produces 64. The input 32+== produces 96, and so on. The state at the end of each "=" is the result of the computation thus far.
Two consecutive operators: 32+-24= ignores the first operator, and produces 8 as the result.
Begin with operator: +32-24= ignores the "+" and produces 8 as the result. **Note that this only applies to the '+' operator as it has a mathematical meaning when it comes before an operand. All other operators before operands are invalid. **
Like SimpleCalculator it does not allow negative inputs although it can handle negative numbers, and it uses IllegalArgumentException to report all invalid inputs and sequences. However it throws a RuntimeException if a valid input causes an operand to overflow. If the operand does not overflow but the result of the arithmetic does, then the result reported should be 0. For example, a + b -10= should result in -10 if a+b will overflow.
The input method is not expected to change the calling object.
Please note that you are expected to cover only the above types of smart inputs (types, not just the specific examples above).
Implement this in a class called SmartCalculator . Write tests to thoroughly test your class.
Tests
Write tests for your implementations. You may be able to abstract some tests that are common to both implementations.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!