Question

In: Computer Science

JAVA Stack - Implementation. You will be able to use the push, pop and peek of...

JAVA

Stack - Implementation.

You will be able to use the push, pop and peek of Stack concept. Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses and thus all implicit precedence rules.

Program Implementation Requirements

Use the stack concept to create a post-fix calculator. I will be using a test-harnessPreview the document to run your program. Please make sure it meets the requirements to be run by the test harness.

Test harness:

public class StackCalcTest {

public static void main(String[] args) {

StackCalc stackCalc = new StackCalc();

String[] values = {"3", "5", "9", "*", "+"};

for(int i = 0; i < 5; i++) {

stackCalc.stack.push(values[i]);

}

System.out.println(stackCalc.stack);

System.out.println(stackCalc.answer()); }}

Solutions

Expert Solution

import java.util.Stack;

public class StackCalc{

Stack<String> stack = null;

StackCalc(){

stack = new Stack<String>();

}

public double answer() {

Stack<String> temp = new Stack<String>();

String s = "";

while(!stack.isEmpty()) {

String k = stack.peek();

s = s + k;

temp.push(k);

stack.pop();

}

Stack<Double> postfix = new Stack<Double>();

for (int i = s.length()-1; i>=0; i--) {

char c = s.charAt(i);

if (Character.isDigit(c))

postfix.push((double) (c - '0'));

else {

double val1 = postfix.peek();

postfix.pop();

double val2 = postfix.peek();

postfix.pop();

switch (c) {

case '+':

postfix.push(val2 + val1);

break;

case '-':

postfix.push(val2 - val1);

break;

case '/':

postfix.push(val2 / val1);

break;

case '*':

postfix.push(val2 * val1);

break;

}

}

}

return postfix.peek();

}

}

****************************************************************************

public class StackCalcTest {

public static void main(String[] args) {

StackCalc stackCalc = new StackCalc();

String[] values = { "3", "5", "9", "*", "+" };

for (int i = 0; i < 5; i++) {

stackCalc.stack.push(values[i]);

}

System.out.println(stackCalc.stack);

System.out.println(stackCalc.answer());

}

}

***************************************************************************

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class. import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super...
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and the non-standard min() operation which returns the minimum value stored on the stack. The zip file gives an implementation SlowMinStack that implements these operations so that push(x) and pop() each run in O(1) time, but  min()runs in Θ(n) time. For this question, you should complete the implementation of FastMinStack that implements all three operations in O(1) time per operation. As part of your implementation, you...
Topic: Students will be able to create skills in the use of linked lists, the stack,...
Topic: Students will be able to create skills in the use of linked lists, the stack, and the queue abstract data types, by implementing solutions to fundamental data structures and associated problems. Add the code for the methods where it says to implement. The main class is already done. There is a sample of the output. 1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x):...
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
void Stack :: push(float x) {   if (count _______ capacity)                               
void Stack :: push(float x) {   if (count _______ capacity)                                                    // If there is no room for a new value { float* new_A = ______ float[ 2 * ________ ];                     // Create a new array with two-fold capacity for (int i = 0; i < count; ++i)                                   // Copy existing data to new array _______= A[i]; delete[]__________ ;                                                          // Delete old array A = new_A;                                                            // Connect to the new array capacity *= _______ ;                                                    // Update the capacity } A[ ______] =...
please in java ! Assume you have a stack of integers. The stack contains same number...
please in java ! Assume you have a stack of integers. The stack contains same number of positive and negative integers. You want to organize it such that negative and positive integers alternate (+-+-.., or -+-+,..). A. Write a Java code that uses no more than two additional Stacks to solve the problem. Note: You do not need to write the code for Stacks, you are using a Stack from the library with a name ourStack and has the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT