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]
2 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]2 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
How to update android 4.4.2 to 5.1​
faust18 [17]

Answer:

try settings and go to updates?

Explanation:

8 0
2 years ago
"At 195 miles long, and with 7,325 miles of coastline, the Chesapeake Bay is the largest and most complex estuary in the United
Paraphin [41]

Answer:

see explaination

Explanation:

Part a) Width of bay at Potomac River:

Given Data:

· Actual Width at Potomac River = 30 miles

· Bay Model Length Ratio Lr = 1/1000

In fluid mechanics models of real structures are prepared in simulation so that they can be analyzed accurately. A model is known to have simulation if model carries same geometric, kinematic and dynamic properties at a small scale.

Length of any part in model = Actual length x Lr

Hence,

Model Width of bay at Potomac River = 30 x 1/1000 = 0.03 miles

Since 1 mile = 5280 ft

Model Width of bay at Potomac River = 0.03 x 5280 = 158.4 ft

Part b) Model Length of bay bridge in model:

Given Data:

· Actual Length of bay bridge = 4.3 miles

· Bay Model Length Ratio Lr = 1/1000

Model Length = Actual Length x Lr = 4.3 x 1/1000 = 0.0043 miles

Since 1 mile = 5280 ft

Model Length in feet = 0.0043 x 5280 = 22.704 ft

Part c) Model Length of bay bridge in model:

Given Data:

· Model Area = 8 acre

· Bay Model Length Ratio Lr = 1/1000

Model Area = Actual Area x Lr x Lr

8 Model Area :: Actual Area =- (Lp)2 2 = 8,000,000 acre 1000

Since 1 square mile = 640 acre,

Actual Area in square miles = 8,000,000/640 = 12,500 square miles

Part d) Average and maximum depth of model:

Given Data:

· Actual Average depth = 28 ft

· Actual Maximum depth = 174 ft

· Bay Model Length Ratio Lr = 1/1000

Model average depth = Actual average depth x Lr = 28 x 1/1000 = 0.028 feet

Since 1 ft = 12 inch

Model average depth in inch = 0.028 x 12 = 0.336 in

Model maximum depth = Actual maximum depth x Lr = 174 x 1/1000 = 0.174 feet

Since 1 ft = 12 inch

Model maximum depth in inch = 0.174 x 12 = 2.088 in

4 0
3 years ago
All air-conditioning units must be grounded electrically to
Tpy6a [65]

Answer:

Prevent electrical shock

Grounding is a non current carrying conductor mainly used to guard against hazards due to leakage in electric circuits

Explanation:

The grounding refers to the connection of an electrical equipment exposed metallic parts to the ground to serve as a source of current flow in the event of an insulation failure will cause the fuses to trip thereby isolating or removing electric power from the device

Grounding also prevents the accumulation of static electricity which can be a source of fire in inflammable areas.

8 0
3 years ago
3. (a) (5 points) Suppose N packets arrive simultaneously to a link at which no packets are currently being transmitted or queue
Bezzdna [24]

Answer:

(N-1) × (L/2R) = (N-1)/2

Explanation:

let L is length of packet

R is rate

N is number of packets

then

first packet arrived with 0 delay

Second packet arrived at = L/R

Third packet arrived at = 2L/R

Nth packet arrived at = (n-1)L/R

Total queuing delay = L/R + 2L/R + ... + (n - 1)L/R = L(n - 1)/2R

Now

L / R = (1000) / (10^6 ) s = 1 ms

L/2R = 0.5 ms

average queuing delay for N packets = (N-1) * (L/2R) = (N-1)/2

the average queuing delay of a packet = 0 ( put N=1)

4 0
3 years ago
The ingredient weights for making 1 yd (cyd) of concrete by assuming aggregates in SSD state are given below. The volume of air
Pachacha [2.7K]

Answer:

Explanation:

Ans) Given batch weight of each component :

Cement = 700 lb

