For objects: Followup to IntQueue Class implementation

public int getFront()
{
Integer result = (Integer) data.get(front);
front++;
 return result.intValue();
}

I keep getting arrayoutofbound exception, how do I fix this ?
azcalv408Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
As objects said;

"Though you probably should do a test to ensure you haven't reached end of queue"

so you need:

public int getFront()
{
    if( front < 0 || front > data.size() - 1 )
    {
        System.err.println( "Fell off the end of the data...  Wrapping round to the beginning" ) ;
        front = 0 ;
    }
    Integer result = (Integer) data.get(front);
    front++;
    return result.intValue();
}
0
CEHJCommented:
>>
Integer result = (Integer) data.get(front);
front++;
return result.intValue();
>>

if you're intending there to alter result, you're not doing, btw
0
objectsCommented:
public int getFront()
{
   if (front==-1)
   {
      throw new RuntimeException("Queue is empty");
   }
   Integer result = (Integer) data.get(front);
   front++;
   if (front>=data.size())
   {
      front = -1;
   }
   return result.intValue();
}
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

objectsCommented:
Actually not sure I like that
0
objectsCommented:
This should work better:

public int getFront()
{
   if (front>=data.size())
   {
      throw new RuntimeException("Queue is empty");
   }
   Integer result = (Integer) data.get(front);
   front++;
   return result.intValue();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
You seem to be confusing the items that are being queued and the question of indexes/where the front is. From the spec in your previous question, there seems to be no assumptions made about what type of item is being queued, so the following

>>Integer result = (Integer) data.get(front);

doesn't seem to make much sense
0
objectsCommented:
Not sure I follow, and whats that got to do with dealing with an empty queue?
0
CEHJCommented:
Working from memory (i no longer have the previous code open) the only methods that can affect where front is are the constructor and insert (and the latter sets the position of front). Therefore, the following should be quite adequate

public int getFront()
{
   if (data.size() == 0)
   {
      throw new RuntimeException("Queue is empty");
   }
   return front;
}

0
azcalv408Author Commented:
>>if (data.size() == 0)
   {
      throw new RuntimeException("Queue is empty");
   }
 >>  return front;

I think getFront() should return an element of the index front, not the index itself.
0
objectsCommented:
> I think getFront() should return an element of the index front, not the index itself.

That is correct, and should also increment the index.
The code I posted above should do the trick, let me know if you have any problems with it.
And if so, also post the code you are testing it with.
0
CEHJCommented:
Well, i wasn't sure whether you wanted the index or the object. In that case the function should return an Object, not an int (or an Integer).
0
objectsCommented:
> should return an Object

That'd be a bit pointless for an IntQueue.
0
CEHJCommented:
Well i don't remember seeing what you're meant to be queueing. If it's just numbers and you're not actually removing anything, merely resetting a pointer to the front of the queue, then you may as well just use an int[]. It's a bit pointless using a collection class
0
azcalv408Author Commented:
>>public int getFront()
{
   if (front>=data.size())
   {
      throw new RuntimeException("Queue is empty");
   }
   Integer result = (Integer) data.get(front);
   front++;
   return result.intValue();
>>}

objects, your code doesn't seem to work, here's the main test program:
public static void main(String[] args)
{
IntQueue queue1 = new IntQueue();
for(int i = 0; i < 20; i++)
queue1.insert(i);
System.out.println("\n now queue1 = " +queue1);
for(int j = 0; j < 15; j++)
queue1.getFront();
System.out.println("\n now queue1 = " +queue1);
}

the second part of queue1, after the second for loop, queue1 should be [15, 16, 17, 18, 19], but the program gives [0, 1, 2, 3, 4, 5, 6, 7..., 19], same as the first output. Can you see why ?
0
objectsCommented:
Think the problems in your toString() method. It should only print elements from front onwards.
0
azcalv408Author Commented:
oh this is all I have for toString
    public String toString()
    {
      String answer = (data).toString();
          return answer;
    }

how do i print from front to end ?
0
objectsCommented:
should be:

    public String toString()
    {
         String answer = data.subList(front, data.size()).toString();
         return answer;
    }
0
azcalv408Author Commented:
objects, sorry to bug you again but I wrote an equals method and it doesnt seem to work:
 public boolean equals(Object obj)
 {
      if(obj instanceof IntQueue)
      {
                 IntQueue queue = (IntQueue) obj;
                 return queue.data.subList(front,data.size()).equals
                (this.data.subList(front,data.size()));
      }
      else
      return false;
 }
can you see what's wrong? thanks a lot
0
objectsCommented:
your comparing the same front and data in some cases. Try

public boolean equals(Object obj)
 {
     if(obj instanceof IntQueue)
     {
                IntQueue queue = (IntQueue) obj;
                return queue.data.subList(queue.front,queue.data.size()).equals
               (this.data.subList(front,data.size()));
     }
     else
     return false;
 }
0
objectsCommented:
0
TimYatesCommented:
:-(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.