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

ILifo Interface Objectives: Practice interface implementation. Design a class na

ID: 3803118 • Letter: I

Question

ILifo Interface Objectives: Practice interface implementation. Design a class named Intstack that implement the following interface called ILifo: public interface ILifo public int getLength public boolean isEmpty public void push (int num) public int pop public int getcapacity public int setcapacity (int newcapacity) The ILifo interface serves to specify a special kind of data structure for storing a collection of integers. It is unique in the insertion and deletion of collection elements. An element can be added to the collection through the push method. The pop method is to remove the most recently added integer that was not yet removed. All ILifo methods are described as below: public int getLength o Return the number of elements i.e., integers) currently in the data structure. public boolean isEmpty o Return true if the data structure is currently empty (i.e., has no element) public void push (int num) o Add the passed n integer to the collection er ef Ion Err 2e o When the collection is full, this method should signal the caller and reject the request. public int pop o Remove the most recently added integer from the collection and return it. o When the collection is empty, this method should signal the caller and reject the request. f size sued int getcapacity cre o Return the capacity of the data structure, that is, the maximum amount of integers the data structure can hold. For example, if the capacity is 100, then the data structure can contains at most 100 integers. Once it contains 100 integers, no more elements can be added before the data structure is popped. int set capacity (int newcapacity) o t is used to expand the current data structure for increasing its capacity. The implementing class has to reallocate storage for existing elements and the desired additional capacity. For example, if the capacity before calling setcapacityis 100, calling setCapacity (200) would result in increasing the total capacity to 200. This method is also responsible for copying all existing elements to the newly allocated storage without compromising the ordering and contents of the collection.

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* change this template file
* open the template in the editor.
*/

/**
*
* @author Sambis
*/
public class IntStack implements ILifo {

    private int capacity;
    private int[] stack;
    private int top;

    public IntStack(int capacity) {
        this.capacity = capacity;
        this.top = -1;
        stack = new int[capacity];
    }

    public IntStack() {
        this.capacity = 10;
        this.top = -1;
        stack = new int[this.capacity];
    }
  
  
    @Override
    public int getLength() {
        return top + 1;
    }

    @Override
    public boolean isEmpty() {
        return top == -1;
    }

    @Override
    public void push(int num)throws UnsupportedOperationException {
        if (top == capacity - 1)
            throw new UnsupportedOperationException("Stack full");
        stack[++top] = num;
    }

    @Override
    public int pop()throws UnsupportedOperationException {
        if (top == -1)
            throw new UnsupportedOperationException("No element in the stack");
        return stack[top--];
    }

    @Override
    public int getCapacity() {
        return capacity;
    }

    @Override
    public int setCapacity(int newCapacity) {
        if (newCapacity < capacity)
            return capacity;
        int[] newStack = new int[newCapacity];
        System.arraycopy(stack, 0, newStack, 0, top + 1);
        stack = newStack;
        capacity = newCapacity;
        return capacity;
    }

    @Override
    public String toString() {
        String string = "";
        for (int i = 0; i <= top; i++)
            string = string + stack[i] + " ";
        return string;
    }
}


interface ILifo {
    int getLength();
    boolean isEmpty();
    void push(int num);
    int pop();
    int getCapacity();
    int setCapacity(int newCapacity);
}