Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 5 (30 pts total) You will implement Stack and Queue classes in Java using an ArrayList of Objects. Since java already has Stack and

image text in transcribed

Question 5 (30 pts total) You will 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 my Queue. Submit your program files with this assignment. Only submit those ending with .java). Do not submit (.class) files. Be sure to add your name at the top of each file. (a) (10 pts) 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 NoSuchElementEx- ception' 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 NoSuchEle- mentException' 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) (10 pts) Implement your own Queue class called "my Queue 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 NoSuchElementEx- ception' 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 C) (10 pts) Write a short program called "test.java to verify the function of your Stack and Queue classes. This program will accept a string from the command line (Ex: 1 2 3 4 5). You can assume that the string will be in the form of numbers separated by a single space. You will insert all these numbers (from left to right as given in the string) into both your stack and queue. Then remove and print all elements in the order they are removed from both structures. Once the structures are empty, print the following: "The Stack/Queue is empty". Your program should not throw any of the exceptions listed above. Example output: 5 4 3 2 1 The Stack is empty 1 2 3 4 5 The Queue is empty

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

Students also viewed these Databases questions