1. Home >
  2. Apps >
  3. Groups >

Need HelP In JaVa

Question asked by hio in #Java on Jun 9, 2009
hio
hio 路 Jun 9, 2009
Rank E1 - BEGINNER
Hi ,,

i need some help in my home work ..

any help will be appreciated..

Assume that a queue of objects is implemented by an array of size N in a circular fashion
Two variables keep track of the front and rear: front and rear, as described in the following class definition.

public class ArrayQueue {
private Object Q[];
private int front,rear;
private int size;
public ArrayQueue(int capacity) {
Q = new Object[capacity];
// variables front and rear are to be initialized here, based // on the requirements below
}
// methods are to be added here based on the requirements below
}

Complete the constructor for ArrayQueue and write the methods enqueue, dequeue, size, front, isEmpty. (Provide a separate solution for each of the following cases.)

1. front is the index of the first element, rear is the index of the last element and the location following the last element is always kept empty.


2. front is the index of the first element, rear is the index of the last element and the location preceding the first element is always kept empty.

3. front is the index of the location preceding the first element, which is always kept empty, rear is the index of the location following the last element, which is always kept empty.

4. . front is the index of the location preceding the first element, which is always kept empty, rear is the index of the last element. (Draw your own picture.)


Thanks


Posted in: #Java
shalini_goel14
shalini_goel14 路 Jun 9, 2009
Rank A3 - PRO
Hi hio,
Please check the following piece of code showing how to write enqueue(), dequeue(), isEmpty() and size() methods in your class. I just made a rough class, based on your mentioned cases- you have to modify enqueue() and dequeue() methods accordingly. One more thing what you want to do in front() method ? I am not clear so I have not implemented that one.

/*
 * ArrayQueue.java
 *
 * Created on June 10, 2009, 9:28 AM
 */

package myjava;

/**
 *
 * @author shalinig
 */
public class ArrayQueue{

    public Object Q[] = null;

    private int front,rear;
    private int size;

   /** Creates a new instance of ArrayQueue*/
    public ArrayQueue(int capacity) {
        Q = new Object[capacity];
        front = 0;
        rear = capacity; 
        size = 0;
          }
    
   public boolean enqueue(Object x) throws ArrayIndexOutOfBoundsException{
       
       if((front + 1 ==rear ) || ((front +1 == size) && rear ==0)){
           
           return false;//queue is full
       }
       else{
           Q[front++] = x;
           size++;
   }
       return true;
   }
      
   
    public Object dequeue() throws ArrayIndexOutOfBoundsException{
       
        if (rear == front) {
            // queue is empty
            return null; 
        } else  {
            // return object
            size--;
            Object obj = Q[rear];
            Q[rear] = null;
            rear++;
            return obj;
        }

   }
    
    public int getSize(){
       return this.getSize();
    }
    
    public boolean isEmpty(){
        return (size==0?true:false);
    }
}
  
  
Let me know if any issues. 馃榾

Thanks !
hio
hio 路 Jun 10, 2009
Rank E1 - BEGINNER
i really appreciate your help ,,

but what have you done is for the first one only,,

can you help me with the other ,,

thanks,,
shalini_goel14
shalini_goel14 路 Jun 10, 2009
Rank A3 - PRO
hio
can you help me with the other ,,

thanks,,
Ha ha ! Too good man. 馃榾 Try to understand what I have done in my program(obviously something wrong would be there) and then you would understand yourself what changes you need to do in enqueue() and dequeue() methods accordingly for all cases.
hio
hio 路 Jun 10, 2009
Rank E1 - BEGINNER
ok ,, ok ,, I will ,,

if you don't mind see what i have dine until now :d

public class ArrayQueue2
{
private Object Q[];
private int front,rear;
private int size;

public ArrayQueue2(int capacity)
{
Q = new Object [capacity];
front = 0;
rear = -1;
}

public void enqueue (Object o)
{
if (front == ( rear + 2 ) % Q.length)
{
System.out.println("full quene");
}
else
{
rear = ( rear + 1 )% Q.length;
Q[rear] = o;
}
}

public Object dequene()
{
if (isEmpty())
{
System.out.println("empty quene");
return null;
}
else
{
Object o = Q[front];
front = ( front + 1 ) % Q.length;
return o;
}

}

public int size()
{
return ( Q.length + rear - front + 1 ) % Q.length;
}

public Object front()
{
if (this.isEmpty())
{
System.out.println("Empty queue");
return null;
}
else
{
return Q[front];
}
}

public boolean isEmpty()
{
return size() == 0;
}
}
is't wright :d
shalini_goel14
shalini_goel14 路 Jun 10, 2009
Rank A3 - PRO
Superb hio,

but why you are using size variable? Where you are setting its value ? You can remove that I guess.

Why you are setting rear=-1 ?
hio
hio 路 Jun 10, 2009
Rank E1 - BEGINNER
Thanks,,

I didn't notice the rear in the first answer :d

Since you asked me about the rear why you set it as rear = capacity;?? ,,

and the size i will deal with it 馃槢
shalini_goel14
shalini_goel14 路 Jun 10, 2009
Rank A3 - PRO
hio
Thanks,,

I didn't notice the rear in the first answer :d

Since you asked me about the rear why you set it as rear = capacity;?? ,,

and the size i will deal with it 馃槢
Man, if we see the literal meaning of rear, it means end and your setting it to -1 means you are putting it before front. Anyways it is going to affect much.

Go the way you want to go.馃榾
hio
hio 路 Jun 10, 2009
Rank E1 - BEGINNER
OK Bhot THANK you mera dost

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up