Water = 315 lb

Coarse aggregate = 1575 lb

Fine aggregate = 1100 lb

Part 1) Amount of water = 328.5 lb

Amount of water is needed to be increased if the aggregates has absorption capacity, To maintain constant water cement ratio, the mixing water is increased because some of the water is absorbed by aggregates.

Amount of water absorbed = 328.5 lb - 315 lb = 13.5 lb

Total amount of aggregates = 1575 + 1100 = 2675 lb

=> % Absorption capacity = 13.5 x 100 / 2675 = 0.5 %

Hence, new amount of Coarse aggregate = (1 - 0.005) x 1575 lb = 1567.125 lb

New amount of fine aggregate = (1 - 0.005) x 1100 = 1094.5 lb

Since, water cement ratio is maintained constant , amount of cement remains unchanged

=> Volume of water = 328.5 / 62.4 = 5.26 ft3

=> Volume of cement = 700 / (3.15 x 62.4) = 3.56 ft3

=> Volume of coarse aggregate = 1567.125 / (2.4 x 62.4) = 10.46 ft3

=> Volume of fine aggregate = 1100 / (2.4 x 62.4) = 7.34 ft3

Volume of air = 2% = 0.02 x 27 = 0.54 ft3

Total concrete volume = 5.26 + 3.56 + 10.46 + 7.34 + 0.54 \approx 27 ft3 = 1 yd3

Hence, calculated amount of each component is correct

Part 2) We know, minus sign indicated that the aggregate will absorb some moisture from concrete, hence mixing water amount needed to be corrected .

=> Amount of water absorbed by coarse aggregate = 0.01 x 1567.125 lb = 15.67 lb

=> Amount of water absorbed by fine aggregate = 0.02 x 1094.50 lb = 21.89 lb

Total amount of water absorbed = 15.67 + 21.89 = 37.56 lb

To maintain same water cement ratio, amount of mixing water is needed to be increased

=> Corrected amount of mixing water = 328.5 lb + 37.56 lb = 366 lb

=> Corrected amount of coarse aggregate = (1 - 0.01) x 1567.125 = 1551.45 lb

=> Corrected amount of fine aggregate = (1 - 0.02) x 1094.5 = 1072.6 lb

Part 3) We know,

Unit weight = Sum of weight of each material / Total volume

=> Sum of weight = 366 + 700 + 1551.45 + 1072.6 = 3690.05 lb

Total volume = 1 yd3 or 27 ft3

=> Expected Unit Weight = 3690.05 lb / 27 ft3 = 136.67 lb/ft3

Also, Concrete Yield = Weight of all components / Unit weight of concrete

=> Yield = 3690.05 / 136.67 = 27 ft3 or 1 yd3

4 0
3 years ago
Other questions:
  • g A 30-m-diameter sedimentation basin has an average water depth of 3.0 m. It is treating 0.3 m3/s wastewater flow. Compute over
    8·1 answer
  • A poundal is the force required to accelerate a mass of 1 lbm at a rate of 1 ft/(s^2). Determine the acceleration of an object o
    10·1 answer
  • There have been many attempts to manufacture and market plastic bicycles. All have been too flexible and soft. Which design-limi
    9·1 answer
  • What is the mass of a brass axle that has a volume of 0.318 cm? ​
    6·1 answer
  • A city emergency management agency and a construction company have formed a public-private partnership. The construction company
    15·1 answer
  • Which of the following best describes the relationship between the World Wide Web and the Internet? А The World Wide Web is a pr
    5·1 answer
  • Hi, everyone I'm a high school student in Texas. My engineering teacher is asking us to find an active engineer to complete a li
    5·1 answer
  • For a small company it's usually best to keep the corporate and brand image as___ as possible​
    9·1 answer
  • What type of car engine is best for cold weather.
    15·1 answer
  • A technician is building a high-performance gaming pc. which two items should the technician focus on selecting parts for the ma
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!