Question

(In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I...

(In Java)

you will be creating a GUI for your calculator that evaluates the postfix expression. I have already created the postfix class that converts the expression from infix to postfix.

Here's the postFix class and its stack class

(https://paste.ofcode.org/bkwPyCMEBASXQL4pR2ym43) ---> PostFix class

(https://paste.ofcode.org/WsEHHugXB38aziWRrp829n)--------> Stack class

Your calculator should have:

  • Text field for the postfix expression
  • Text field for the result
  • Two buttons: Evaluate and Clear

You can start with the code written below and improvise to work with this assignment. This program has two class this I have provided below.

(https://paste.ofcode.org/iq4Y4JKdgqMmJBRjxiNYN2)---->UI class

(https://paste.ofcode.org/nh5vnhdZg33JBNuamPyVen)-----> Distance class

The user types in a valid postfix expression and clicks Evaluate. The GUI evaluates the postfix expression and displays the result.

Your calculator should support the following operators:

+ - * / ^ sqrt swap dup rot

You must have at least the following classes in this project:

  • Postfix
  • PostfixTest
  • The GUI class

You may use Java's built-in Stack class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Postfix.java

import java.util.Stack;

public class Postfix {
  
// default constructor
public Postfix(){}
  
// method that decides the priority of each operator
static int precedence(char c)
{
switch(c)
{
case '+':
case '-':
return 1;
  
case '*':
case '/':
return 2;
  
case '^':
return 3;
}
return -1;
}
  
// method to convert an infix expression to a postfix expression
static String infixToPostfix(String expression)
{
String result = "";
  
Stack<Character> stack = new Stack<>();
  
for(int i = 0; i < expression.length(); i++)
{
char ch = expression.charAt(i);
if(Character.isLetterOrDigit(ch))
result += ch;
else if(ch == '(')
stack.push(ch);
else if(ch == ')')
{
while(!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if(!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression Encountered!";
else
stack.pop();
}
else
{
while(!stack.isEmpty() && precedence(ch) <= precedence(stack.peek()))
result += stack.pop();
stack.push(ch);
}
}
while(!stack.isEmpty())
result += stack.pop();
  
return result;
}
}

PostfixTest.java

public class PostfixTest {
  
// default constructor
public PostfixTest(){}
  
public static String getPostfixExpression(String expression)
{
return Postfix.infixToPostfix(expression);
}
}

InfixToPostfixCalculatorUI.java (Main class)

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class InfixToPostfixCalculatorUI {
  
private final JFrame mainFrame;
private final JPanel fieldPanel, buttonPanel, mainPanel;
private final JLabel infixLabel, postfixLabel;
private JTextField infixField, postfixField;
private final JButton evaluateButton, clearButton;
  
public InfixToPostfixCalculatorUI()
{
mainFrame = new JFrame();
mainPanel = new JPanel(new GridLayout(2, 1)); // row = 2, col = 1
  
// setting up the panel for containing the labels and the textfields
fieldPanel = new JPanel(new GridLayout(2, 1, 5, 25)); // row = 2, col = 1, horizontal gap between components = 5 and vertical gap between components = 25
infixLabel = new JLabel("Infix Expression:");
postfixLabel = new JLabel("Postfix Expression:");
infixField = new JTextField(20);
infixField.setHorizontalAlignment(SwingConstants.CENTER);
postfixField = new JTextField(20);
postfixField.setHorizontalAlignment(SwingConstants.CENTER);
postfixField.setEditable(false);
fieldPanel.add(infixLabel);
fieldPanel.add(infixField);
fieldPanel.add(postfixLabel);
fieldPanel.add(postfixField);
  
// setting up the panel for containing the 2 buttons
buttonPanel = new JPanel(new FlowLayout(1, 10, 25));
evaluateButton = new JButton("Evaluate");
clearButton = new JButton("Clear");
buttonPanel.add(evaluateButton);
buttonPanel.add(clearButton);
  
// adding the 2 panels to the main panel
mainPanel.add(fieldPanel);
mainPanel.add(buttonPanel);
mainFrame.add(mainPanel);
  
mainFrame.setTitle("Infix To Postfix Calculator");
mainFrame.setSize(350, 200);
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
  
// action listener for evaluate button
evaluateButton.addActionListener((ActionEvent e) -> {
if(infixField.getText().equals(""))
JOptionPane.showMessageDialog(null, "Please provide a valid Infix expression!", "Field(s) Missing Alert"
, JOptionPane.ERROR_MESSAGE);
else
{
evaluateButton.setEnabled(false);
String infxiExpression = infixField.getText().trim();
postfixField.setText(PostfixTest.getPostfixExpression(infxiExpression));
}
});
  
clearButton.addActionListener((ActionEvent e) -> {
infixField.setText("");
postfixField.setText("");
evaluateButton.setEnabled(true);
});
}
  
public static void main(String[] args)
{
InfixToPostfixCalculatorUI infixToPostfixCalculatorUI = new InfixToPostfixCalculatorUI();
}
}

Add a comment
Know the answer?
Add Answer to:
(In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • You are to write a program name expressionTree.java that evaluates an infix expression entered by the...

    You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2)   One alphabetic character - "x" (representing a value to be supplied later). (3)   Binary operators (+, -, *, / and % (modulo)). (4)   Parentheses          You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • Evaluateeg EvaluateInFix.java: import java.util.Stack; import java.util.Scanner; public class EvaluateInfix {    public static void main(String[] args)...

    Evaluateeg EvaluateInFix.java: import java.util.Stack; import java.util.Scanner; public class EvaluateInfix {    public static void main(String[] args)    {    Scanner keyboard = new Scanner(System.in); System.out.println("This program an inFix Expression: "); System.out.print("Enter an inFix Expression that you want to evaluate (or enter ! to exit): "); String aString = keyboard.nextLine();    while (!aString.equals("!")) {    System.out.println (evaluateinFix(aString));    System.out.print("Enter an inFix Expression that you want to evaluate (or enter ! to exit): ");    aString = keyboard.nextLine(); } // end while...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT