Implement a generic stack and queue classes (Stack UMUC and Queue UMUC , named t
ID: 3758791 • Letter: I
Question
Implement a generic stack and queue classes (Stack UMUC and Queue UMUC , 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. . Use the GUI and classes you defined in Week to test your stack and queue classes . Methods for StackUMUC . Methods for QueueUMUC 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 in a single zip file: . 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, at, odt. pdf . this should be a professional quality presentation . there should be appropriate comments in the source code filesExplanation / Answer
/**Generic Stack class that contains methods
* push,pop,peek and Overrides toString method*/
//StackUMUC.java
public class StackUMUC <T>
{
private T [] stack;
private int size = 0;
/*Constructor of StackUMUC with input lenght parameter*/
public StackUMUC(int lenght)
{
stack = (T[]) new Object[lenght];
}
/*Insert element t into stack*/
public void push(T t)
{
if(size!=stack.length)
{
stack[size] = t;
size++;
}
else
System.out.println("Stack is full");
}
//Pop element from stack
public T pop()
{
if(size!=0)
{
return stack[size--];
}
else
return null;
}
//Get peek element from stack
public T peek()
{
return stack[size - 1];
}
//Returns the string description of stack
public String toString()
{
String stackDesrciption="";
for(int index=0;index<size;index++)
//add elements to the string
stackDesrciption+=stack[index]+",";
//returns the string value
return stackDesrciption;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
/**Generic queue class that contains methods
* put,get,peek and Overrides toString method*/
//QueueUMUC.java
public class QueueUMUC<T>
{
//queue of type T
private T [] queue;
//queue has two parameters front and rear
private int front;
private int rear;
/*Constructor of QueueUMUC with input lenght parameter*/
public QueueUMUC(int lenght)
{
queue = (T[]) new Object[lenght];
front=0;
rear=queue.length-1;
}
/*Insert element from front of array*/
public void put(T t)
{
queue[front%queue.length] = t;
//increment front by one
front++;
}
/*Returns the element from the rear*/
public T get()
{
if(rear!=0)
{
T obj=queue[rear--];
return obj;
}
else
return null;
}
//returns the peek of the element
public T peek()
{
return queue[rear];
}
/*Returns the queue as string description*/
public String toString()
{
//check if front and rear are zero
if(front==0 && rear==0)
return "";
String queueDescription="";
//add elements to string from o to front
for(int index=0;index<front;index++)
queueDescription+=queue[index]+",";
//add elements to front to rear
if(rear!=0)
for(int index=front;index<rear;index++)
queueDescription+=queue[index]+",";
return queueDescription;
}
}
---------------------------------------------------------------------------------------------------------------------------------------
/*Test program for StackUMUC and QueueUMUC classes
* and print the result of operations on stack and queue.*/
//TestGenerics.java
public class TestGenerics
{
public static void main(String[] args)
{
//Create a object of StackUMUC of string type
StackUMUC<String>genStack=new StackUMUC<String>(5);
genStack.push("cpp");
genStack.push("c");
genStack.push("java");
genStack.push("csharp");
genStack.push("matlab");
System.out.println("Printing stack");
System.out.println("=================");
System.out.println(genStack.toString());
System.out.println("POP element from stack");
System.out.println("=================");
System.out.println(genStack.pop());
System.out.println("Printing stack");
System.out.println("=================");
System.out.println(genStack.toString());
//Create a object of StackUMUC of integer type
StackUMUC<Integer>genIntegerStack=new StackUMUC<Integer>(5);
genIntegerStack.push(1);
genIntegerStack.push(2);
genIntegerStack.push(3);
genIntegerStack.push(4);
genIntegerStack.push(5);
System.out.println(genIntegerStack.toString());
//stack prints that stack is full
genIntegerStack.push(5);
System.out.println(genIntegerStack.toString());
//Create a object of QueueUMUC of string type
QueueUMUC<String>genQueue=new QueueUMUC<String>(5);
genQueue.put("cpp");
genQueue.put("c");
genQueue.put("java");
genQueue.put("csharp");
genQueue.put("matlab");
//print queue
System.out.println("Printing Queue elements");
System.out.println("=================");
System.out.println(genQueue.toString());
System.out.println("get element from Queue elements");
System.out.println("=================");
genQueue.get();
System.out.println("Printing Queue elements");
System.out.println(genQueue.toString());
System.out.println("Peek element of Queue");
System.out.println("=================");
System.out.println(genQueue.peek());
}
}
----------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Printing stack
=================
cpp,c,java,csharp,matlab,
POP element from stack
=================
matlab
Printing stack
=================
cpp,c,java,csharp,
1,2,3,4,5,
Stack is full
1,2,3,4,5,
Printing Queue elements
=================
cpp,c,java,csharp,matlab,
get element from Queue elements
=================
Printing Queue elements
cpp,c,java,csharp,matlab,
Peek element of Queue
=================
csharp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.