Question: I need help with this java problem I haven't coded java in a long time so I am really rusty with the syntax: Implement Stack
I need help with this java problem I haven't coded java in a long time so I am really rusty with the syntax:
Implement Stack and Queue classes in Java using an ArrayList of Objects. Since java already has Stack and Queue classes, you can call your classes myStack and myQueue.
a) Implement your own Stack class called myStack in java. Your class should include the following methods:
Object pop(): returns and removes the elements on the top of the stack. A NoSuchElementException is thrown if pop() is called on an empty stack.
Object peek(): returns the element on the top of the stack without removing it. A NoSuchElementException is thrown if peek() is called on an empty stack.
push(Object: element): pushes the element onto the stack
boolean isEmpty(): returns true if no elements are present in the stack, else returns false.
int size(): returns the number of items in the stack.
b) Implement your own Queue class called myQueue in Java. Your class should include the following methods:
enqueue(Object: element): add an element to the queue.
Object front(): returns the head element of the queue without removing it. A NoSuchElementException is thrown if front() is called on an empty queue.
Object dequeue(): returns and removed the head of the queue. A NoSuchElementException is thrown if dequeue() is called on an empty queue.
boolean isEsmpty(): returns true if no elements are present in the queue, else returns false.
int size(): returns the number of items in queue.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
