Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ArrayQueue.java import java.util.Arrays; import java.util.NoSuchElementException; public class ArrayQueue implements QueueInterface { //================================================================================= ===== Properties private final static int DEFAULT_CAPACITY = 100; // default array size

ArrayQueue.java


import java.util.Arrays; import java.util.NoSuchElementException; public class ArrayQueue implements QueueInterface { //================================================================================= ===== Properties private final static int DEFAULT_CAPACITY = 100; // default array size private E[] elements = null; // the array for the queue private int front = 0, back = 0, size = 0; //================================================================================= ===== Constructors public ArrayQueue() { this(DEFAULT_CAPACITY); } public ArrayQueue(int capacity) { elements = (E[])new Object[capacity]; clear(); } //================================================================================= ===== Methods @Override public boolean add(E element) { verifyCapacity(); moveBack(); elements[back] = element; size++; return true; } @Override public E remove() { if(isEmpty()) throw new NoSuchElementException(); E ret = elements[front]; elements[front] = null; moveFront(); //size--; if(--size == 0) clear(); // no major advantage return ret; } @Override public E peek() { return (isEmpty() ? null : elements[front]); } @Override public boolean isEmpty() { return size == 0; } @Override public int size() { return size; } @Override public void clear() { if(!isEmpty()) { for(int i = 0; i < size; i++) { elements[front] = null; moveFront(); } } front = size = 0; back = elements.length - 1; } @Override public String toString() { if(isEmpty()) return "[]"; String ret = ""; int f = front; for(int i = 0; i < size; i++) { ret += ", " + elements[front]; moveFront(); } front = f; return "[" + ret.substring(2) + "]"; } //--------------------------------- Internal Methods public boolean isFull() { return size == elements.length; } private void moveFront() { front = ++front % elements.length; } private void moveBack() { back = ++back % elements.length; } private void verifyCapacity() { if(isFull()) { E[] orig = elements; elements = (E[])new Object[size * 2]; for(int i = 0; i < size; i++) { elements[i] = orig[front]; front = (front + 1) % size; } front = 0; back = size - 1; } } //===================================================== DELETE FOR PRODUCTION public String showActualArray() { return Arrays.toString(elements); } //===================================================== DELETE FOR PRODUCTION }




QueueInterface.java


public interface QueueInterface { boolean add(E element); // Returns true unless system memory is full E remove(); // Throws NoSuchElementException if empty E peek(); // Returns value at top or null if empty void clear(); boolean isEmpty(); // Returns true / false int size(); // Returns the number of elements in the Queue } 




Tester.java


public class Tester { public static void main(String[] args) { LinkedQueue nums = new LinkedQueue(); nums.add(1); System.out.printf("add(1): %-20s%n", nums); nums.add(2); System.out.printf("add(2): %-20s%n", nums); nums.add(3); System.out.printf("add(3): %-20s%n", nums); nums.add(4); System.out.printf("add(4): %-20s%n", nums); nums.add(5); System.out.printf("add(5): %-20s%n", nums); nums.remove(); System.out.printf("remove: %-20s%n", nums); nums.remove(); System.out.printf("remove: %-20s%n", nums); nums.add(6); System.out.printf("add(6): %-20s%n", nums); nums.add(7); System.out.printf("add(7): %-20s%n", nums); nums.add(8); System.out.printf("add(8): %-20s%n", nums); nums.remove(); System.out.printf("remove: %-20s%n", nums); nums.remove(); System.out.printf("remove: %-20s%n", nums); nums.remove(); System.out.printf("remove: %-20s%n", nums); nums.remove(); System.out.printf("remove: %-20s%n", nums); nums.remove(); System.out.printf("remove: %-20s%n", nums); nums.remove(); System.out.printf("remove: %-20s%n", nums); nums.add(1); System.out.printf("add(1): %-20s%n", nums); nums.add(2); System.out.printf("add(2): %-20s%n", nums); nums.add(3); System.out.printf("add(3): %-20s%n", nums); nums.add(4); System.out.printf("add(4): %-20s%n", nums); nums.add(5); System.out.printf("add(4): %-20s%n", nums); } }




LinkedQueue.java (The rest are finished this is supposed to be modified)


import java.util.NoSuchElementException;public final class LinkedQueue implements QueueInterface { //----------------------------------------------------- Inner Node Class private class Node { private E data; private Node next; private Node(E next) { this(next, null); } private Node(E data, Node next) { this.data = data; this.next = next; } } //---------------------------------------------------------------------- //====================================================================== Properties private Node first; private Node last; private int size; //====================================================================== Constructors public LinkedQueue() { } //====================================================================== Methods @Override public boolean isEmpty() { return false; } @Override public void clear() { } @Override public int size() { return 0; } @Override public boolean add(E newEntry) { return false; } // Returns null if empty @Override public E peek() { return null; } // Throws NoSuchElementException if empty @Override public E remove() { return null; } @Override public String toString() { return ""; } //----------------------------------------------- Personal Methods }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres the modified version of the LinkedQueue class that implements the QueueInterface java import j... 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

Survey of Accounting

Authors: Carl S Warren

5th Edition

9780538489737, 538749091, 538489731, 978-0538749091

More Books

Students also viewed these Programming questions