Question: **********************JAVA********************** INSTRUCTIONS: Download this code and complete the equals method and the iterator method. Two queues are equal if they contain the same data in

**********************JAVA**********************

INSTRUCTIONS: Download this code and complete the equals method and the iterator method. Two queues are equal if they contain the same data in the same order. Test your work by modifying the main method in ExQueue class.

NOTE: My equals method works, I'm just stuck on how to do the iterator method. Thanks so much!

//QUEUE FILE

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ArrayQueue;

/** * * */ /** * Download this code and complete the equals method and the iterator method. * Two queues are equal if they contain the same data in the same order. * Test your work by modifying the main method in ExQueue class. */ public interface Queue extends Iterable,Cloneable {

public void enqueue(E item); public E dequeue(); public int size( ); public boolean empty( ); }

//ARRAYQUEUE FILE

package ArrayQueue;

import java.util.*; /** * Download this code and complete the equals method and the iterator method. * Two queues are equal if they contain the same data in the same order. * Test your work by modifying the main method in ExQueue class. */ public class ArrayQueue implements Queue {

/** The number of items in the queue is stored in the instance variable count. For a non-empty queue, the items are stored in a circular array beginning at data[front] and continuing through data[rear]. The total capacity of the array is data.length. */

private E [] data; private int front, rear, count; private int next_index(int i) { return (i+1) % data.length; }

private void ensureCapacity(int minCap) { if ( data.length < minCap) { E [] big = (E[]) new Object[minCap]; for (int k=0; k

public void enqueue(E item) { if (count == data.length) ensureCapacity(2*count+1); rear = next_index(rear); data[rear] = item; count++; } public E dequeue() { if ( !empty() ) { E tmp = data[front]; data[front]=null; front = next_index(front); count--;return tmp; } else throw new NoSuchElementException("An empty queue is dequeued!."); }; public int size( ) { return count; } public boolean empty( ) { return (count == 0); } public boolean equals(Object q) { int ManyItems=0; ArrayQueue temp; temp=(ArrayQueue)q; for(int i=0; i iterator() { return new QueueIterator(); } public String toString(){ StringBuffer str= new StringBuffer(); for (int i=0; i implements Iterator {

public boolean hasNext(){ //To change body of generated methods, choose Tools | Templates. for(int i=0;i

public E next() { /** for (int i=0; i

package ArrayQueue; /** * Download this code and complete the equals method and the iterator method. * Two queues are equal if they contain the same data in the same order. * Test your work by modifying the main method in ExQueue class. */ public class ExQueue {

public static void main(String[] args) { Queue q = new ArrayQueue(); Queue p= new ArrayQueue(); /** q.enqueue("peter"); q.enqueue("john"); q.enqueue("mary"); q.enqueue("rosemary"); q.enqueue("rick"); q.enqueue("lee");

while(!q.empty()) { System.out.println(q.dequeue()); } */ q.enqueue("dan"); q.enqueue("tommy"); q.enqueue("tina"); q.enqueue("robb"); //p.enqueue("dan"); //p.enqueue("tommy"); //p.enqueue("tina"); //p.enqueue("robb"); //q.enqueue("john"); System.out.println(q.toString()); //System.out.println(p.toString()); //System.out.println(q.equals(p)); //q.dequeue(); //q.enqueue("sara"); //q.dequeue(); q.iterator(); System.out.println(q); //System.out.println(q.next()); //System.out.println(q.hasNext()); //System.out.println(q.toString()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!