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
Why does an object under forced convection reach a steady-state faster than an object subjected to free-convection?
bonufazy [111]

Answer:

Free convection:

   When heat transfer occurs due to density difference between fluid then this type of heat transfer is know as free convection.The velocity of fluid is zero or we can say that fluid is not moving.

Force convection:

   When heat transfer occurs due to some external force then this type of heat transfer is know as force convection.The velocity of fluid is not zero or we can say that fluid is moving in force convection.

Heat transfer coefficient of force convection is high as compare to the natural convection.That is why heat force convection reach a steady-state faster than an object subjected to free-convection.

We know that convective heat transfer given as

 q = h  A ΔT

h=Heat transfer coefficient

A= Surface area

ΔT = Temperature difference

5 0
3 years ago
Question text
lisabon 2012 [21]

Answer:

That's a really nice question sadly I don't know the answer I'm replying to you cuz I'm tryna get points so... Sorry

3 0
3 years ago
Given in the following v(t) signal.
tatuchka [14]

Answer:

Check the v(t) signal referred to in the question and the solution to each part in the files attached

Explanation:

The detailed solutions of parts a to d are clearly expressed in the second file attached.

3 0
3 years ago
A thick steel slab (rho= 7800 kg/m3 , cp= 480 J/kg K, k= 50 W/m K) is initially at 300 °C and is cooled by water jets impinging
dimaraw [331]

Answer:

t = 2244.3 sec

Explanation:

calculate the thermal diffusivity

\alpha = \frac{k}{\rho c}

           = \frac{50}{7800\times 480} = 1.34 \times 10^{-5} m^2/s

                   

Temperature at 28 mm distance after t time  = =  50 degree C

we know that

\frac[ T_{28} - T_s}{T_i -T_s} = erf(\frac{x}{2\sqrt{at}})

\frac{ 50 -25}{300-25} = erf [\frac{28\times 10^{-3}}{2\sqrt{1.34\times 10^{-5}\times t}}]

0.909 = erf{\frac{3.8245}{\sqrt{t}}}

from gaussian error function table , similarity variable w calculated as

erf w = 0.909

it is lie between erf w = 0.9008  and erf w = 0.11246 so by interpolation we have

w = 0.08073

erf 0.08073 = erf[\frac{3.8245}{\sqrt{t}}]

0.08073 = \frac{3.8245}{\sqrt{t}}

solving fot t we get

t = 2244.3 sec

3 0
3 years ago
Let r be a bank's interest rate in percent per year (APR). An initial amount of money P, also called as principal, will mature t
Liono4ka [1.6K]

Answer:

See explanation below.

Explanation:

For this case the program needs to take the inputs as P,r and n and the output would be as A and printed on the system. The code is:

# Inputs

P = float(input("Enter the present value : "))  

r = float(input("Enter your APR : "))  

n = float(input("Enter the number of years : ") )

# Output

A = P*(1 +(r/100))**n

print("The future values is:", A)  

And the result obtained is:

Enter the present value : 1000

Enter your APR : 0.95

Enter the number of years : 5

The future values is: 1048.4111145526908

7 0
3 years ago
Other questions:
  • Why is it important to stop climate change?
    15·2 answers
  • Let Deterministic Quicksort be the non-randomized Quicksort which takes the first element as a pivot, using the partition routin
    13·1 answer
  • What is ONE DIFFERENCE between civil structural engineering
    13·1 answer
  • Basic output with variables (Java) This zyLab activity is intended for students to prepare for a larger programming assignment.
    7·1 answer
  • 1. Given: R= 25 , E = 100 V<br> Solve for I
    5·1 answer
  • The value of universal gas constant is same for all gases?<br> a) yes<br> b)No
    15·1 answer
  • Which of the following about valence electron is correct?
    10·2 answers
  • Find the rate of heat transfer through a 6 mm thick glass window with a cross-sectional area of 0.8 m2 if the inside temperature
    15·1 answer
  • Transcript
    7·1 answer
  • From the top of a vertical cliff 80m high, the angles of depression of 2 buoys lying due west of the cliff are 23° and 15° respe
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!