Complete the following for full credit in this assignment:
Complete the coding requirement for the Class Method Bodies:
Write the constructor: This should initialize all of your classfields and dynamically allocate memory for your array that willhold your stack data. You should use the constant “n†as the sizefor your fixed-size stack.
Write the push method: This should add the item in the formalparameter to your stack. Caveat: You need to check and make sureyou aren’t going out of bounds with your array. If so, you need toignore requests to add to the stack as it is full. After that, youcan simply add the item and update your cursor (“top†field).
Write the toString method: If the stack is empty, simple returnempty brackets “[]â€. Otherwise, build a string that consists ofevery item in the stack inside the brackets and separated by commas(e.g. [1,2,3]). You must use a loop to do this, but you will likelyhave an extra comma at the end. You can use the substring method toremove the ending comma.
Write the isEmpty method: This can be done in one line if youare savvy. Simply return whether the size of your stack is zero ornot.
Write the size method: This can also be done in one linedepending on how you programmed the rest of the class. Simplyreturn how many items are currently in the stack.
Write the peek method: This should return the value at the topof the stack. If the stack is empty, return -1. Caveat: Make sureyou check whether the stack is empty BEFORE you try accessing arrayelements otherwise you could crash the program.
Write the pop method: This should return the value at the top ofthe stack and then remove that item from the stack. We can use“lazy evaluation†which means we don’t actually remove the item butonly remove access to it. Simply decrement your cursor variable toremove access to it. If the stack is empty, return -1. Caveat: Makesure you check whether the stack is empty BEFORE you try accessingarray elements otherwise you could crash the program.
Write a clear method. This should set the stack to the emptystate. In other words, it “resets†the stack to how it was when youfirst created the stack object. You should use “Lazy Evaluationâ€for efficiency.
public class Lab05{
  public static void main(String[] args){
     StackInt st = new StackInt();
     p(st.isEmpty());        // This should be true
     p(st.peek());           // Thisshould be -1
     p(st.size());           // Thisshould be 0
     for(int i = 0; i < 10;i++)
        st.push(i);        // This adds 10items to the stack
     p(st.toString());        // This should be[0,1,2,3,4,5,6,7,8,9]
     p(st.isEmpty());        // This should be false
     p(st.peek());           // Thisshould be 9
     p(st.size());           // Thisshould be 10
     p(\"Popped \" + st.pop() + \" fromstack...\");  // This should be \"Popped 9 fromstack...\"
     p(st.toString());        // This should be[0,1,2,3,4,5,6,7,8]
     st.clear();            // Thisshould clear the stack...
     p(st.isEmpty());        // This should be true
  }
 Â
  public static void p(E s){
     System.out.println(s);
  }
}