Home > Enterprise >  Java implementation of Queue producing java.lang error
Java implementation of Queue producing java.lang error

Time:10-17

I'm trying to implement Queue data structure in java by creating two different classes one is queue defination and methods class and other is where I'll be using it. However on running these I can't run this and keep getting an error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: at QueueArrayImplementation.main(Queue.java:61)

I've studied and tried modifying access modifiers but can't understand where I'm making a mistake

class Queue{
    private static int front,rear,capacity;
    private static int queue[];

    Queue(int size){
        front = rear = 0;
        capacity = size;                    //  left space in queue array
        queue = new int[capacity];          //  an array as queue 
    }

    protected void enQueue(int item){
        if(capacity == rear){               //  if queue is full
            System.out.println("Queue is full");
        }
        else{                               //  insert element at the rear
            queue[rear] = item;
            rear  ;
        }
        return;
    }
    protected void deQueue(){               //  remove element from rear
        if(front==rear){
            System.out.println("Queueis empty");
            return ;
        }
        else{
            for(int i=0;i<rear-1;i  ){      //  shift one element to right 
                queue[i] = queue[i 1];
            }

            if(rear < capacity)             //  clear the last element which is repeated in array
                queue[rear] = 0;

        rear--;                             //  decrease rear's flag 
        }
        return;
    }
    protected void queueDisplay(){
        int i;
        if(front == rear){
            System.out.println("Queue is Empty");
            return;
        }

        for(i=front ; i<rear ; i  ){        //  traverse from front to rear
            System.out.println(" %d, ",queue[i]);
        }
        return;
    }
    protected void queueFront(){
        if(front==rear){
            System.out.println("Queue is empty");
            return;
        }
        System.out.println("Queue front is",queue[front]);
        return;
    }
}

public class QueueArrayImplementation{
    public static void main(String[] args){
        Queue q = new Queue(4);
        System.out.println("initital Queue");
        q.queueDisplay();
        q.enQueue(10);
        q.enQueue(20);
        q.enQueue(30);
        q.queueDisplay();
        q.queueFront();   
    }
}

CodePudding user response:

The methods you are declaring are protected, this is only used in its subclasses, make it public if you want to use it

CodePudding user response:

Whenever we declare a method as protected we are telling the compiler that the functionality exposed by the class is required to be enhanced (extended) first and then used in the application.

So following things can be done to make the application work :

  1. We can move the main method inside the Queue class.

  2. We can make the methods (enQueue/deQueue/queueDisplay/queueFront) public.

  • Related