Question

The second programming project involves writing a program that accepts an arithmetic xpression of unsigned integers in postfix notation and builds the arithmetic expression tree that epresents that expression. From that tree, the corresponding fully parenthesized infix expression should be displayed and a file should be generated that contains the three address format instructions. This topic is discussed in the week 4 reading in module 2, section II-B. The main class should create the GUI shown below Three Adddress Generator Enter Postfix Expression 359-23 Construct Tree Infix Expression ((3-(5 9))1(2 3)) The GUI must be generated by code that you write. You may not use a drag-and-drop GUI generator Pressing the Construct Tree button should cause the tree to be constructed and using that tree, the corresponding infix expression should be displayed and the three address instruction file should be generated. The postfix expression input should not be required to have spaces between every token. Note in the above example that 9+- are not separated by spaces. The above example should produce the following output file containing the three address instructions: Add RO 59 Sub R1 3 RO Mul R2 2 3 Div R3 R1 R2 It is not necessary to reuse registers within an expression as shown in module 2, section I1-B, and you can assume there are as many available as needed. Each new expression should, however, begin using registers starting at RO Inheritance should be used to define the arithmetic expression tree. At a minimum, it should involve three classes: an abstract class for the tree nodes and two derived classes, one for operand nodes and another for operator nodes. Other classes should be included as needed to accomplish good object-oriented design. All instance data must be declared as private.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:-

// MyGUI.java:

// Import packages

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.EmptyStackException;

import java.util.Stack;

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;

// Declaare and define the class MyGUI

abstract class MyGUI extends JFrame implements ActionListener {

JTextField userInput;

JLabel inputDescLbl, resultLbl;

JPanel inputPanel, resultPanel;

JButton evlBtn;

Stack<Object> stk;

// Define the constructor MyGUI

MyGUI() {

super("Tree Address Generator");

inputPanel = new JPanel(new FlowLayout());

resultPanel = new JPanel(new FlowLayout());

setLayout(new GridLayout(2, 1));

userInput = new JTextField(20);

inputDescLbl = new JLabel("Enter Postfix Expression:");

evlBtn = new JButton("Construct Tree");

evlBtn.addActionListener(this);

resultLbl = new JLabel("Infix Expression:", SwingConstants.LEFT);

add(inputPanel);

add(resultPanel);

inputPanel.add(inputDescLbl);

inputPanel.add(userInput);

inputPanel.add(evlBtn);

resultPanel.add(resultLbl);

stk = new Stack<Object>();

}

}

//Stack.java:

// Declare and define the class Stack

class Stack {

private int[] a;

private int top, m;

public Stack(int max) {

m = max;

a = new int[m];

top = -1; }

public void push(int key) {

a[++top] = key; }

public int pop() {

return (a[top--]); }

}

// Declare and define the class Evaluation()

class Evaluation {

public int calculate(String s) {

int n, r = 0;

n = s.length();

Stack a = new Stack(n);

for (int i = 0; i < n; i++) {

char ch = s.charAt(i);

if (ch >= '0' && ch <= '9')

a.push((int) (ch - '0'));

else if (ch == ' ')

continue;
else {

int x = a.pop();

int y = a.pop();

switch (ch) {

case '+':

r = x + y;

break;

case '-':

r = y - x;

break;

case '*':

r = x * y;

break;

case '/':

r = y / x;

break;

default:

r = 0;

}

a.push(r);

}

}

r = a.pop();

return (r);

}

}

// PostfixToInfix.java:

// Import packages

import java.util.Scanner;

import java.util.Stack;

// Declare and define the class PostfixToInfix

class PostfixToInfix {

// Determine whether the character entered is an operator or not

private boolean isOperator(char c) {

if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')

return true;

return false;

}

// Declare and define the convert()

public String convert(String postfix) {

Stack<String> s = new Stack<>();

for (int i = 0; i < postfix.length(); i++) {

char c = postfix.charAt(i);

if (isOperator(c)) {

String b = s.pop();

String a = s.pop();

s.push("(" + a + c + b + ")");

} else

s.push("" + c);

}

return s.pop();

}

// Program starts from main()

public static void main(String[] args) {

PostfixToInfix obj = new PostfixToInfix();

Scanner sc = new Scanner(System.in);

// Prompt the user to enter the postfix expression

System.out.print("Postfix : ");

String postfix = sc.next();

// Display the expression in infix expression

System.out.println("Infix : " + obj.convert(postfix));

}

}

OUTPUT:-

MyGUL.java Stack.java Evalution.java PostfixTolnfix.java 3 35 36 37 38 39 if (isoperator(c)) string b = s.pop(); String a-s.pop(); s.push( a c+b+ )); f else s,push( + c); return s.pop(); 9 50 51 52 53 54 // Program starts from main() 55 public static void main(String[] args) 56 57 58 59 60 61 62 63 64 65 PostfixToInfix objnew PostfixToInfix); Scanner sc-new Scanner(System.in); // Prompt the user to enter the postfix expression System.out.print(Postfix : ); string postfix = sc.next(); // Display the expression in infix expression System.out.println(Infix: obj.convert(postfix)); 67 68 69 70 71 72 73 PostfixABD++C-D/ Infix : (((A+ (B+D))-C)/D)

Add a comment
Know the answer?
Add Answer to:
The second programming project involves writing a program that accepts an arithmetic xpression of unsigned integers...
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
  • The second project involves completing and extending the C++ program that evaluates statements of an expression...

    The second project involves completing and extending the C++ program that evaluates statements of an expression language contained in the module 3 case study. The statements of that expression language consist of an arithmetic expression followed by a list of assignments. Assignments are separated from the expression and each other by commas. A semicolon terminates the expression. The arithmetic expressions are fully parenthesized infix expressions containing integer literals and variables. The valid arithmetic operators are +, –, *, /. Tokens...

  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from an input file. This program consists of two classes. The first class is the Weight class, which is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods and two private methods: 1. A public constructor that allows...

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