**********************JAVA********************** INSTRUCTIONS: Download this cod
ID: 3893869 • Letter: #
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 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<E> extends Iterable<E>,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<E> implements Queue<E> {
/** 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<count; k++)
{ big[k]=data[front]; front=next_index(front); }
front = 0; rear = count-1; data = big;
}
}
/** Creates a new instance of MyQueue */
public ArrayQueue() {
data = (E[]) new Object[64]; count = 0; front = 0; rear = 63;
}
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<count;i++){
if(this.data[i]==temp.data[i]){
ManyItems++;
if(ManyItems==count){
return true;
}
}
}
return false;
}
public Iterator<E> iterator()
{
return new QueueIterator();
}
public String toString(){
StringBuffer str= new StringBuffer();
for (int i=0; i<count;i++){
str.append(data[i]);
}
//str.append(data[front]);
//System.out.println(str);
return new String(str);
}
private class QueueIterator<E> implements Iterator<E> {
public boolean hasNext(){ //To change body of generated methods, choose Tools | Templates.
for(int i=0;i<count;i++){
if(data[front]!=null){
return true;
}
if (data[rear]!=null){
return true;
}
}
return false;
}
public E next() {
/**
for (int i=0; i<count; i++){
data[front]=data[0];
front++;
rear++;
*/
count=data.length;
if(front==count){
front=0;
}
else{
front++;
}
if(rear==count){
rear=0;
}
else{
rear++;
}
return null;
}
}
}
//EXQUEUE FILE
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<String> q = new ArrayQueue<String>();
Queue<String> p= new ArrayQueue<String>();
/**
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());
}
}
Explanation / Answer
Given below is the completed code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
ArrayQueue.java
--------
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<E> implements Queue<E> {
/** 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<count; k++)
{ big[k]=data[front]; front=next_index(front); }
front = 0; rear = count-1; data = big;
}
}
/** Creates a new instance of MyQueue */
public ArrayQueue() {
data = (E[]) new Object[64]; count = 0; front = 0; rear = 63;
}
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<count;i++){
if(this.data[i]==temp.data[i]){
ManyItems++;
if(ManyItems==count){
return true;
}
}
}
return false;
}
public Iterator<E> iterator()
{
return new QueueIterator();
}
public String toString(){
StringBuffer str= new StringBuffer();
for (int i=0; i<count;i++){
str.append(data[i]);
str.append(" ");
}
//str.append(data[front]);
//System.out.println(str);
return new String(str);
}
private class QueueIterator implements Iterator<E> {
private int index;
private int iterated; //the no. of elements iterated
QueueIterator()
{
index = front;
iterated = 0;
}
public boolean hasNext(){
return iterated < count;
}
public E next() {
E val = data[index];
index = next_index(index);
iterated++;
return val;
}
}
}
ExQueue.java
-----------
package ArrayQueue;
import java.util.Iterator;
/**
* 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<String> q = new ArrayQueue<String>();
Queue<String> p= new ArrayQueue<String>();
/**
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 = " + q.toString());
System.out.println("p = " + p.toString());
System.out.println(q.equals(p));
//q.dequeue();
//q.enqueue("sara");
//q.dequeue();
//test the iterator
System.out.println(" Printing the elements of q using iterator");
Iterator<String> iter = q.iterator();
while(iter.hasNext())
System.out.print(iter.next()+ " ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.