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

Implement a generic stack and queue classes (StackUM and QueueUM, named to avoid

ID: 667320 • Letter: I

Question

Implement a generic stack and queue classes (StackUM and QueueUM, named to avoid confusion with the classes already defined in the JDK). Specific requirements include:

Create a set of tests demonstrating that your classes work correctly.

Methods for StackUM<T>

StackUM (int) // constructor

void push (T)

T pop ()

T peek ()

String toString ()

Methods for QueueUM<T>

QueueUM (int) // constructor

void put (T)

T get ()

T peek ()

String toString ()

In your documentation, answer this question: At what point does the performance of your program begin to degrade - say take longer than 1 or 10 seconds?

Submission should include the following:

a readme file (see below)

all source code files

relevant data files

Documentation (a readme file) for the project should include the following sections:

a description of the design,

a user's guide

a test plan

a section on lessons learned

this should be a single file in one of the following formats: doc, docx, txt, odt, pdf

this should be a professional quality presentation

there should be appropriate comments in the source code files

Explanation / Answer

/*The StackUM class that contains methods for push ,pop , peek and toString methods
* */
// StackUM.java
import java.util.NoSuchElementException;
public class StackUM<T>
{
   private T[] elements;
   private int top;

  
   //Constructor to create an generic type of array of size
   public StackUM(int size)
   {
       elements = (T[]) new Object[size];
       top = -1;
   }
  
  
   //Insert generic type element into the Stack
   public void push(T element)
   {
       if (top == elements.length-1)
           throw new IllegalStateException("Stack is full");
       else
           elements[++top] = element;
   }
  
   //Returns the generic element from the stack
   T pop()
   {
       if (isEmpty())  
           throw new NoSuchElementException("Stack is empty");

       else
           return elements[top--];

   }
  
   //Returns the generic element from the stack without removing element from the stack
   T peek()
   {
       if (isEmpty())
           throw new NoSuchElementException("Stack is empty");

       return elements[top];

   }

   //Returns true if stack is empty otherwiser returns false
   public boolean isEmpty()
   {
       return top == -1;
   }


  
   //Returns the string representation fo stack elemets
   @Override
   public String toString()
   {
       String str="";
      
       if(top==-1)
           throw new NoSuchElementException("Stack is empty");
       else
       {
           for (int i = 0; i <=top; i++)
           {
               str+=elements[i]+" ";
           }
       }
       return str;
   }

}

---------------------------------------------------------------------------------------------------------------------------------------

/*Tester class of StackUM*/
//StackUMTester.java
public class StackUMTester
{
   public static void main(String[] args)
   {
      
      
       //Create an instance of stack of string type with size 5
       StackUM<String>countries=new StackUM<String>(5);
      
      
       //push string type values into stack
       countries.push("USA");
       countries.push("UK");
       countries.push("Italy");
       countries.push("France");
       countries.push("INDIA");
      
       //print stack elements
       System.out.println(countries.toString());
      
       //Sincek stack is full, it display stack is full
       //countries.push("RUSSIA");
      
       //prints the peek of the stack
       //System.out.println(countries.peek());
      
       countries.pop(); //removes INDIA
       countries.pop(); //removes France
       countries.pop(); //removes Italy
       countries.pop(); //removes UK
       countries.pop(); //removes USA
      
       //print stack elements
       System.out.println(countries.toString());
      
       //since stack is empty , prints stack is empty
       //countries.pop();      
   }//end of main
}

Sample output:

USA UK Italy France INDIA
Exception in thread "main" java.util.NoSuchElementException: Stack is empty
   at StackUM.toString(StackUM.java:67)
   at StackUMTester.main(StackUMTester.java:38)

---------------------------------------------------------------------------------------------------------------------------------------

/**The java QueueUM class that contains the methods defintions for
* the constructor , put, get , peek and toString methods
* */
//QueueUM.java
import java.util.NoSuchElementException;
public class QueueUM<T>
{
   private T[] elements;
   private int size;
   private int head;   // head of the queue
   private int tail ; // tail of the queue

   //Constructor of QueueTM with integer value
   public QueueUM(int capacity)
   {
       //create an array of object of type T
       elements = (T[]) new Object[capacity];  
       size=0;
       head=0;
       tail=0;
      
   }
  
  
   /*Insert an genric type element into the queue*/
   public void put(T element)
   {
       if (size == elements.length)
       {
           throw new IllegalStateException("Queue is full");
       }
       elements[tail] = element;
       tail = (tail + 1);
       size++;

   }

  
  
   /*Removes and returns and an genric type element from the queue*/
   T get()
   {
       if (size == 0)
       {
           throw new NoSuchElementException("Queue is empty");
       }
       T item = elements[head];
       elements[head] = null;
       head = (head + 1);
       size--;
       return item;


   }

  
   /*Returns the head of the stack without removing element*/
   T peek()
   {
       if (size == 0)
       {
           throw new NoSuchElementException("No elements");
       }
       return elements[head];

   }

   //Returns true if size is -1
   public boolean isEmpty()
   {
       return size == -1;
   }


  
   //Prints the queue elements of the queue is not empty
   @Override
   public String toString()
   {
       String str="";
       int i,j;
        if(head==0&&tail==0)
        {
            return "Queue is empty";
        }
        if(head>tail)
        {
            for(i=0;i<=tail;i++)
                str+=elements[i]+" ";
            for(j=head;j<=size-1;j++)
               str+=elements[j]+" ";          
        }
        else
        {
            for(i=head;i<tail;i++)
            {
               str+=elements[i]+" ";
            }          
        }
        return str;
   }//end of toString method
} //end of the class QueueTM

---------------------------------------------------------------------------------------------------------------------------------------


/*The java QueueUM tester class that test the methods of the QueueTM
* class. and print the resutls on console
* */
//QueueUMTester.java
public class QueueUMTester
{
   public static void main(String[] args)
   {

       //Create an instance of stack of Queue type with size 5
       QueueUM<String>countries=new QueueUM<String>(5);


       //put string type values into Queue
       countries.put("USA");
       countries.put("UK");
       countries.put("Italy");
       countries.put("France");
       countries.put("INDIA");

       //print Queue elements
       System.out.println(countries.toString());

       //prints the peek of the Queue
       System.out.println("Head of Queue : "+countries.peek());

       //Removing elements from queue
       System.out.println(countries.get()); //removes INDIA
       System.out.println(countries.get()); //removes France
       System.out.println(countries.get()) ;//removes Italy
       System.out.println(countries.get()); //removes UK
       System.out.println(countries.get()); //removes USA

       //since Queue is empty , prints stack is empty
       countries.get();

   }
}


Sample output:

USA UK Italy France INDIA
Head of Queue : USA
USA
UK
Italy
France
INDIA
Exception in thread "main" java.util.NoSuchElementException: Queue is empty
   at QueueUM.get(QueueUM.java:49)
   at QueueUMTester.main(QueueUMTester.java:40)

Hope this helps you

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