CrazyEngineers
  • Need HelP In JaVa

    hio

    hio

    @hio-Wktcms
    Updated: Oct 19, 2024
    Views: 1.1K
    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


    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • shalini_goel14

    MemberJun 9, 2009

    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 !
    Are you sure? This action cannot be undone.
    Cancel
  • hio

    MemberJun 10, 2009

    i really appreciate your help ,,

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

    can you help me with the other ,,

    thanks,,
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberJun 10, 2009

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • hio

    MemberJun 10, 2009

    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
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberJun 10, 2009

    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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • hio

    MemberJun 10, 2009

    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 😛
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberJun 10, 2009

    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.😀
    Are you sure? This action cannot be undone.
    Cancel
  • hio

    MemberJun 10, 2009

    OK Bhot THANK you mera dost
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register