Question

5) Consider the following Java statements, assuming that MyStack is a class that implements the interface...

5) Consider the following Java statements, assuming that MyStack is a class that implements the interface StackInterface :

int n = 4;

StackInterface stack = new MyStack<>();

while (n > 0)

{

stack.push(n);

n--;

} // end while

int result = 1;

while (!stack.isEmpty())

{

int integer = stack.pop();

result = result * integer;

} // end while

System.out.println("result = " + result);

a.) What value is displayed when this code executes?

b.) What mathematical function does the code evaluate?

6) Show the contents of the stack as you trace the algorithm checkBalance , given in Segment 5.8, for each of the following expressions:

a) a { b [ c * ( d + e )] - f }

b) { a ( b * c ) / [ d + e ] / f ) - g }

c) a { b [ c - d ] e ] ) f

7) Using the algorithm convertToPostfix , given in Segment 5.16, convert each of the following infix expres-sions to postfix expressions:

a) a * b / ( c - d )

b) ( a - b * c ) / ( d * e * f + g )

c) a / b * ( c + ( d - e ))

d) ( a ^ b * c - d ) ^ e + f ^ g ^ h

Problem: Complete Exercises 5, 6, and 7 above..

Exercise 5: Describe SPECIFICALLY a. What value is displayed when the code executes? b. what mathematical function odes it evaluate?

Exercise 6: What value is displayed when the code executes?

Exercise 7: Convert the expressions from infix format to postfix format.

Requirements: You will submit the lab in written format ONLY, no coding is required (Need answers ASAP please)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answers are as follows :

a.) What value is displayed when this code executes? => 1*2*3*4 = 24 is the answer

b.) What mathematical function does the code evaluate? It computes Factorial



6) Show the contents of the stack as you trace the algorithm checkBalance , given in Segment 5.8, for each of the following expressions:


Contents of the Stack

a) a { b [ c * ( d + e )] - f }

Stack will contain
1. {
2. { [
3. { [ (
4. { [
5. {
6. Empty

b) { a ( b * c ) / [ d + e ] / f ) - g }
Stack will contain
  
1. {   
2. { (
3. {
4. { [
5. { ---> Ends
6. { ---> Ends and returns false

c) a { b [ c - d ] e ] ) f
1. {   
2. { [
3. {
4. { ---> ']' No matching parenthesis '[' found so retiurns false


7) Using the algorithm convertToPostfix , given in Segment 5.16, convert each of the following infix expres-sions to postfix expressions:

a) a * b / ( c - d ) .

a*b / cd-

ab*/cd-
ab* cd - /

b) ( a - b * c ) / ( d * e * f + g )
( abc* -) / (de*f*g+)
abc* - de*f*g+/

c) a / b * ( c + ( d - e ))
a / b * cde- +
ab/cde- +*

d) ( a ^ b * c - d ) ^ e + f ^ g ^ h
ab^ c*d - e ^ fg^h^ +




Thanks, let me know if there is any concern.

Add a comment
Know the answer?
Add Answer to:
5) Consider the following Java statements, assuming that MyStack is a class that implements the interface...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert...

    By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • i want similar for this code to solve two questions : 1- Write a program to...

    i want similar for this code to solve two questions : 1- Write a program to convert a postfix expression to infix expression 2-Write a program to convert an infix expression to prefix expression each question in separate code ( so will be two codes ) #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c;...

  • In Java please as soon as possible Convert the following expressions to both Prefix and Postfix...

    In Java please as soon as possible Convert the following expressions to both Prefix and Postfix / Infix and create the binary trees which represent them. V. (A B/C+D$E) * (F/G)- H A. (A+B)+(C/(D E)-F)/G H B. KL+A B+ CDEF$/-/HI+ * - C. Convert the following expressions to both Prefix and Postfix / Infix and create the binary trees which represent them. V. (A B/C+D$E) * (F/G)- H A. (A+B)+(C/(D E)-F)/G H B. KL+A B+ CDEF$/-/HI+ * - C.

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • 2.  Convert the expressions from infix to postfix.  Demonstrate use of the stack to carry this out. A)...

    2.  Convert the expressions from infix to postfix.  Demonstrate use of the stack to carry this out. A) 2 * (3 + 4) / (5 * 2) B) A – (B  + C * D  / E) C) A / B / C    - (D + E ) * F

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT