Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedimage text in transcribed

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 added/enqueued 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. Stack: Queue: Last in, frst out First in, first out Requirements: Write a new class called stackBasedQueue that implements following queue operations using only 2 stacks: enqueue(newltem)- Add an integer input to the back of queue. dequeue() Remove the element from the front of the queue. peek)- Get the front element. isEmpty) Return whether the queue is empty or not. stackBasedQueuel) Constructor that initialize the stacks to create a new queue object .--Class Template--- import java.util.stack; public class stackBasedQueue Stack stack1; Stack stack2; public void stackBasedOueue) // Enter code Here // This is a stack initialization Stack); public boolean isEmpty() ( // Enter code Here return true; public void enqueue(int newItem)1 // Enter code Here public int dequeue () throws QueueException // In case of problem: // throw new QueueException("You can not DEQUEUE on an EMPTY queue(") // Enter code Here return 0; ublic void dequeueA1lo0 //Enter code Here public int peek) throws QueueExceptiont // In case of problem: // throw new QueueException ("You can not PEEK on an EMPTY queue :((") // Enter code Here return 1; // Tester code public static void main (String[] args) stackBasedQueue qnew stackBasedQueue) q. enqueue (1); q.enqueue (2); q. enqueue (3); System . out .print 1n("Ans#1 : "+q.peek ( )); // Should be 1 q. dequeue); q. dequeue); System.out.print 1n("Ans#2: "4q.peek()); // Should be 3 System.out.println("Ans#3 "+q.isEmpty()); // Should be false q. dequeueAll); System . out.print 1n("Ans#4. "+4.isEmpty()); // Should be true System . out.print 1n("Ans#5: "+9 . peek()); //Should throw exception with message: "You can not PEEK on an EMPTY queue " public class OueueException extends java.lang. RuntimeException public QueueException(String s) super (s) Expected Program output: 2ProblemsJavadoc Declaration Console 3 stackBasedQueue [Java Application] CProgram Filesjavaljre1.8.0 7Abinjavaw.exe (Feb 10, 2019, 4:20:25 PM Exception in thread "main" QueueException: You can not PEEK on an EMPTY queue :( at stackBasedQueue.main(stackBasedQueue iava:19) Ans#1 : Ans#2: Ans#3 : Ans#4 : 1 3 false true

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

ISBN: 3319234609, 978-3319234601

Students also viewed these Databases questions

Question

Use MATLAB to compute the mean for each column in matrix B

Answered: 1 week ago