Changing Item Positions In A Queue

Nov 3, 2014

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 (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.)

public abstract class NoDuplicatesQueueWilson<T> implements NoDuplicatesQueueInterfaceWilson<T>
{
private int MAX_QUEUE = 5; // Default array size, small for testing purposes
private T[] items; // The array of the queue.
private int front; // The first entered item of a queue.
private int back; // The last entered item of a queue.
private int count; // A counter.

[Code] ....

View Replies


ADVERTISEMENT

Swing/AWT/SWT :: Changing Color Of JList Item

Oct 23, 2014

is there a good way to change the color of a JList item that isn't selected? I'm throwing my hand at building a client/server chat app and on the client side, I need to highlight the name of the person (which is in a JList) who has sent a message to the client GUI.

View Replies View Related

Encrypt Strings Present In Array To Alphabet After 2 Positions

Mar 13, 2014

I have to encrypt the strings present in an array to the alphabet after 2 positions

Example:

my name is x

should give an output

oa pcog ku z

Although i have taken an input but unable to increment the char in the array...

View Replies View Related

JavaFX 2.0 :: How To Set Divider Positions For SplitPane After Window Resizing

Sep 30, 2014

I would like to keep the same values of the positions for dividers (SplitPane) even if the width or the height of the window changes. I want to keep the proportion of the differents dividers. After resizing the window, the values of positions are not the sames ! I use this source without success :

  stage.widthProperty().addListener(new ChangeListener<Number>() {
  @Override public void changed( ObservableValue<? extends Number> observableValue, Number number, Number number2) {
               sp.setDividerPositions(0.40f, 0.02f, 0.54f);

[Code] ....

View Replies View Related

JavaFX 2.0 :: Cannot Set Divider Positions In Multiple Splitpanes At One Button Click

May 29, 2014

I have a problem with setting multiple splitpane divider positions. In my application there are multiple splitpanes (one inheriting the other). When resizing the scene (e.g. by double-click or mouse drag) the divider position default values (as set in the FXML) are not taken into account and the application therefore incorrectly displays the divider positions. For example, one pane that should be only 100px wide now spans over the half of the application. Its not very nice because one has to readjust the divider positions manually each time after resizing the window. At the same time, I dont want to set maximum values to keep the layout flexible.
 
I tried to solve this by adjusting the divider positions at a button click event (..setOnAction(new EventHandler<ActionEvent>()...) but when I click it only one splitpane at a time is adjusted. As I have 3 splitpanes I have to click the button 3 times to get all the adjustments. I am not sure whether this is because the click event is consumed after the first adjustment and subsequent repaint of the scene?
 
A sample code of what I have now would look like this:
 
adjustButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent e){
     pane1.setDividerPositions(0.9);
     pane2.setDividerPositions(0.8);
     pane3.setDividerPositions(0,075);
      }
});

View Replies View Related

Ball Object Which Implements Runnable Interface And Traces Various Positions Of A Ball

Feb 11, 2014

a) I have a Ball Object which implements the Runnable interface and traces the various positions of a ball.

b) I then have a Ball_Bounce JPanel inside a JFrame which creates two instances of the Ball object and then paints them to the JPanel.

As per my understanding, when the main() program in Ball_Bounce.java is started, there a total of three threads running in this program, one for each ball and one for the main(). What I cannot understand is whenever the balls collide, I end up getting the "Collision" message twice even though the collision is checked only in the main() thread.

