Question

In: Computer Science

In simple Java language algorithm: Implement a static stack class of char. Your class should include...

  1. In simple Java language algorithm:
  2. Implement a static stack class of char. Your class should include
  • two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack
  • a push and pop operator
  • an isEmpty and isFull method . Both return Booleans indicating the status of the stack
  1. Using the stack class you created in problem 1), write a static method called parse that parses a String for balanced parentheses. Unlike the problem we considered in class, we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no erros, otherwise, parse should return the position within the String where an error occurred. For example

parse(“{3 + {4/2} }”)   would return -1

parse(“{ { 4*X}”) would return 6 since at position 6 we expected another “}”

parse(“{3+4}}”) would also return a 6

Solutions

Expert Solution

import java.util.*;
import java.io.*;

class IntegerStack {

}

class StaticStack {
        private int size;
        private Stack<Character> stack1 = new Stack<Character>();
        
        StaticStack() {
                size = 10;
        }

        StaticStack(int size) {
                this.size = size;
        }

        void push(char x) {
                if(isFull()) {
                        System.out.println("Stack is Full!");
                        return ;
                }
                size++;
                stack1.push(x);                 
        }
        
        int pop() {
                if(isEmpty()) {
                        System.out.print("Stack is Empty!");
                        return -1;
                }
                size--;
                return stack1.pop();
        }

        boolean isEmpty() {
                return stack1.empty();
        }

        boolean isFull() {
                return size == stack1.size();
        }

}





class Main {
        
        static int parse(String exp) {
                
                StaticStack s = new StaticStack();
                int i;
                
                for(i=0;i<exp.length();++i) {
                        
                        if(exp.charAt(i)=='{' || exp.charAt(i)=='(')
                                s.push(exp.charAt(i));
                        else if(exp.charAt(i)=='}') {
                                if(s.isEmpty()) {
                                        s.push('}');
                                        i++;
                                        break;
                                }       
                                if(s.pop()!='{') {
                                        break;
                                }
                        }else if(exp.charAt(i) == ')') {
                                
                                if(s.isEmpty()) {
                                        s.push('}');
                                        i++;
                                        break;
                                }
                                if(s.pop()!='(') {
                                        break;
                                }
                        }
                }
                if(s.isEmpty())
                return -1;
                
                return i;
                
        }

        public static void main(String args[]) {

                System.out.println(parse("{3+{4/2}}"));
                System.out.println(parse("{{4*X}"));
                System.out.println(parse("{3+4}}"));
                
//              StaticStack s = new StaticStack();
//              
//              s.push('A');
//              System.out.println(s.pop());
                
        

        }
}

OUTPUT:


Related Solutions

Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Language: Java(Netbeans) Implement a simple calculator.
Language: Java(Netbeans) Implement a simple calculator.
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should have a private linked list member, and utilize the linked list methods to implement its functionality. The stack should include a constructor, a destructor, a push, a pop, and an isEmpty method (which returns a bool). c. Implement, QueueFromList, a templated queue class backed by the above singlylinked list. The queue should have a private linked list member, and utilize the linked list methods...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
For this assignment, implement a simple stack calculator which can compute an infix expression. It should...
For this assignment, implement a simple stack calculator which can compute an infix expression. It should take in a string containing an infix expression, compute the result, and print it out. It should handle operators +, -, *, / and parenthesis. Your program must have two main steps -- first convert the expression to postfix, and then compute the result using the algorithms discussed in class and textbook. These algorithms require that you use a stack. You must implement your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT