Solved

For objects: Followup to  IntQueue Class implementation

Posted on 2003-11-26
21
256 Views
Last Modified: 2012-05-04
public int getFront()
{
Integer result = (Integer) data.get(front);
front++;
 return result.intValue();
}

I keep getting arrayoutofbound exception, how do I fix this ?
0
Comment
Question by:azcalv408
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 4
  • +1
21 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9825707
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9825784
>>
Integer result = (Integer) data.get(front);
front++;
return result.intValue();
>>

if you're intending there to alter result, you're not doing, btw
0
 
LVL 92

Expert Comment

by:objects
ID: 9827125
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 92

Expert Comment

by:objects
ID: 9827141
Actually not sure I like that
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9827151
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9827286
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
 
LVL 92

Expert Comment

by:objects
ID: 9827422
Not sure I follow, and whats that got to do with dealing with an empty queue?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9827554
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
 

Author Comment

by:azcalv408
ID: 9827847
>>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
 
LVL 92

Expert Comment

by:objects
ID: 9827864
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9827985
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
 
LVL 92

Expert Comment

by:objects
ID: 9828153
> should return an Object

That'd be a bit pointless for an IntQueue.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9828210
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
 

Author Comment

by:azcalv408
ID: 9828217
>>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
 
LVL 92

Expert Comment

by:objects
ID: 9828236
Think the problems in your toString() method. It should only print elements from front onwards.
0
 

Author Comment

by:azcalv408
ID: 9828255
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
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 9828263
should be:

    public String toString()
    {
         String answer = data.subList(front, data.size()).toString();
         return answer;
    }
0
 

Author Comment

by:azcalv408
ID: 9828332
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
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 9828364
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
 
LVL 92

Expert Comment

by:objects
ID: 9828440
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9830253
:-(
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 441
How do I remove an object from a 3 60
glassfish admin console 1 45
Java class and jar 3 58
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question