Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write comment lines on each line of code. java 5. Problem Statement Create an in

ID: 3856342 • Letter: W

Question

Write comment lines on each line of code.
java


5. Problem Statement Create an interface Stack in package myPack with following methods Methods: void push(int l) e int pop Create two subclasses inheriting the Stack interface. class FixedStack that can accept a fixed numbers of integers. The size of the Stack will be specified in the constructor. If the number of integers exceeds the size, "Stack overflow" message should be displayed class VariableStack that can accept numbers more than its size. The size of the Stack will be specified in the constructor. If the number of integers exceeds the size, the stack should automatically grow to accommodate the new element. 6. Expected Output Make a program that checks the implementation of both the classes Files to be submitted for verification 1. 2. Eclipse Console Screenshot with the relevant output. Eclipse files in below format

Explanation / Answer

package myPack;

/**
*
* @author Sam
*/
interface Stack {
    void push(int i);
    int pop();
}

class FixedStack implements Stack{
    private final int arr[];
    private final int size;
    private int top;

    public FixedStack(int size) {
        this.size = size;
        arr = new int[size];
        top = -1;
    }

    @Override
    public void push(int i) {
        if (top == size - 1) {
            System.out.println("Stack overflow while pushing " + i + " in fixed stack");
            //throw new UnsupportedOperationException();
            return;
        }
        arr[++top] = i;
    }

    @Override
    public int pop() {
        if (top < 0) {
            System.out.println("Stack underflow");
            //throw new UnsupportedOperationException();
            return -999;
        }
        return arr[top --];
    }
  

}

class VariableStack implements Stack{
    int arr[];
    int size;
    int top;

    public VariableStack(int size) {
        this.size = size;
        arr = new int[size];
        top = -1;
    }
  
    @Override
    public void push(int i) {
        if (top == size - 1) {
            realloc();
        }
        arr[++top] = i;
    }

    @Override
    public int pop() {
        if (top < 0) {
            System.out.println("Stack underflow");
            //throw new UnsupportedOperationException();
            return -999;
        }
        return arr[top --];
    }

    private void realloc() {
        int tmpArr[] = new int[size*2];
        System.arraycopy(arr, 0, tmpArr, 0, size);
        size *= 2;
        arr = tmpArr;
    }
  
}
public class TestClass {
    public static void main(String[] args) {
        FixedStack fs = new FixedStack(3);
        fs.push(1);
        fs.push(2);
        fs.push(3);
        //ERROR
        fs.push(0);
      
        VariableStack vs = new VariableStack(3);
        vs.push(1);
        vs.push(2);
        vs.push(3);
        vs.push(0);
        vs.push(9);
        //no error
    }
}

I hope you like it. Let me know your views

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote