1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
leonid [27]
3 years ago
14

The second programming project involves writing a program that accepts an arithmetic expression of unsigned integers in postfix

notation and builds the arithmetic expression tree that represents 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 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 R0 5 9 Sub R1 3 R0 Mul R2 2 3 Div R3 R1 R2 It is not necessary to reuse registers within an expression as shown in module 2, section II-B, and you can assume there are as many available as needed. Each new expression should, however, begin using registers starting at R0. 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. You may assume that the expression is syntactically correct with regard to the order of operators and operands, but you should check for invalid tokens, such as characters that are not valid operators or operands such as 2a, which are not valid integers. If an invalid token is detected a RuntimeException should be thrown and caught by the main class and an appropriate error message should be displayed.
Engineering
1 answer:
Tpy6a [65]3 years ago
3 0

Answer:

Explanation:

Note: In case of any queries, just comment in box I would be very happy to assist all your queries

SourceCode:

// 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:

e Console X terminated PostfixTolnfix [Java Application] C:\Program Files\Java\jrel.8.0_121\bin\javaw.exe Postfix : ABD++C-D/ .

You might be interested in
Technician A says that applying too much braking force can cause tires to lose traction. Technician B says that ABS systems have
Deffense [45]

Answer:

technically both are correct but tech b is more correct because generally the older the vehicle the worse abs is newer vehicles have abs that no matter how hard you push on the brake peddle the wheels shouldnt lock up

Explanation:

7 0
4 years ago
The snowmobile has a weight of 250 lb, centered at G1, while the rider has a weight of 150 lb, centered at G2. Ifh=3ft, determin
KATRIN_1 [288]

Answer:

See explaination

Explanation:

Please kindly check attachment for the step by step solution of the given problem.

5 0
4 years ago
This came off my car earlier today. What is it and what does it do?
patriot [66]

Answer:

get a new car or duck tape it back on or go get it fixed

Explanation:

that's just whut I wold do.

6 0
3 years ago
Read 2 more answers
Quốc gia được cấu thành từ mấy yếu tố
kirill [66]
Về phương diện công pháp quốc tế thì một chủ thể được xem là quốc gia khi có đầy đủ các yếu tố sau: lãnh thổ, dân cư và có chính quyền. Quốc gia là chủ thể quan trọng của quan hệ pháp luật quốc tế.
8 0
3 years ago
A wastewater is to be disinfected using ultraviolet (UV) light. Batch experiments find that the bacterial concentration decays u
Ksivusya [100]

Answer:

k = 0.1118 per min

Explanation:

Assume;

Initial number of bacteria = N0

Number of bacteria IN 'T' time = Nt

So,

Nt=N0e^{-kt}\\\\in\ 6.2 min\\\\\\frac{N0}{2}= N0e^{-k(6.2)}\\\\ln\frac{1}{2} = -k[6.2]

k = 0.1118 per min

7 0
3 years ago
Other questions:
  • Describe the basic mechanism by which positive photolithography transfers a pattern, and comment on the chemical interactions of
    11·1 answer
  • An electric field is expressed in rectangular coordinates by E = 6x2ax + 6y ay +4az V/m.Find:a) VMN if point M and N are specifi
    14·1 answer
  • How is an electric generator like an electric motor in reverse?
    13·1 answer
  • In a hydroelectric power plant, water enters the turbine nozzles at 780 kPa absolute with a low velocity. If the nozzle outlets
    11·1 answer
  • A cylindrical specimen of Aluminium having a diameter of 12.8 mm and gauge length of 50.8 is pulled in tension. Use the data giv
    5·1 answer
  • A piece of corroded steel plate was found in a submerged ocean vessel. It was estimated that the original area of the plate was
    14·1 answer
  • Rainfall rates for successive 20-min period of a 140min storm are 1.5, 1.5, 6.0, 4.0, 1.0, 0.8, and 3.2 in/hr, totaling 6.0in. D
    12·1 answer
  • On some engines after torquing cylinder head fasteners you must
    12·1 answer
  • (a)Identify the relevant population for the below foci, and suggest the appropriate sampling
    10·1 answer
  • Free poînts lol Philippînes - cool<br>​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!