Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Secial requirements: Both MyStack and MyQueue Classes 1. size method a. Returns the number of items in the collection. 2. empty method a. Returns true

Secial requirements:

Both MyStack and MyQueue Classes

1. size method a. Returns the number of items in the collection. 2. empty method a. Returns true if the size of the collection is zero; otherwise returns false. 3. peek method a. Returns the item at the front or top of the collection. b. Throws a NoSuchElementException if the collection is empty 4. search method a. Returns the (zero-based) number of elements from the front or top of the collection where the specified item is first found. Returns -1 if the item is not found in the collection.

MyStack 1. You must implement the IStack interface as either a linked list or an array list (refer to your work from Assignment 02). Your MyStack must not be arbitrarily limited to any fixed size at run-time. 2. push method a. Insert the new item at the top of the stack. 3. pop method a. Remove and return the item at the top of the stack. b. Throws a NoSuchElementException if the stack is empty

MyQueue 1. You must implement the IQueue interface as either a linked list or an array list (refer to your work from Assignment 02). Your MyQueue must not be arbitrarily limited to any fixed size at run-time. 2. add method a. Insert the new item at the back of the queue. 3. remove method a. Remove and return the item at the front of the queue. b. Throws a NoSuchElementException if the queue is empty

image text in transcribed

given code

---------------------------------------------------------------------------

