Part 1 Design and implement a generic stack class StackUC and a generic queue cl
ID: 3758847 • Letter: P
Question
Part 1
Design and implement a generic stack class StackUC and a generic queue class QueueUC (these names are chosen as to avoid confusion with similar classes defined in JDK).
For StackUC class, implement the following methods:
StackUC(int) // class constructor
void push(T)
T pop()
T peek()
String toString()
For QueueUC class, implement the following methods:
QueueUC(int) // class constructor
void put(T)
T get()
T peek()
String toString()
The source code of the two classes should compile without errors.
Part 2
Test the two classes StackUC and QueueUC for the JDK types Integer and String and for the user defined type Point. The class Point represents 2D Cartesian points having x and y as instance variables, a constructor with arguments, and the methods getX, getY and toString.
The programs should compile without errors
Explanation / Answer
PART 1) QUEUE and STACK generic implementations.
instruction to use and run the code :
1) copy the codes here as separate .java files (the ===== lines mark the boundaries of the codes). To test use the QueueTest and StackTest . They further have comments about what is happening in each steps.
================== STACK CODE ============
import java.util.Arrays;
public class StackUC<T> {
private int maxSize;
private T[] stack;
private int top;
public StackUC(int size) {
maxSize = size;
stack = (T[])new Object[maxSize];
top = -1;
}
public void push(T element) {
if(top==maxSize-1) {
throw new IllegalStateException("Stack is full, cant push anymore elements");
}else{
stack[++top] = element;
}
}
public T pop() {
if(isEmpty()) {
throw new IllegalStateException("Stack is empty, cant pop anymore elements");
}else{
T value = stack[top];
top--;
return value;
}
}
public T peek() {
return stack[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
@Override
public String toString() {
return "Stack: "+ Arrays.toString(stack);
}
}
==========================================
========= ====== QUEUE ======== ================
import java.util.Arrays;
public class QueueUC<T> {
private int maxSize;
private T[] queue;
private int front;
private int rear;
public QueueUC(int size) {
maxSize = size;
queue = (T[]) new Object[maxSize];
front = -1;
rear = -1;
}
public void put(T element) {
if (isFull()) {
throw new IllegalStateException("Queue is full");
}
if (isEmpty()) {
front++;
rear++;
queue[rear] = element;
} else {
rear = (rear + 1) % maxSize;
queue[rear] = element;
}
}
public T get() {
T value = null;
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
if (front == rear) {
value = queue[front];
front = -1;
rear = -1;
} else {
value = queue[front];
front = (front + 1) % maxSize;
}
return value;
}
public T peek() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
return queue[front];
}
public boolean isEmpty() {
return (front == -1 && rear == -1);
}
public boolean isFull() {
return ((rear + 1) % maxSize == front);
}
@Override
public String toString() {
return "Queue: "+ Arrays.toString(queue);
}
}
=========================================
PART 2)
============= POINT ==============
public class Point {
int x;
int y;
Point(int xCoord,int yCoord){
this.x=xCoord;
this.y=yCoord;
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
@Override
public String toString(){
return "("+this.getX()+","+this.getY()+")";
}
}
=================================
============================= STACK TEST ======
public class StackTest {
public static void main(String args[]){
//String test
StackUC<String> ss = new StackUC<String>(2);
ss.push("a");
ss.push("b");
System.out.println(ss.toString()); //print the stack elements
ss.pop(); // should pop out "b" from stack
System.out.println(ss.pop()); //should print a as stack follows LIFO - last in first out
//point test
Point p1 = new Point(2,3);
Point p2 = new Point(4,5);
StackUC<Point> sp = new StackUC<Point>(3);
sp.push(p1);
sp.push(p2);
System.out.println(sp.toString());
System.out.println(sp.pop());
System.out.println(sp.peek());
//Integer test
StackUC<Integer> stack = new StackUC<Integer>(5);
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
System.out.println(stack.toString());
stack.pop(); // will pop out 8
System.out.println(stack.pop()); // will pop out 7
System.out.println(stack.pop()); // will pop out 6
// negative test keeping it commented
//stack.push(6); // will throw an stack full error as the max limit is crossed - which is valid
}
}
=================================================
=============QUEUE TEST ==========================
/*
Main program to test generic queue with Point, String and Integer type of elements.
*/
public class QueueTest {
public static void main(String[] args) {
//Test with Point
QueueUC<Point> pointQueue = new QueueUC<Point>(3);
pointQueue.put(new Point(1, 2)); // add point(1,2) to queue
pointQueue.put(new Point(1, 3)); // add point (1,3) to queue
System.out.println(pointQueue.toString() ); // print the queue
// remove elements QUEUE follows FIFO : first in first out
System.out.println("first point :" + pointQueue.get()); // should remove (1,2)
System.out.println("second point :" + pointQueue.get()); // should remove (1,3)
//Test with String
QueueUC<String> stringQueue = new QueueUC<String>(3);
stringQueue.put("string1");
stringQueue.put("string2");
System.out.println("first String :" + stringQueue.get());
System.out.println("second String :" + stringQueue.get());
System.out.println(stringQueue.toString() );
//Test with Integer
QueueUC<Integer> integerQueue = new QueueUC<Integer>(3);
integerQueue.put(1);
integerQueue.put(2);
System.out.println(integerQueue.toString() );
System.out.println("first Integer :" + integerQueue.get());
System.out.println("second Integer :" + integerQueue.get());
}
}
==================================================