Design a class named IntStack that implements the following

Design a class named IntStack that implements 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 seCapacity (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 intersection 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 ( )

• Return the number of elements (i.e., integers) currently in the data structure.

Public boolean isEmpty ( )

• Return true if the data structure is currently empty (i.e., has no element)

Public void push (int num )

• Add the passed-in integer to the collection.

• When the collection is full, this method should signal the caller and reject the request.

Public int pop ( )

• Remove the most recently added integer from the collection and return it.

• When the collection is empty, this method should signal the caller and reject the request.

Public int getCapacity ( )

• 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 contain at most 100 integers. Once it contains 100 integers, no more elements can be added before the data structure is popped.

Public seCapacity (int newCapacity)

• It is used to expand the current data structure for increasing its capacity. The implementing class has to reallocate storage for existing elements and desired additional capacity. For example, if the capacity before calling setCapacity 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.

• Notice that the length and the capacity are usually different. For an ILifo data structure with capacity of 100, its length could be 20 because only 20 elements currently exist in the collection, in which case the structure can accommodate 80 more elements.

• This method is not allowed to shrink the data structure.

You are asked to write a class named IntStack that implements the above defined interface ILifo. An IntStack object should store integer elements of the collection in an array. All fields should be declared to be private. Your implementation should be as efficient as possible with respect to the speed of insertion/deletion and the space needed to store the collection.

Besides, IntStack class shall offer two versions of constructors, and the standard toString instance method.

Public IntStack (int initialCapacity)

• The client code would like to create an IntStack object with the specified initial capacity.

Public IntStack ( )

• You, as the class designer, decide the default capacity.

Public String toString ( )

• Return the String representation of this ILifo data structure, with the most recently added elements at the end.

Code Specification:

• The header comment lines at the top of your java files contain a brief description of the program or the class. The description should be one or 2 lines long describing the purpose of the program or the class.

• Declare all class fields as private

• You also have to include comment lines in all of your public methods, static or not.

• Use javadoc\'s comment format for public class and method documentation.

• Use descriptive variable name or method names.

• Define constants whenever appropriate.

Submission:

Your final solution will have two files; one for the ILifo interface and the other for IntStack class.

Use array to store the data.

Solution

package chegg;

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

}

package chegg;

import java.util.Arrays;

//This code is implementation of interface ILifo
//Have Class variables and methods .
public class IntStack implements ILifo{
   private int capacity = 100;
   private int arr[] = new int[capacity];
   private int top = -1;
  
   public IntStack(){
      
   }
   public IntStack(int capacity){
   this.capacity = capacity;
   }
  

       public void push(int num) {
       if (top < capacity - 1) {
       top++;
       arr[top] = num;
       System.out.println(\"Element \" + num
       + \" is pushed to Stack !\");
       } else {
       System.out.println(\"Stack Overflow !\");
       }
       }

       @Override
       public String toString() {
           return \"IntStack [arr=\" + Arrays.toString(arr) + \"]\";
       }


       public int pop() {
       if (top <0) {
           System.out.println(\"Stack Underflow !\");
           }
           //--top;
       int value = arr[top];
       top--;
       return value;
       }

      
       @Override
       public int getLength() {
           return arr.length;
       }

       @Override
       public boolean isEmpty() {
           if(top>=0){
               return false ;          
       }
      
       return true;

}  
      

       @Override
       public int getCapacity() {
           // TODO Auto-generated method stub
           return capacity;
       }

       @Override
       public void setCapacity(int newCapacity) {
           capacity = newCapacity;
       }


}

Design a class named IntStack that implements the following interface called ILifo: Public interface ILifo { Public int getLength ( ); Public boolean isEmpty (
Design a class named IntStack that implements the following interface called ILifo: Public interface ILifo { Public int getLength ( ); Public boolean isEmpty (
Design a class named IntStack that implements the following interface called ILifo: Public interface ILifo { Public int getLength ( ); Public boolean isEmpty (

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site