Link to home
Start Free TrialLog in
Avatar of azcalv408
azcalv408

asked on

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 ?
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

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();
}
>>
Integer result = (Integer) data.get(front);
front++;
return result.intValue();
>>

if you're intending there to alter result, you're not doing, btw
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();
}
Actually not sure I like that
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Not sure I follow, and whats that got to do with dealing with an empty queue?
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;
}

Avatar of azcalv408
azcalv408

ASKER

>>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.
> 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.
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).
> should return an Object

That'd be a bit pointless for an IntQueue.
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
>>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 ?
Think the problems in your toString() method. It should only print elements from front onwards.
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 ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial