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
What prevented this weld from becoming ropey?
Mama L [17]

Answer:

If I am not mistaken I believe it is a higher voltage.

Explanation:

Hope this helps

8 0
2 years ago
Read 2 more answers
Write a naive implementation (i.e. non-vectorized) of matrix multiplication, and then write an efficient implementation that uti
erik [133]

Answer:

import numpy as np  

import time  

def matrixMul(m1,m2):      

   if m1.shape[1] == m2.shape[0]:  

       

       t1 = time.time()

       r1 = np.zeros((m1.shape[0],m2.shape[1]))

       for i in range(m1.shape[0]):

           for j in range(m2.shape[1]):

               r1[i,j] = (m1[i]*m2.transpose()[j]).sum()

       t2 = time.time()

       print("Native implementation: ",r1)

       print("Time: ",t2-t1)

       

       t1 = time.time()

       r2 = m1.dot(m2)

       t2 = time.time()

       print("\nEfficient implementation: ",r2)

       print("Time: ",t2-t1)

       

   else:

       print("Wrong dimensions!")

Explanation:

We define a function (matrixMul) that receive two arrays representing the two matrices to be multiplied, then we verify is the dimensions are appropriated for matrix multiplication if so we proceed with the native implementation consisting of two for-loops and prints the result of the operation and the execution time, then we proceed with the efficient implementation using .dot method then we return the result with the operation time. As you can see from the image the execution time is appreciable just for large matrices, in such a case the execution time of the efficient implementation can be 1000 times faster than the native implementation.

7 0
2 years ago
Need help fast I been stuck in this for the longest
gavmur [86]

Answer:

3rd and 4rth

Explanation:

a geologist studies the earth and both of these have something to do with the earth

6 0
3 years ago
Calculate the theoretical density of FCC iron (eg. austenitic stainless steel). The lattice parameter for FCC iron is 0.357 nm a
Ann [662]

Answer: 12.4 feet

Explanation:

If there is a smooth transition and there is no change in slopes, energy considerations can be used

The cube has a kinetic energy of

ke = mv^2/2 = 10 lbm * 20^2ft^2/s^2 / 2 = 2000 lbm-ft^2 / s^2

At the highest point when there is a gain in potential energy

pe = mgh = 10 lbm * 32.2 ft/s^2 * h ft = 322 lbm ft^2/s^2

If there is no loss in energies,

pe = ke

322h lbm ft^2/s^2 = 2000 lbm ft^2/s^2

h = 2000 /322 = 6.211 (ft)

= h / sin(30) = 12.4 ft

8 0
2 years ago
where σ(t) and σ(0) represents the time-dependent and initial (i.e., time =0) stresses, respectively, and t and τ denote elapsed
artcher [175]

Answer:

MUDA MUDA A certain vehicle loses 3.5% of its value each year. If the vehicle has an initial value of $11,168, construct a model that represents the value of the vehicle after a certain number of years. Use your model to compute the value of the vehicle at the end of 6 years.

Explanation:

4 0
3 years ago
Other questions:
  • Does the location of a millimeter change the voltage or current of the circuit?
    7·1 answer
  • How are project deliverables determined?
    14·2 answers
  • The costs of mining and transporting coal are roughly independent of the heating value of the coal. Consider:
    15·1 answer
  • Multiple Choice
    10·1 answer
  • Specify whether the statements are true or false.
    15·1 answer
  • Technician A says when you push the horn button, electromagnetism moves an iron bar inside the horn, which opens and closes cont
    5·2 answers
  • How many ase certifications are there for automotive technicians?
    7·1 answer
  • I know this answer i just want too see if people know it too
    9·2 answers
  • Help please!!!!!
    12·1 answer
  • can anyone help me with this please.i have the current and pf for branch 1 and 2 but cant figure out the total current, pf and a
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!