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.

Solution

IntStackDemo.java file : -

// IntStack class implements ILifo interface.

//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.


import java.util.Arrays;
class IntStack implements ILifo{
   private int max;                    // max elements IntStack can store
   private int[] stackArray;        // array to store elements
   private int top;                 // to pop recently added
   public IntStack() {
      max = 10;                     // setting max
      stackArray = new int[max];
      top = -1;
   }
   public IntStack(int initialCapacity) {
      max = initialCapacity;
      stackArray = new int[max];
      top = -1;
   }
   // input: int output: void
   public void push(int num) {          
       if( top == max-1 ){
        System.out.println(\"IntStack full\");      
       }
       else{
        stackArray[++top] = num;              // pushing elements
       }
   }
   // input nothing. returns recently added elements
   public int pop() {
       if( top == -1 ){
        System.out.println(\"IntStack Empty\");
        return -1;
       }
       return stackArray[top--];                       // poping element
   }
   // returns boolean. input nothing
   public boolean isEmpty() {
      return (top == -1);
   }
   // return no of elements. input nothing
   public int getLength(){
        return top+1;
   }
   // setting new capacity for IntStack input: int value output: status of method 0 for success and -1 for failure.
   public int seCapacity(int newCapacity){
        if (newCapacity < max){
            System.out.println(\"IntStack cannot shrink\");
            return -1;                                            // failure.
        }
        else{
            max = newCapacity;
            int[] newone = new int[newCapacity];            // creating new array.
            System.arraycopy(stackArray, 0, newone, 0, stackArray.length); // copying elements to new array.
            stackArray = newone;          // setting new array as IntStack array.
            return 0;                        // success
        }
   }
   // input nothing. output string format of IntStack
   public String toString(){
         String temp = \"\";
         for(int i=0;i<=top;i++){
            temp = temp + Integer.toString(stackArray[i]) + \" \";      // converting to string
         }
        return temp;
   }
}
// demo of IntStack
public class IntStackDemo{
   public static void main(String[] args) {
      IntStack is = new IntStack(2);
      System.out.println(\"10 pushed\");
      System.out.println(\"11 pushed\");
      is.push(10);
      is.push(11);
      is.push(12);
      System.out.println(\"12 not pushed\");
      is.seCapacity(4);
      is.push(12);
      System.out.println(is.toString());
   }
}

ILifo.java file : - (interface)

interface ILifo {

public int getLength ( );

public boolean isEmpty ( );

public void push (int num);

public int pop ( );

public int getCapacity ( );

public int seCapacity (int 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 (
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