Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify your stack implementation in such a way that a client application using your stack is unaware of the underlying array-based implementation. For instance, in

Modify your stack implementation in such a way that a client application using your stack is unaware of the underlying array-based implementation. For instance, in a C or C++ implementation, you would define function prototypes in .h files and implementation details in .c files. In Java and C#, you use interfaces. The application uses the interfaces (or .h files) with no knowledge of how the functions are implemented. That way the array-based implementation could be replaced with a linked list-based one without any change to the client application.

Codes to be implemented

Stack.java/////

 public class Stack { private final int SIZE =99; private int top; private int arr[]; public Stack() { top = -1; arr = new int[SIZE]; } // implement various stack methods public void push(int val) { if (top == SIZE) { System.out.println("Stack is full"); } else { top++; arr[top] = val; } } public int pop() { // check of stack is empty or not if (top == -1) { System.out.println("Stack is empty"); return -1; } else { top--; return arr[top + 1]; } } public int top() { // check of stack is empty or not if (top == -1) { System.out.println("Stack is empty"); return -1; } else { return arr[top]; } } public int size() { return top + 1; } public boolean isEmpty() { // check of stack is empty or not if (top == -1) { return true; } else { return false; } } public boolean isFull() { // check of stack is full or not if (top == SIZE) { return true; } else { return false; } } // main driver code of the program. contains test code public static void main(String args[]) { // create Stack Stack s = new Stack(); // push some values s.push(10); s.push(20); System.out.println("Stack size : " + s.size()); System.out.println("Stack top elem : " + s.top()); System.out.println("Stack is empty ? : " + s.isEmpty()); s.pop(); System.out.println("Stack top elem : " + s.top()); s.pop(); System.out.println("Stack size : " + s.size()); System.out.println("Stack is empty ? : " + s.isEmpty()); } }

Brackets.java//////

import java.util.*; public class Brackets { // function to check if brackets are balanced static int areBracketsBalanced(String expr) { // Using Stack class Stack stack = new Stack(); // Traversing the Expression for (int i = 0; i < expr.length(); i++) { char x = expr.charAt(i); if (x == '(') { // Push the element in the stack stack.push(x); continue; } // IF current current character is not opening // bracket, then it must be closing. So stack // cannot be empty at this point. if (stack.isEmpty()) return i+1; char check ; check = (char) stack.pop(); //if(x==')') } // Check Empty Stack if(stack.isEmpty()) return -1; //Balanced else return expr.length(); //no closing paranthesis to previously opened paranthesis } // Driver code public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter expression:"); String expr = sc.nextLine(); // Function call int x=areBracketsBalanced(expr); if (x==-1) { //if balanced System.out.println("All brackets matched"); } else { //not balanced System.out.println("Mismatched bracket at position "+x); } } }

PostFixCalculator.java

import java.util.Scanner; public class PostFixCalculator { public static void main(String[] args) { char inputArr[] = new char[100]; // to store the input int operandsCount =0; //count of operands int operatorCount = 0; //count of operators Stack operatorStack = new Stack(); //stack to store operators char input = ' '; // to get our postfix expression Scanner inp = new Scanner(System.in); int index =0; // index of inputArr also stores the length while(input!='=') { input = inp.next().charAt(0); if(input=='+'||input=='-'||input=='*') { operatorCount++; inputArr[index] = input; index++; } else if(input!='=') { operandsCount++; inputArr[index] = input; index++; } } if(operandsCount-1 == operatorCount) { int result=0; for(int i=0 ; ioperatorCount) { System.out.println("Too Many Operands"); } else { System.out.println("Few Operators"); } } }

Stack.java is stack implementation , Brackets.java checks to see if brackets balance, PostFixCalculator.java accepts operands and operators from user and runs calculation

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions