Solved

For objects: Followup to  IntQueue Class implementation

Posted on 2003-11-26
21
252 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
  • 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now