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
1. Fatigue equations are based solely on theoretical assumptions. Experimental data is only used to verify the theory. a. True.b
Rainbow [258]

Answer:

1.  b. False

2. b. False

3.  b. False

4.  b. False

5. a. True

6. a. True

7.  b. False

8.  b. False

9. a. True

Explanation:

1. The fatigue properties of a material  are determined by series of test.

2. For most steels there is a level of fatigue limit below which a component will survive an infinite number of cycles, for aluminum and titanium a fatigue limit can not be defined, as failure will eventually occur after enough experienced cycles.

3. Although there is a cyclic stress, there are also stresses complex circumstances involving tensile to compresive and constant stress, where the solution is given into the mean stress and the stress amplitude or stress range, which is double the stress amplitude.

4. Low‐cycle fatigue is defined as few thousand cycles and high cycle fatigue is around more than 10,000 cycles.

5. The number of cycles for failure on brittle materials are less and determined compared with the ductile materials.

6.  The bending fatigue could be handled with specific load requirements  for uniform bending or axial fatigue of the same section size where the material near the surface is subjected to the  maximum stress, as in torsional fatigue, which can be performed on  axial-type specially designed machines also, using the proper fixtures if  the maximum twist required is small, in which linear motion is changed to rotational motion.

7.  A SN-Curve for a given material, is a plot displayed on logarithmic scales of the magnitude of an alternating stress in relation to the number of cycles to failure

8. The strain life method measures the strain resistance of local stresses and strains around stress concentration that controls the fatigue life of the material. It is more accurate than determining fatigue performance as the stress-life method is for long life millions of cycles in elastic stresses, but an it gets an effective stress concentration in fatigue loading.

9. Linear Elastic Fracture Mechanics (LEFM) states that the material is isotropic and linear elastic so, when the stresses near the crack surpasses the material fracture toughness, the crack grows.

7 0
3 years ago
What's the difference between accuracy and percision in measuring and gaging?
lidiya [134]

Answer:

The term Accuracy means that how close our result to the original result.

Suppose we do any experiment in laboratory and we calculate mass = 7 kg but answer is mass = 15 kg then our answer is not accurate.

And the term Precision means how likely we get result like this.

Suppose we do any experiment in laboratory and we calculate mass five times and each time we get mass = 7 kg then our answer is precised but not accurate.

4 0
3 years ago
Anaircraft component is fabricated from an aluminum alloy that has a plane-strain fracture toughness of 40 MPa 1/2.It has been d
navik [9.2K]

Answer:

Yes, fracture will occur since toughness (42.4 MPa) is greater than the toughness of the material, 40MPa

Explanation:

Given

Toughness, k = 40Mpa

Stress, σ = 300Mpa

Length, l = 4mm = 4 * 10^-3m

Under which fracture occurred (i.e., σ= 300 MPa and 2a= 4.0 mm), first we solve for parameter Y (The dimensionless parameter)

Y = k/(σπ√a)

Where a = ½ of the length in metres

Y = 40/(300 * π * √(4/2 * 10^-3))

Y = 1.68 ---- Approximated

To check if fracture will occur of not; we apply the same formula.

Y = k/(σπ√a)

Then we solve for k, where

σ = 260Mpa and a = ½ * 6 * 10^-3

So,.we have

1.68 = k/(260 * π * √(6*10^-3)/2)

k = 1.68 * (260 * π * (6*10^-3)/2)

k = 42.4 MPa --- Approximately

Therefore, fracture will occur since toughness (42.4 MPa) is greater than the toughness of the material, 40 MPa

7 0
3 years ago
Read 2 more answers
The substance is steam (H2O). NOTE: The purpose of this problem is to illustrate that there are conditions where water vapor is
Gennadij [26K]

Answer:

See the attached pictures for detailed answer.

Explanation:

See the attached pictures for step by step explanation.

7 0
3 years ago
A 30 mm thick AISI 1020 steel plate is sandwiched between two 10 mm thick 2024-T3 aluminum plates and compressed with a bolt and
denis-greek [22]

Answer:

275 MPa

Explanation:

Regardless of what it is holding, the stiffness of a bolt depends on its own material properties and geometry.

The stiffness is:

k = E * \frac{A}{l}

I assume this one is made of steel, because regular bolts are steel.

The Young's modulus for steel is E = 210 GPa

The longitude is given. (But note that in a real application you have to consider the length up to the nut.)

The section is (using the nominal diameter of 10 mm)

A = \frac{\pi * d^2}{4} = \frac{\pi * 0.01^2}{4} = 7.85e-5 m^2

Then:

k  = 2.1e11 * \frac{7.85e-5}{0.06} = 275e6 Pa = 275 MPa

5 0
3 years ago
Other questions:
  • In what situation you would prefer to use a successive approximation ADC over flash ADC?
    13·1 answer
  • Time complexity of merge sort
    15·1 answer
  • If 5000 N of thrust is acting to the left, and 4300 N of drag is acting to the right, what is the magnitude and direction of the
    11·1 answer
  • g A pump is required to deliver 100 gpm at a head of 100 ft, but the pump rated capacity is 150 gpm at a head of 100 ft. If the
    9·1 answer
  • Determine the resultant normal force across the cross section at point B. Express your answer to three significant figures and i
    6·1 answer
  • A horse on the merry-go-round moves according to the equations r = 8 ft, u = (0.6t) rad, and z = (1.5 sin u) ft, where t is in s
    5·1 answer
  • If you are in a tornado situation, which of the following actions would put you in danger?
    11·1 answer
  • Consider a single crystal of nickel oriented such that a tensile stress is applied along a [001] direction. If slip occurs on a
    6·1 answer
  • Which regulations are related to guard rail height and dimensions and uniformity of stairs?
    7·2 answers
  • Select the correct answer.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!