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...
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...
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
Write your own version of a class template that will create a static stack of any...
Write your own version of a class template that will create a static stack of any data type. Demonstrate the class with a driver program. please make a version to copy.
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
Implement a VotingMachine class that can be used for a simple election. Include the methods to...
Implement a VotingMachine class that can be used for a simple election. Include the methods to voteForDemocrat and voteForRepublican. Add a method to clear out all votes. Additionally, add a method to print the results. Write the driver code to simulate the voting. in java
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT