Write a java program THAT USES STACKS data type push pop etc

Write a java program THAT USES STACKS data type (push, pop, etc).

Create a Calculator w/ GUI

Write a program that graphically displays a working calculator for simple infix expressions that consist of: single-digit operands, the operators: +, -, *, and /, and parentheses.

Make the following assumptions:

unary operators (e.g. -2) are illegal

all operations, including division, are integer operations (and results are integers)

the input expression contains no embedded spaces and no illegal characters

the input expression is a syntactically correct infix expression

division by zero will not occur (consider how you can remove this restriction)

Create a GUI application, the calculator has a display and a keypad of 20 keys, which are arranged as follows:

C

<

Q

/

7

8

9

*

4

5

6

-

1

2

3

+

0

(

)

=

As the user presses keys to enter an infix expression, the corresponding characters appear in the display. The C (Clear) key erases all input entered so far; the < (Backspace) key erases the last character entered. When the user presses the = key, the expression is evaluated and the result appended to the right end of the expression in the display window. The user can then press C and enter another expression. If the user presses the Q (Quit) key, the calculator ceases operation and is erased from the screen.

C

<

Q

/

7

8

9

*

4

5

6

-

1

2

3

+

0

(

)

=

Solution

import java.util.Stack;
import java.util.*;
import java.io.*;


public class StackCalculation
{
public static void main (String[] args) throws Exception
{
String[] str = {\"22+\"};
String string = convertStringArrayToString(str);
System.out.println(string);
Stack<String> stack = new Stack<String>();
stack.add(string);
double a = 0;
double b = 0;
double sum = 0;
while (!stack.isEmpty())
{
if(stack.peek().equals(\"+\"))
{
  
       String elem = stack.peek();
a = Double.parseDouble(elem);

stack.pop();
b = Double.parseDouble(stack.peek());
stack.pop();
sum = a + b;
//Write something to push back the sum of the last 2 numbers a + b
stack.push(\"\" + sum);
}

else if(stack.peek().equals(\"*\"))
{
String elem = stack.peek();
a = Double.parseDouble(elem);
stack.pop();
String elem = stack.peek();
b= Double.parseDouble(elem);
stack.pop();
sum = a * b;
//Write something to push back the product of the last 2 numbers a * b
stack.push(\"\" + sum);
}

else if(stack.peek().equals(\"-\"))
{
String elem = stack.peek();
a = Double.parseDouble(elem);
stack.pop();
String elem = stack.peek();
b= Double.parseDouble(elem);
stack.pop();
sum = a - b;
//Write something to push back the difference of the last 2 numbers a - b
stack.push(\"\" + sum);
}

else if(stack.peek().equals(\"/\"))
{
String elem = stack.peek();
a = Double.parseDouble(elem);
stack.pop();
String elem = stack.peek();
b = Double.parseDouble(elem);
stack.pop();
sum = a / b;
//Write something to push back the quotient of the last 2 numbers a/b
stack.push(\"\" + sum);
}

else
{
//Convert the last item in the stack to a double and push
//this into the stack
String elem = stack.peek();
a = Double.parseDouble(elem);
stack.push(\"\" + a);
}

}

System.out.println(stack);
//System.out.println(string);
}

private static String convertStringArrayToString(String[] strArr) {
StringBuilder sb = new StringBuilder();
for(String str : strArr) sb.append(str);
return sb.toString();
}
}

Write a java program THAT USES STACKS data type (push, pop, etc). Create a Calculator w/ GUI Write a program that graphically displays a working calculator for
Write a java program THAT USES STACKS data type (push, pop, etc). Create a Calculator w/ GUI Write a program that graphically displays a working calculator for
Write a java program THAT USES STACKS data type (push, pop, etc). Create a Calculator w/ GUI Write a program that graphically displays a working calculator for

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site