0
\$\begingroup\$

Description: An array-based Queue class. Since this class is designed using an array the size of the Queue is limited to whatever the program gives you. MAX_SIZE is made public to let a user know about MAX_SIZE. In Constructor, frontIndex and rearIndex set to 0. A point can be raised what if front() or remove() gets called on empty Queue then Integer.MIN_VALUE is returned and size would still be 0.

package queue.arrayQueue;

public class ArrayQueue
{
    public static final int MAX_SIZE = 1040;
    private int frontIndex;
    private int rearIndex;
    private int queueElement[];

    public ArrayQueue()
    {
        frontIndex = 0;
        rearIndex = 0;
        queueElement = new int [MAX_SIZE];
    }

    public int size()
    {
        return (MAX_SIZE + rearIndex - frontIndex) % MAX_SIZE;
    }

    public boolean isFull()
    {
        return (size() == MAX_SIZE - 1);
    }

    public boolean isEmpty()
    {
        return (size() == 0);
    }

    public void enqueue(int value)
    {
        if(!isFull())
        {
            queueElement[rearIndex] = value;
            rearIndex = (rearIndex + 1) % MAX_SIZE;
        }
    }

    public int front()
    {
        if(!isEmpty())
        {
            return queueElement[frontIndex];
        }
        return Integer.MIN_VALUE;
    }

    public void dequeue()
    {
        if (!isEmpty())
        {
            frontIndex = (frontIndex + 1) % MAX_SIZE;
        }
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ there is no remove() in the code. did you mean dequeue()? \$\endgroup\$ Commented May 30, 2018 at 9:32
  • \$\begingroup\$ Is there a reason why you define isFull() as size() == MAX_SIZE - 1 rather than size() == MAX_SIZE? I would expect MAX_SIZE to be the maximum amount of enqueued elements. \$\endgroup\$ Commented May 30, 2018 at 13:56
  • \$\begingroup\$ Your "off-topic" question might have a place on Code Review's Meta site, but being on-topic is a requirement for questions on Code Review, so you might want to edit the question to remove that part. I am not aware of any weekly posting limit as long as the questions are not duplicates and not off-topic. \$\endgroup\$ Commented May 30, 2018 at 14:02

1 Answer 1

1
\$\begingroup\$

It is good practise to define an interface (or use one that is already defined) and implement it in your class, especially for containers. That way a user can replace your implementation by another one (or vice versa) without changing any surrounding code.


public static final int MAX_SIZE = 1040;
private int frontIndex;
private int rearIndex;
private int queueElement[];

I like to improve readability by separating groups of members with different access modifiers with a blank line in between.

int queueElement[] is valid in Java, but the syntax stems from C. I would always use int[] queueElement, which is more idomatic in Java. In my opinion it is also way more intuitive, because it reads like "an integer array named queueElement", rather than "an integer named queueElement array".

Since an array (usually) contains multiple elements, the convention is to use a plural name like queueElements.


public ArrayQueue()
{
    frontIndex = 0;
    rearIndex = 0;
    queueElement = new int [MAX_SIZE];
}

These member variables are already set to 0 by default. It is good to be explicit, but I would do it in the member declaration rather than the constructor to make the code shorter without losing readability.


public boolean isFull()
{
    return (size() == MAX_SIZE - 1);
}

Most users would probably expect MAX_SIZE to be the maximum amount of enqueued items, rather than MAX_SIZE - 1. I am not sure whether you thought about it as an array index, or whether this compensates for an off by one error in the size() method. If a user checks for the MAX_SIZE and adds 1040 items in a loop, then, because enqueue() neither throws an exception nor returns anything when the queue is full, the last item will just be ignored silently.

\$\endgroup\$
2
  • \$\begingroup\$ should the Interface be in the same package as the class? @Raimund \$\endgroup\$ Commented May 30, 2018 at 17:43
  • 1
    \$\begingroup\$ Not necessarily, but since it is your default implementation of the interface, I would put it in the same package. \$\endgroup\$ Commented Jun 2, 2018 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.