[#]public class Ball implements Runnable
{
private boolean xUp, yUp, xUp1, yUp1;
private int x, y, xDx, yDy;
private final int MAX_X = 500, MAX_Y = 500;
private boolean flag = true;
private static Thread ball;
 
[code]...

View Replies View Related

EJB / EE :: Same Listener MDB For Different Jms Queue?

Feb 21, 2014

Is it possible to define the same message driven bean as a listener to different queues?

The goal being to define different redelivery configuration for different kind of messages, but handle them all through a single deployment MDB for unity/entity purposes.

The other option is to have many MDBs targeted to the same source code.

View Replies View Related

Delete From Queue Not Working

May 29, 2014

leaveQ method does not work..To see the other files related to these code click here:(Its a dropbox location) URL....Java Code:

public class CustomerQ {
private int MaxLength;
private int totalCustomers;//assuming #of customers served
int Qlength;
Customer cus;
LinkedList4Q cus4Q;

[code]....

View Replies View Related

Implement A Priority Queue

Apr 15, 2014

Implement a priority queue based on a sorted linked list. The remove operation on the priority queue should remove the item with the smallest key.

View Replies View Related

Bank Queue Simulator

Jul 13, 2014

I am doing a bank queue simulator program which will figure what will happen in 100 minute where 0 to 2 customers comes every minute. 3 counters will handle these customers each counter will poll the customer after 3 minutes.my problem is queue.poll()is not working in counter method and it is adding null values to the queue in the same method. when i add while customers.isEmpty(); the program will not work i do not know why

package dataalgo;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
public class customer {

[code]....

View Replies View Related

Double Ended Queue

Jan 7, 2015

implement Double Ended Queue?????

import java.util.*;
public class DoubleEndedQueueImplHW22 {
ArrayList<Integer> deque = new ArrayList<Integer>();
public void insertFront(int a){
System.out.println("adding at front: "+a);
deque.add(0,a);
System.out.println(deque);

[code]....

View Replies View Related

Implementation Of Immutable Queue

Nov 12, 2014

The following codes shows an implementation of an enqueue function of a FIFO immutable queue, and the output result shows the String "c".

I don't understand why since I expected it should be null.

The head and the tail of an ImmutableQueue Object are two separate queue, and each time I call the enqueue function, it just return a new object with a new tail, however, the head is not modified except the first two times I call the function.

Therefore, I expected head.next.next should be a null element, but the result is not like that.

public class ImmutableQueue<E> {
private int size =0;
public Queue<E> head;
public Queue<E> tail;
public ImmutableQueue(){}
private ImmutableQueue(Queue<E> hd, Queue<E> tl){
head=hd;
tail=tl;

[Code] ....

View Replies View Related

Print Queue Using Priority Q Simulation?

Nov 28, 2014

I have a class "ExecuteJob" which has Print Q in the form of Priority Q.

You can keep adding job to the Q by calling one of the method in the class. However, and object cant do things simultaneity can it? While im adding a new job to the print queue, can it be executing and existing job in the print Q.

To achieve that, I would need to implement process and threads? I believe am I right? So that adding a job is independent to being removed?

View Replies View Related

Finding Palindromes From Stack And Queue?

Apr 30, 2015

I'm trying to create a class that takes an String from a Stack and checking if it's a palindrome than taking a another String from a queue and checking if that is also a palindrome.

import java.util.Stack;
public class Palindrome {
 public static void main(String[] args) {
// TODO Auto-generated method stub
 String enteredLine;
int leftStack, rightStack;
int leftQueue, rightQueue; 
PalinedromeArray stack1 = new PalinedromeArray();

[code]....

View Replies View Related

Processing Queue Containing Dissimilar Messages

May 25, 2014

I am new to Java/OOP in general, and am trying to implement a multi-threaded system that contains a master thread, and a set of worker threads that are heterogeneous in the work they do. Once they complete the work, the workers indicate to the master by posting the result on to its queue. Here is the problem. The results of each type of work is different, and the master has to process each differently. In C (which I'm familiar with), this can be achieved by having a message type that is a union of all the expected messages, and by using a switch statement.

I thought of doing something similar in Java, by using instance of on each incoming message (each individual message class having been subclassed from a super message class) , and doing switch on that, but it doesn't seem to be the OO way to do things. The only other way I could think of was to implement an abstract method to get the type of each message, and then use the type in a switch statement, or if-then-else. Is there some other Java idiom to do this kind of processing? Also, if this is an acceptable method, why is it superior to using the reflection to find out the message type (instead of using the abstract getType())?

The message types look similar to the code below:

abstract class Message {
abstract String getType();
} class Result1 extends Message {
ResultType1 content;
String getType() {

[Code] ....

View Replies View Related

Priority Queue Implementation Using Heap / BST

Dec 4, 2014

I am in the process of implementing Priority queue, as I understand that there are many data structures you could use to implement. I implemented it with the an array, which it works absolutely fine. However I have limitations on what collections I can use from the collections classes. I fact I cant use any of the collections classes. Meaning I cant use array.

I’m trying to implement Priority Queue using heap. And implementing heap using binary trees. But however I have a few questions which I need to clarify and I cant think of any other way of resolving it. Ofcourse I can implement my own simple array class using linked list.

Inserting into heap would be quite simple, as I just need to find the right last position from left to right leaf to insert the node into the tree. However after inserting, you may want to make sure that leaf node values are > than root node. Therefore, the root node will always be with the highest priority.

I call these steps where you compare from top down as bubbledown and bubbleup. To do this I really need a for each node within the treee node to have attribute pointing to its root node. So in case of bubbleup I always have a pointer for a given node to its root, without it would mean I would to traverse through the entire tree to identify its root. Which I believe is very inefficient.

Or have I taken this completely wrong? Or is it the case that heap are only best with arrays and therefore use array (by implement it using linked list?)

View Replies View Related

Implementing A Thread-safe Queue

Jul 10, 2014

I have situation where a user can request java server to send a value to an embedded device, and if the device is asleep, that value needs to be stored in a queue until the device wakes up and sends a position to java server, at which point the server checks if there is a value in the queue and if there is, it then sends that value to the device. The maximum size of the queue is 1 for now. And if the user makes continuous requests to java server, the old value is removed and the new value is added.

Initially I was looking into BlockingQueue, but the problem with that is, well, it blocks. queue.put(value) will block if queue is full, and queue.take() will block if queue is empty. I can't have something that blocks. When the device responds to server, server checks if value is in queue, if it is not then the server carries on the rest of its responsibility. Thus, I then entertained ConcurrentLinkedQueue. While queue.offer(value) and queue.poll(); allow you to add and remove values respectively from the queue without blocking, it does not allow you to set a maximum size limit of the queue. My queue must have a maximum size limit and it has to always be the newest value the user submits to the queue (where old values are removed).So this is what I came up with:

Java Code: class Unit {
private List<Integer> cmdQueue;

public Unit(){
cmdQueue = Collections.synchronizedList(new LinkedList<Integer>());

[code]....

I use Collections.synchronizedList, as I do here, do I still need to use synchronize as I did above?

View Replies View Related

Can't Get GUI Program To Print Out Queue List

Apr 11, 2014

I have been working on this Java Gui program and i cant get it to print to the textbox correctly.i originally had it displayed in a dialog window but it would print one integer a time in a seperate window.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
import java.io.*;

[code]....

View Replies View Related

FIFO - Implementation Of Immutable Queue

Nov 12, 2014

The following codes shows an implementation of an enqueue function of a FIFO immutable queue, and the output result shows the String "c". I don't understand why since I expected it should be null.

The head and the tail of an ImmutableQueue Object are two separate queue, and each time I call the enqueue function, it just return a new object with a new tail, however, the head is not modified except the first two times I call the function.

Therefore, I expected head.next.next should be a null element, but the result is not like that.

public class ImmutableQueue<E> {
public Queue<E> head;
public Queue<E> tail;
public ImmutableQueue(){}
private ImmutableQueue(Queue<E> hd, Queue<E> tl){
head=hd;
tail=tl;

[Code] ......

View Replies View Related

Queue Class And Java LinkedList

Sep 30, 2014

I have a Queue class (containing a LinkedList plus a few other variables and stats for my project), which works fine with the standard LinkedList, but I'm trying to add my own code for MyLinkedList.

However, I keep getting a NullPointerException at my remove method.

public class MyLinkedList<T> {
Node head;
public MyLinkedList() {
head = null;
}
public class Node {
T contents;
Node nextNode;

[Code] ......

View Replies View Related

Sorting Priority Queue Containing Objects

Mar 30, 2014

I have a PriorityQueue called incoming, containing objects of type Vehicle.

All vehicles have a fuel level, that can be accessed by the getter method getFuelLevel().

I want to be able to choose to sort the queue so that the Vehicles with the lowest fuel levels are at the front of the queue.

View Replies View Related

Finding Road With Stack Or Queue

May 2, 2014

I have to make code about finding road with Stack or Queue. At first, I tried to make it in this way...

Java Code:

//public class QueueMain {
public static void main(String[] args){
int [][] arr = {{0, 0, 1, 0, 0, 1, 0, 0, 0}, //p
{0, 0, 0, 0, 0, 0, 1, 0, 0},//q
{0, 0, 0, 0, 0, 0, 1, 0, 0},//r
{0, 0, 0, 0, 1, 0, 0, 0, 0},//s
{0, 0, 0, 0, 0, 1, 0, 0, 0},//t
{0, 0, 0, 1, 0, 0, 0, 1, 0},//w
{0, 0, 0, 0, 0, 0, 0, 0, 0},//x
{0, 0, 1, 0, 0, 0, 0, 0, 1},//y
{0, 0, 0, 0, 0, 0, 0, 0, 0}};//z
String [] add = {"P", "Q", "R", "S", "T", "W", "X", "Y", "Z"};
int originnumber = 0;
int destinationnumber = 8;

[Code] ....

But it doesn't work at all. I tried to put visited address into array<mark>... but it didn't work.

View Replies View Related

Enterprise JavaBeans :: First In First Out Job Queue Processing

Mar 3, 2013

I am looking for the ability, on the server side, to run programs or "jobs" in a job queue, where the jobs are processed as first in first out. If you are familiar with the IBM iSeries, they have a built in job queue mechanism which accomplishes what I am looking for. The primary purposes for this would be to process and update large amounts of data in a thread safe environment.

View Replies View Related

Calling A Method - Return Smallest Value In A Queue

Nov 15, 2014

I've created a getMin method to return the smallest value in a queue. However, I'm having trouble calling the method in my main.

/**
* Main method.
*
* @param args
* the command line arguments; unused here
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
Queue<Integer> testQueue = new Queue1L<Integer>();

[Code] .....

View Replies View Related

Array Blocking Queue And Inundation Of Data

Jul 24, 2014

I set the limit to 2 so the producer blocks after two insertions until an item is consumed. Because we are dealing with a basic counter that increments, we do not lose any data. Since after queue is freed up we simply increment by 1, so we are able to store all numbers. However, in real world situation, you might be storing data coming from redis server into the queue. And redis server may be publishing a plethora of data. So when the queue blocks, and new data comes in, what happens? Is the currently blocked data lost forever and the new data that came in is now blocked, or is the new data that came in ignored and the old data remains being blocked until queue is freed up?

Java Code:

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueBlocking {
private BlockingQueue<Integer> sQueue = new ArrayBlockingQueue<Integer>(2);
int counter=0;
private void test() throws InterruptedException{

[code]...

View Replies View Related

Writing Method Symmetric Using Stack And Queue

Apr 19, 2015

The question is write to a method symmetric that accepts a stack of integers as a parameter and replaces the stack contents with itself plus a symmetrical version of itself (the same elements in the opposite order).

For example, suppose a variable s stores the following elements:
bottom [10, 50, 19, 54, 30, 67] top

After a call of symmetric(s),the stack would store the following elements
bottom [10, 50, 19, 54, 30, 67, 67, 30, 54, 19, 50, 10] top

Note that the symmetric version is added on to the top of what was originally in the stack. The bottom half of the stack contains the original numbers in the same order.

If your method is passed an empty stack, the result should be an empty stack.
If your method is passed a null stack, your method should throw an IllegalArgumentException.

a) Write the method symmetric using one temporary stack and one temporary queue.
/> Re-write the method using only one temporary Queue.

What I have done so far is

public static Stack symmetric(Stack s1){
Stack s2 =new Stack();
int theTop=0;
if(s1.isEmpty()){
return s1;

[Code] .....

Its not working.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved