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
The formula for the cross sectional area of specimen at the middle is
poizon [28]
Times the radius squared !

hope this helps :)))
8 0
3 years ago
g A smooth pipeline with a diameter of 5 cm carries glycerin at 20 degrees Celsius. The flow rate in the pipe is 0.01 m3/s. What
earnstyle [38]

Answer:

The friction factor is 0.303.

Explanation:

The flow velocity (v), measured in meters per second, is determined by the following expression:

v = \frac{4\cdot \dot V}{\pi \cdot D^{2}} (1)

Where:

\dot V - Flow rate, measured in cubic meters per second.

D - Diameter, measured in meters.

If we know that \dot V = 0.01\,\frac{m^{3}}{s} and D = 0.05\,m, then the flow velocity is:

v = \frac{4\cdot \left(0.01\,\frac{m^{3}}{s} \right)}{\pi\cdot (0.05\,m)^{2}}

v \approx 5.093\,\frac{m}{s}

The density and dinamic viscosity of the glycerin at 20 ºC are \rho = 1260\,\frac{kg}{m^{3}} and \mu = 1.5\,\frac{kg}{m\cdot s}, then the Reynolds number (Re), dimensionless, which is used to define the flow regime of the fluid, is used:

Re = \frac{\rho\cdot v \cdot D}{\mu} (2)

If we know that \rho = 1260\,\frac{kg}{m^{3}}, \mu = 1.519\,\frac{kg}{m\cdot s}, v \approx 5.093\,\frac{m}{s} and D = 0.05\,m, then the Reynolds number is:

Re = \frac{\left(1260\,\frac{kg}{m^{3}} \right)\cdot \left(5.093\,\frac{m}{s} \right)\cdot (0.05\,m)}{1.519 \frac{kg}{m\cdot s} }

Re = 211.230

A pipeline is in turbulent flow when Re > 4000, otherwise it is in laminar flow. Given that flow has a laminar regime, the friction factor (f), dimensionless, is determined by the following expression:

f = \frac{64}{Re}

If we get that  Re = 211.230, then the friction factor is:

f = \frac{64}{211.230}

f = 0.303

The friction factor is 0.303.

4 0
2 years ago
For RTK to work, what do we need besides two or more receivers collecting data from a sufficient number of satellites simultaneo
Liula [17]

Answer:

phase measurement and the information content

Explanation:

The full form of RTK is Real Time Kinematic. It is used for satellite navigation technique to increase the precision of the position data that is derived from the positioning systems based on satellites like the NavIC, GPS, Galileo, BeiDou and GLONASS. It takes help of the measurements of phase of signal's carrier wave and also the information content of these signals and it also relies on the single interpolated virtual station in order to provide the real time corrections and provide correct and accurate information.

7 0
2 years ago
An actual vapour compression system comprises following process represents a. 1-2 Compression process b. 2-3 Condens 1 (or heat
Gemiola [76]

Answer:

Explanation:

The deatailed diagram of VCRS is given below such

1-2=Isentropic compression in which temperature increases at constant entropy

2-3=Isobaric heat rejection i.e. heat rejected at constant pressure(condensation)

3-4=Irreversible expansion or throttling in which enthalpy remains constant

4-1=Isobaric heat addition(Evaporation)

4 0
3 years ago
Let suppose, you are going to develop a web-application for school management system. Then what architectural pattern will you u
Gnesinka [82]

Answer:

The architectural pattern i will use for the school management is the client-server pattern.

This pattern would consist of  a server and many clients. wherein the server component would provide services to that of the clients and its components as specified and also there would be a client request service from that of the server.

Explanation:

Solution

A school management system would always involve the client server pattern as this pattern would have a server and many clients wherein the server component would give services to that of the clients and its components as specified and also there would be a client request service from that of the server. This server would share the appropriate services to such clients and also listen to the client's requests.

Such kind of pattern would mostly be used for for the online platforms or application like that of document.

5 0
3 years ago
Other questions:
  • 7 Single-use earplugs require a professional fitting before they can be used.
    10·2 answers
  • Consider the following pulley system. A block of mass m is connected to a translational spring of stiffness k through a cable, w
    10·1 answer
  • If a barrel of oil weighs 1.5 kN, calculate the specific weight, density, and specific gravity of the oil. The barrel weighs 110
    7·1 answer
  • Plot the following trig functions using subplots, choosing an appropriate layout for the number of functions displayed. The subp
    8·1 answer
  • List the three main methods employed in dimensional analysis
    6·1 answer
  • Buying shop supplies from the shop owner to work on your own car at home is an ethical practice.
    14·1 answer
  • What is the hardest part of thermodynamics?
    5·1 answer
  • The natural material in a borrow pit has a mass unit weight of 110.0 pcf and a water content of 6%, and the specific gravity of
    11·1 answer
  • For many clients, a decision is based primarily on time and money, which are the two most abundant
    10·2 answers
  • Why is communication one of the most important aspects of an engineer's job?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!