public class CSE205_Assignment03 {

public static void main(String[] args) {

System.out.println("IStack tests -----------------------------");

IStack_ctor_test();

IStack_push1_test();

IStack_push3_test();

IStack_push100_test();

IStack_pop1_test();

IStack_pop3_test();

IStack_pop100_test();

IStack_search100_test();

System.out.println("IQueue tests -----------------------------");

IQueue_ctor_test();

IQueue_add1_test();

IQueue_add3_test();

IQueue_add100_test();

IQueue_remove1_test();

IQueue_remove3_test();

IQueue_remove100_test();

IQueue_search100_test();

}

public static void IStack_ctor_test()

{

IStack s = new MyStack();

assertEqual(0, s.size(), "IStack_ctor_test: size", false);

assertEqual(true, s.empty(), "IStack_ctor_test: empty", false);

}

public static void IStack_push1_test()

{

IStack s = new MyStack();

s.push(1);

assertEqual(1, s.size(), "IStack_push1_test: size", false);

assertEqual(false, s.empty(), "IStack_push1_test: empty", false);

assertEqual(1, (int)s.peek(), "IStack_push1_test: peek", false);

}

public static void IStack_push3_test()

{

IStack s = new MyStack();

s.push(1);s.push(2);s.push(3);

assertEqual(3, s.size(), "IStack_push3_test: size", false);

assertEqual(3, (int)s.peek(), "IStack_push3_test: peek", false);

}

public static void IStack_push100_test()

{

IStack s = new MyStack();

for (int i = 0; i

s.push(i);

assertEqual(100, s.size(), "IStack_push100_test: size", false);

assertEqual(99, (int)s.peek(), "IStack_push100_test: peek", false);

}

public static void IStack_pop1_test()

{

IStack s = new MyStack();

s.push(1);

assertEqual(1, s.size(), "IStack_pop1_test: size", false);

assertEqual(1, (int)s.pop(), "IStack_pop1_test: pop", false);

assertEqual(0, s.size(), "IStack_pop1_test: size", false);

assertEqual(true, s.empty(), "IStack_pop1_test: empty", false);

}

public static void IStack_pop3_test()

{

IStack s = new MyStack();

s.push(1);s.push(2);s.push(3);

assertEqual(3, s.size(), "IStack_pop3_test: size", false);

assertEqual(false, s.empty(), "IStack_pop3_test: empty", false);

assertEqual(3, (int)s.pop(), "IStack_pop3_test: pop", false);

assertEqual(2, (int)s.peek(), "IStack_pop3_test: peek", false);

assertEqual(2, s.size(), "IStack_pop3_test: size", false);

assertEqual(2, (int)s.pop(), "IStack_pop3_test: pop", false);

assertEqual(1, (int)s.peek(), "IStack_pop3_test: peek", false);

assertEqual(1, s.size(), "IStack_pop3_test: size", false);

assertEqual(1, (int)s.pop(), "IStack_pop3_test: pop", false);

assertEqual(true, s.empty(), "IStack_pop3_test: empty", false);

}

public static void IStack_pop100_test()

{

IStack s = new MyStack();

for (int i = 0; i

s.push(i);

assertEqual(100, s.size(), "IStack_pop100_test: size", false);

assertEqual(99, (int)s.pop(), "IStack_pop100_test: pop", false);

assertEqual(98, (int)s.peek(), "IStack_pop100_test: peek", false);

assertEqual(99, s.size(), "IStack_pop100_test: size", false);

for (int i = 0; i

s.pop();

assertEqual(1, (int)s.pop(), "IStack_pop100_test: pop", false);

assertEqual(0, (int)s.peek(), "IStack_pop100_test: peek", false);

assertEqual(1, s.size(), "IStack_pop100_test: size", false);

}

public static void IStack_search100_test()

{

IStack s = new MyStack();

for (int i = 0; i

s.push(i);

assertEqual(99, s.search(0), "IStack_search100_test: search", false);

assertEqual(98, s.search(1), "IStack_search100_test: search", false);

assertEqual(1, s.search(98), "IStack_search100_test: search", false);

assertEqual(0, s.search(99), "IStack_search100_test: search", false);

assertEqual(-1, s.search(999), "IStack_search100_test: search", false);

for (int i = 0; i

s.pop();

assertEqual(0, s.search(49), "IStack_search100_test: search", false);

assertEqual(1, s.search(48), "IStack_search100_test: search", false);

assertEqual(48, s.search(1), "IStack_search100_test: search", false);

assertEqual(49, s.search(0), "IStack_search100_test: search", false);

assertEqual(-1, s.search(999), "IStack_search100_test: search", false);

}

public static void IQueue_ctor_test()

{

IQueue q = new MyQueue();

assertEqual(0, q.size(), "IQueue_ctor_test: size", false);

assertEqual(true, q.empty(), "IQueue_ctor_test: empty", false);

}

public static void IQueue_add1_test()

{

IQueue q = new MyQueue();

q.add(1);

assertEqual(1, q.size(), "IQueue_add1_test: size", false);

assertEqual(false, q.empty(), "IQueue_add1_test: empty", false);

assertEqual(1, (int)q.peek(), "IQueue_add1_test: peek", false);

}

public static void IQueue_add3_test()

{

IQueue q = new MyQueue();

q.add(1);q.add(2);q.add(3);

assertEqual(3, q.size(), "IQueue_add3_test: size", false);

assertEqual(1, (int)q.peek(), "IQueue_add3_test: peek", false);

}

public static void IQueue_add100_test()

{

IQueue q = new MyQueue();

for (int i = 0; i

q.add(i);

assertEqual(100, q.size(), "IQueue_add100_test: size", false);

assertEqual(0, (int)q.peek(), "IQueue_add100_test: peek", false);

}

public static void IQueue_remove1_test()

{

IQueue q = new MyQueue();

q.add(1);

assertEqual(1, q.size(), "IQueue_remove1_test: size", false);

assertEqual(1, (int)q.remove(), "IQueue_remove1_test: pop", false);

assertEqual(0, q.size(), "IQueue_remove1_test: size", false);

assertEqual(true, q.empty(), "IQueue_remove1_test: empty", false);

}

public static void IQueue_remove3_test()

{

IQueue q = new MyQueue();

q.add(1);q.add(2);q.add(3);

assertEqual(3, q.size(), "IQueue_remove3_test: size", false);

assertEqual(false, q.empty(), "IQueue_remove3_test: empty", false);

assertEqual(1, (int)q.remove(), "IQueue_remove3_test: pop", false);

assertEqual(2, (int)q.peek(), "IQueue_remove3_test: peek", false);

assertEqual(2, q.size(), "IQueue_remove3_test: size", false);

assertEqual(2, (int)q.remove(), "IQueue_remove3_test: pop", false);

assertEqual(3, (int)q.peek(), "IQueue_remove3_test: peek", false);

assertEqual(1, q.size(), "IQueue_remove3_test: size", false);

assertEqual(3, (int)q.remove(), "IQueue_remove3_test: pop", false);

assertEqual(true, q.empty(), "IQueue_remove3_test: empty", false);

}

public static void IQueue_remove100_test()

{

IQueue q = new MyQueue();

for (int i = 0; i

q.add(i);

assertEqual(100, q.size(), "IQueue_remove100_test: size", false);

assertEqual(0, (int)q.remove(), "IQueue_remove100_test: pop", false);

assertEqual(1, (int)q.peek(), "IQueue_remove100_test: peek", false);

assertEqual(99, q.size(), "IQueue_remove100_test: size", false);

for (int i = 0; i

q.remove();

assertEqual(98, (int)q.remove(), "IQueue_remove100_test: pop", false);

assertEqual(99, (int)q.peek(), "IQueue_remove100_test: peek", false);

assertEqual(1, q.size(), "IQueue_remove100_test: size", false);

}

public static void IQueue_search100_test()

{

IQueue q = new MyQueue();

for (int i = 0; i

q.add(i);

assertEqual(0, q.search(0), "IQueue_search100_test: search", false);

assertEqual(1, q.search(1), "IQueue_search100_test: search", false);

assertEqual(98, q.search(98), "IQueue_search100_test: search", false);

assertEqual(99, q.search(99), "IQueue_search100_test: search", false);

assertEqual(-1, q.search(999), "IQueue_search100_test: search", false);

for (int i = 0; i

q.remove();

assertEqual(0, q.search(50), "IQueue_search100_test: search", false);

assertEqual(1, q.search(51), "IQueue_search100_test: search", false);

assertEqual(48, q.search(98), "IQueue_search100_test: search", false);

assertEqual(49, q.search(99), "IQueue_search100_test: search", false);

assertEqual(-1, q.search(999), "IQueue_search100_test: search", false);

}

public static void assertEqual(int expected, int actual, String message, boolean silentPass)

{

if (expected == actual && ! silentPass)

System.out.printf("--- PASSED: %s ", message);

if (expected != actual)

System.out.printf("*** FAILED: (expected: %d, actual: %d) %s ", expected, actual, message);

}

public static void assertEqual(boolean expected, boolean actual, String message, boolean silentPass)

{

if (expected == actual && ! silentPass)

System.out.printf("--- PASSED: %s ", message);

if (expected != actual)

System.out.printf("*** FAILED: (expected: %b, actual: %b) %s ", expected, actual, message);

}

}

-----------------------------------------------------------------

/**

A queue is a first in first out (FIFO) data structure.

New items are added to the end of the list.

Items are removed from the front of the list.

*/

public interface IQueue {

/**

Inserts the specified element at the end of this queue.

*/

public void add(Object item);

/**

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

@return the first item in this queue or null if the queue is empty

*/

public Object peek();

/**

searches the queue for an item.

@return the zero-based index of the item in the queue; returns -1 if the item is not in the queue.

*/

public int search(Object item);

/**

Retrieves and removes the head of this queue.

@return the first item in the queue

*/

public Object remove();

/**

A count of the number of items in the queue.

@return A count of the number of items in the queue.

*/

public int size();

/**

Tests if this queue is empty.

@return true if the queue is empty

*/

public boolean empty();

}

-----------------------------------------------------------

/**

A stack is a last in first out (LIFO) data structure.

New items are pushed onto the top of the list.

Items are popped from the top of the list.

*/

public interface IStack {

/**

Pushes an item onto the top of this stack.

*/

public void push(Object item);

/**

Looks at the object at the top of this stack without removing it from the stack.

@return the item at the top of the stack or throws a NoSuchElementException is the stack is empty

*/

public Object peek();

/**

searches the stack for an item.

@return the zero-based index of the item in the stack; returns -1 if the item is not in the stack.

*/

public int search(Object item);

/**

Removes the object at the top of this stack and returns that object as the value of this function.

@return the item at the top of the stack or throws a NoSuchElementException is the stack is empty

*/

public Object pop();

/**

A count of the number of items in the stack.

@return A count of the number of items in the stack.

*/

public int size();

/**

Tests if this stack is empty.

@return true if the stack is empty

*/

public boolean empty();

}

--------------------------------------------------------

import java.util.NoSuchElementException;

public class MyQueue implements IQueue {

// add any necessary variables here . . -----

@Override

public void add(Object item) {

}

@Override

public Object peek() {

}

@Override

public int search(Object item) {

}

@Override

public Object remove() {

}

@Override

public int size() {

}

@Override

public boolean empty() {

}

// add any necessary methods or classes below . . -----

}

--------------------------------------------------------------------------------

import java.util.NoSuchElementException;

public class MyStack implements IStack {

// add any necessary variables here . . -----

@Override

public void push(Object item) {

}

@Override

public Object peek() {

}

@Override

public int search(Object item) {

}

@Override

public Object pop() {

}

@Override

public int size() {

}

@Override

public boolean empty() {

}

// add any necessary methods or classes below . . -----

}

-------------------------------------------------------------------------------------------

CSE205_Assignment03.java (This file is complete you will make no changes to this file) IStack.java (This file is complete you will make no changes to this file) IQueue.java (This file is complete you will make no changes to this file) MyStack.java (you must complete this file) MyQueue.java (you must complete this file)

UML Class Diagram IStack > +push(item: Object): void + peek(): Object + search(item : Object):int + pop() : Object + size): int +add(item:Object): void +peek): Object + search(item : Object): int + remove() Object + size(): int + empty):boolean empty):boolean implements implements MyStack MyQueue

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

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

Students also viewed these Databases questions