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

*Please use the hint down below to solve this problem. Answer must use interface

ID: 3600390 • Letter: #

Question

*Please use the hint down below to solve this problem. Answer must use interfaces and have a separate driver class to test code. Java language.*

Design an ADT for a two-color, double stack ADT that consists of two stacks — one “red” and one “blue” — and has its operations color-coded versions of the regular stack ADT operations. For example, this ADT should support both a redPush operation and a bluePush operation. Give an efficient implementation of this ADT using a single array whose capacity is set at some value value N that is assumed to always be larger than the sizes of the red and blue stacks combined.

BlueStackADT<Interface>
isEmptyBlue( ) : boolean
sizeBlue( ) : int
pushBlue(E element) : void
popBlue( ) : E
topBlue( ) : E

RedStackADT<Interface>
isEmptyRed( ) : boolean
sizeRed( ) : int
pushRed(E element) : void
popRed( ) : E
topRed( ) : E

Explanation / Answer

Please find my implementation.

public class DoubleStack<T>

{

   private int capacity;

   private int topR, topB;

   private T arr[];

   // Constructor

   DoubleStack(int n)

   {

       arr = (T[])new Object[n];

       capacity = n;

       topR = -1;

       topB = capacity;

   }

   // Method to push an element x to Red stack

   void pushRed(T x)

   {

       // There is at least one empty space for

       // new element

       if (topR < topB - 1)

       {

           topR++;

           arr[topR] = x;

       }

       else

       {

           System.out.println("Stack Overflow");

           System.exit(1);

       }

   }

   // Method to push an element x to Blue Stack

   void pushBlue(T x)

   {

       // There is at least one empty space for

       // new element

       if (topR < topB -1)

       {

           topB--;

           arr[topB] = x;

       }

       else

       {

           System.out.println("Stack Overflow");

           System.exit(1);

       }

   }

   // Method to pop an element from Red stack

   T popRed()

   {

       if (topR >= 0)

       {

           T x = arr[topR];

           topR--;

           return x;

       }

       else

       {

           System.out.println("Stack Underflow");

           System.exit(1);

       }

       return null;

   }

  

   public T topBlue() {

       if(topB < capacity)

       {

           return arr[topB];

       }

      

       return null;

   }

  

   public T topRed() {

       if(topR >= 0)

       {

           return arr[topR];

       }

      

       return null;

   }

   // Method to pop an element from Blue stack

   T popBlue()

   {

       if(topB < capacity)

       {

           T x =arr[topB];

           topB++;

           return x;

       }

       else

       {

           System.out.println("Stack Underflow");

           System.exit(1);

       }

       return null;

   }

  

   public boolean isEmptyBlue() {

       return topB >= capacity;

   }

  

   public int sizeBlue() {

       return capacity - topB;

   }

  

   public boolean isEmptyRed() {

       return topR <= 0;

   }

  

   public int sizeRed() {

       return topR;

   }

   // Driver program to test twoStack class

   public static void main(String args[])

   {

       DoubleStack<Integer> ts = new DoubleStack<>(5);

       ts.pushRed(5);

       ts.pushBlue(10);

       ts.pushBlue(15);

       ts.pushRed(11);

       ts.pushRed(7);

       System.out.println("Popped element from" +

               " stack1 is " + ts.popRed());

       ts.pushBlue(40);

       System.out.println("Popped element from" +

               " stack2 is " + ts.popBlue());

   }

}

/*

Sample run:

Popped element from stack1 is 7

Popped element from stack2 is 40

*/