• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing item positions in a queue  RSS feed

 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I got an interesting challenge today. I think logically I know what I have to do but I'm at a complete loss as for the actual coding implementation. So I have to develop this method called moveToBack(T entry). What this is meant to do is as implies, move entry to the back of my queue. As simple as it sounds, I know that I cant just move its position that simple. I know that I'll have to essentially remove whatever the desired value is, and then re-add it to the back of the queue. The interesting problem with this, however; is that I know that the FIFO property exists for queue's. So if the desired entry to be moved is at the 3rd position of 4, I'd have to remove positions 1 and 2 to finally get to 3. But I want it to keep those values still. So I assume what I'll have to do is remove each element of the queue (thank god it'll only be 5 entries max for the purpose of the project) and save it somewhere, then empty the queue and finally add the elements back in while waiting and putting the desired element to the last position.

If that's the case, I'm really curious on how I would do this. I have 4 files, 2 interfaces, the main class that contains the methods and what not for the queue, and a 4th class that'll be used for running test data and testing the methods of the program. Now, I wont add the interfaces code below because those are fine and all methods that need to be added are. I just gotta improve my moveToBack method so that it does what its supposed to. (I know I should have exceptions instead of my very poor else statements, but for this project it's not necessary.)



Any suggestions on this methods implementation?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W Wilson wrote:I know that I cant just move its position

Why not ? You are using an array to implement your queue, so it's very easy to just make a note of the value you want to move, move all the items behind it up one position in the array and then add the removed value at the end.
W Wilson wrote:I know that the FIFO property exists for queue's

But that is only in the public interface to the queue. Within the code for the queue itself you can manipuate the data any way you want.
 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. I didn't think of that actually.

On a side note, because NoDuplicatesQueue is abstract, I cannot instantiate it. What should I do? This is what I tried:



And the NoDuplicatesQueue class has to be abstract or else it wont compile due to a method in my QueueInterface.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like any other abstract class, you can fix that by creating an instance of a subclass which isn't abstract.

I don't see any such classes in what you posted but maybe you have one in your project. If not, and if you actually need to instantiate an instance of your queue, you'll need to write one.
 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Like any other abstract class, you can fix that by creating an instance of a subclass which isn't abstract.

I don't see any such classes in what you posted but maybe you have one in your project. If not, and if you actually need to instantiate an instance of your queue, you'll need to write one.


I do not have any other classes in the project besides the 2 interface classes, the abstract class, and finally the class I'm working in which is testing the program.

Edit: After looking over it a few times, I figured out my problem. It was just a very poor oversight on my part.

In other news, it compiles great but my Driver class when ran crashes with a null pointer exception. Trying to figure out why this line:



In my enqueue method causes this exception.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only reason that line could throw a NullPointerException is if items is null. It took me a second to realise (its 6am here), but you never set any value to the class member called items.

Look closely at all places where you think you are setting items. Look up a term called shadowing and it should all become clear.


Also, I spotted a few other bugs.

1) When you grow the size of your array you appear to be losing all of its contents.

2) When you move an element to the back of the queue you appear to be losing the element that is currently at the back of the queue and possibly adding a duplicate. Certainly no item currently in the queue is being moved.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!