Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

All your code should be in a package called calculator. Your tests should be in the default package. This ensures that your test can see

All your code should be in a package called "calculator". Your tests should be in the default package. This ensures that your test can see your classes and interfaces in the same way as any other client.
1)The Calculator interface
Write an interface Calculator that represents a single calculator. This interface should contain the following:
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.
2)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.
3)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.
Implement this in a class called SmartCalculator.Write tests to thoroughly test your class.
4)Tests
Write tests for your implementations. You may be able to abstract some tests that are common to both implementations. Read the test notes on the course web page to help you design tests effectively.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

State the uses of job description.

Answered: 1 week ago

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago