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
A DC generator turns at 2000 rpm and has an output of 200 V. The armature constant is 0.5 V-min/Wb, and the field constant of th
WITCHER [35]

Answer:

b. 10A

Explanation:

Using the formula, E= k × r×I

200= 0.5 ×2000×0.02×I

200=20×I

Dividing with 20

I = 200/20= 10A

4 0
3 years ago
Read 2 more answers
A body weighs 50 N and hangs from a spring with spring constant of 50 N/m. A dashpot is attached to the body. If the body is rai
lbvjy [14]

Answer:

a) 3.607 m

b) 1.5963 m

Explanation:

See that attached pictures for explanation.

3 0
3 years ago
A double-threaded Acme stub screw of 2-in. major diameter is used in a jack having a plain thrust collar of 2.5-in. mean diamete
Temka [501]
This is the answer for the question

6 0
2 years ago
Air enters the combustor of a jet engine at p1=10 atm, T1=1000°R, and M1=0.2. Fuel is injected and burned, with a fuel/air mass
snow_lady [41]

Answer:

M2 = 0.06404

P2 = 2.273

T2 = 5806.45°R

Explanation:

Given that p1 = 10atm, T1 = 1000R, M1 = 0.2.

Therefore from Steam Table, Po1 = (1.028)*(10) = 10.28 atm,

To1 = (1.008)*(1000) = 1008 ºR

R = 1716 ft-lb/slug-ºR cp= 6006 ft-lb/slug-ºR fuel-air ratio (by mass)

F/A =???? = FA slugf/slugaq = 4.5 x 108ft-lb/slugfx FA slugf/sluga = (4.5 x 108)FA ft-lb/sluga

For the air q = cp(To2– To1)

(Exit flow – inlet flow) – choked flow is assumed For M1= 0.2

Table A.3 of steam table gives P/P* = 2.273,

T/T* = 0.2066,

To/To* = 0.1736 To* = To2= To/0.1736 = 1008/0.1736 = 5806.45 ºR Gives q = cp(To* - To) = (6006 ft-lb/sluga-ºR)*(5806.45 – 1008)ºR = 28819500 ft-lb/slugaSetting equal to equation 1 above gives 28819500 ft-lb/sluga= FA*(4.5 x 108) ft-lb/slugaFA =

F/A = 0.06404 slugf/slugaor less to prevent choked flow at the exit

5 0
3 years ago
Liquid benzene and liquid n-hexane are blended to form a stream flowing at a rate of 1700 lbm/h. An on-line densitometer (an ins
Taya2010 [7]
Let me think of that
5 0
3 years ago
Other questions:
  • This assignment covers the sequential circuit component: Register and ALU. In this assignment you are supposed to create your ow
    13·1 answer
  • Suppose you are designing a sliding window protocol for a 1 Mbps point-to-point link to the moon, which has a one-way latency of
    10·1 answer
  • (3) In the following power system, the transformer is assumed to be ideal. Determine the: a) currents and voltages across each i
    9·1 answer
  • Two advantages of deforming steel at room temperature rather than at elevated temperatures are: (select 2 answers from the optio
    13·1 answer
  • El tiempo hasta que falle un sistema informático sigue una distribución Exponencial con media de 600hs. (Utilice 3 decimales par
    13·1 answer
  • Can someone please help me with this <br>I've an exams tomorrow ​
    7·1 answer
  • For many clients, a decision is based primarily on time and money, which are the two most abundant
    10·2 answers
  • A parallel circuit has a resistance of 280 and an inductive reactance of 360 02. What's this circuit's impedance?
    6·1 answer
  • which type of irrigation fluid is typically used for endoscopic procedures using monopolar electrosurgery
    15·1 answer
  • A pipe fitter would fabricate which one of the following systems?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!