Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CSc 2720 Data Structures: Assignment 2 How to Submit: Turn the java file in Folder Assignment 2 in iCollege no later than 11:59 p.m. on
CSc 2720 Data Structures: Assignment 2 How to Submit: Turn the java file in Folder Assignment 2 in iCollege no later than 11:59 p.m. on 02/22/2018. Notes: 1. 2. This is a common Google and other tech companies interview question. Make sure not to copy the code from the internet. Tunitin (plaglarism software), will be activated for detecting plagiarism. Refresher We have seen in class that a queue is an abstract data type, also called a First-In-First-Out FIFO) data structure because the first element addedfenqueued to the queue is always the first one to be removed/dequeued. A stack is an abstract data type, also called a Last-in-First-Out LIFO) data structure because the first element added/pushed to the stack is always the last one to be removed/poped Gueuei Write a new class called stackBasuRu that implements fallowing queue aperations using only 2 stacks enqueuesnew ternAdd an integer input to the back of queue dequeue) Remove the element from the front of the queue peeki) Get the front elernent isErnptyll- Returm whethethe qeue is ernpty or not. stackBas Constructor that initialize the stacks to create a new queue object Class Expected Program output: inport java.util.Stack; public class stackBasedQueue Stack stacki; Stack stack2; Exception in thread "main" QueueException: You can not PEEK on an EMPTY queue :( public void / Enter code Here // This is a stack initialization Stack s-new Stack; Ans#1 : Ans#2 : Ans#3 : Anst4: 1 3 false true public boolean isEmpty) Enter code Here return true; public void enqueue(int newIten) /Enter code Here public int dequeue) throws QueueExceptiant In case of problem: throw new QueueException("You can not DEQUEUE on an EMPTY queue( Enter code Here return e public void dequeueAllo //Enter code Here public int peek) throws QueueException In case of problem: throw new QueueException("You can nat PEEK on an EMPTY queue : Enter code Here return 1; // Tester code public static void main(String) args)t stackBasedQueue q new stackBasedQueue) q.enqueue(1) q.enqueu(2); System.out.printIn("Ans#1. ..+4. peek()); // Should be 1 q.dequeue); q.dequeue); System-out, print1n("Ans#2: 5ystem.out , print1n("Ans#3- "+q.peek()); // Should be 3 "4q.1sEmpty()); // Should be false q. dequeueAll; System-out.print1n("Ans#4. ..+4.isEmpty()); System.out.printin("AnslS: "+q.peek)) // Should be true //Should throw exception with nessage: "You can not PEEK on an EMPTY queue( public class QueueException extends ava.lang.RuntineException public QueueException(String s) super(s)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started