Santa Monica College Java Worksheet
Description
( 10 points ) Queue (Links to an external site.) is an abstract data type (ADT) consisting of a sequence of entities with a first-in-first-out (FIFO) (Links to an external site.) property. Queue has the following operations, in alignment with the Java Queue interface (Links to an external site.) in the Oracle’s SDK:
add(x)
: inserts the specifiedx
element to the back of queue without violating capacity limitation.remove()
: removes the head (front) of queue, returning the removed element.peek()
: retrieves and displays but does not remove (i.e., read-only) the head of queue.isEmpty
: returns whether the queue is empty or not as boolean.
Implement the above FOUR (4) operations in your Solution class as follows. You are responsible for implementing the area shown in red below. Note that you are given TWO (2) local Stacks to help you manage the above listed queue operation
STARTER CODE
import java.util.Stack;
public class HW5_1 {
public static void main(String[] args) {
// your solution will be tested w/ random add()'s and remove()'s.
// Observers like peek(), isEmpty() will be used along the way to validate.
Solution sol = new Solution();
sol.add(8);
sol.add(1);
sol.peek(); // 8, if you use System.out.print()
sol.remove(); // 8
sol.isEmpty(); // false
sol.add(2);
sol.add(3);
sol.peek(); // 1
// etc
}
}
class Solution {
// PLEASE USE THESE GLOBAL STACKS FOR THIS PROBLEM
private Stack<Integer> pushStack = new Stack<Integer>();
private Stack<Integer> popStack = new Stack<Integer>();
/*
* ====================================
* !!! DO NOT MODIFY ABOVE THIS LINE!!!
* ====================================
*/
/**
* PURPOSE:
* PARAMETERS:
* RETURN VALUES:
*/
public void add(int x) {
// YOUR CODE HERE
}
/**
* PURPOSE:
* PARAMETERS:
* RETURN VALUES:
*/
public int remove() {
// YOUR CODE HERE
}
/**
* PURPOSE:
* PARAMETERS:
* RETURN VALUES:
*/
public int peek() {
// YOUR CODE HERE
}
/**
* PURPOSE:
* PARAMETERS:
* RETURN VALUES:
*/
public boolean isEmpty() {
// YOUR CODE HERE
}
}
CONSTRAINTS / ASSUMPTIONS
- You MUST use Java Stack (Links to an external site.) (java.utils.Stack), both local
pushStack
andpopStack
instances, to implement the solution queue for this problem; solution without the use of java.utils.Stack receives 0 logic points. - You will used the provided global stacks in your Solution class for this problem.
- You must use only the standard Stack methods,
pop()
,push()
,peek()
, andempty()
, for this problem. - This problem tests your understanding of how queue works in Java by implementing it from scratch using the Stack ADT you learned.
- All operations called on the queue are valid. This means both
remove()
andpeek()
will NOT be called on an empty queue. You won’t have to create custom Exceptions to handle errors.
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."