Subject Computer Science Java Programming

Question

Use the STATE DESIGN PATTERN to implement a simple calculator. The calculator will accept only positive integer numbers and two types of operators, + and -. For example, an input could be 9 + 10 + 10 - 2
1) You should design the state machine first and then translate it into the state design pattern
2) Your calculator must compute correctly the expression
3) You must handle the cases where users type in invalid inputs (e.g. 10.3 or *)
Please submit your resulting code and a short Word document explaining the rationale behind your code.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

* Calculator class
*/
public class Calculator {

// State variable
private CalculatorState state;

// Constructor
public Calculator() {
    reset();
}

// Method to reset state machine
public void reset() {
    this.state = new AdditionState(0);
}

// Method to calculate input line
public int calculate(String line) throws NumberFormatException {
    for (char c: line.toCharArray...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor
Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats