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
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
/** 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 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 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started