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


ADVERTISEMENT

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

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

Why Only String Class Is Immutable

Feb 24, 2015

I would like to understand why only String class is immutable.

1. Why String class is immutable? What is the main reason for making String class as immutable.
2. Why there is no int pool or float pool or Integer pool etc, why only String pool.

View Replies View Related

Can Classes Be Immutable In Some Situations But Not Others

Feb 12, 2014

I have some complex objects I want to design. Most of the time I would like these objects to be immutable so that other classes cannot change their values. However, I also want to create an editor to create these objects and the editor will need to change the object's values. It would also be useful to be able to set the objects' fields one at a time during serialization rather than doing everything in a huge constructor.

C provides a way to make objects unchangeable by using the 'const' keyword, but Java doesn't have that. I could also wrap my objects in other accessor objects, but then I'm duplicating a lot of code. Are there any good ways to make my objects mutable only to certain other classes?

View Replies View Related

Strings Are Immutable In Java

Sep 21, 2014

strings are immutable in java...so can we apply toupper nd tolower function directly in a string

View Replies View Related

String Immutable In Java?

Jun 26, 2014

I am a java fresher. I want to know weather String is immutable in Java or not .....

View Replies View Related

Meaning Of String Or Immutable Class

Aug 26, 2014

this is my code :

String name = "Mohan";

String is a immutable class and where these character stored when we assign some character. How the logics are working inside the String class.

View Replies View Related

How To Test If Class / Object Is Immutable

Jul 2, 2014

I have make the immutable class as below, Now my question is how I can test if my this class/object is immutable

package com.learning;
import java.util.*;
import java.util.Map.Entry;
public final class ImmutableTest {
private final int id;
private final String name;
private final HashMap<String, String> hm ;

[Code]...

How I can Test If it is immutable class without looking ?

View Replies View Related

String Class Is Not Immutable - Can Reassign Another Value To Same Variable

Feb 26, 2015

public static void main(String[] args) {
String str1="Java";
str1="one";
System.out.println("str1="+str1);
}

String object is stored in a private final char array in String.java.
private final char value[];

The basic characteristic of a final variable is that it can initialize a value only once. By marking the variable value as final, the class String makes sure that it can’t be reassigned a value.

so the String objects can be initialized only once but the above code shows that str1 was initialized first with "Java", then it can be re-assigned value "one" bcos the output is one. If it can be re-initialized, basic characteristic of final variable is not satisified and hence how can we call String objects are immutable?

View Replies View Related

Create Separate String Objects As Strings Are Immutable

Jan 28, 2015

I want to clarify it whether this below code, when running this loop, will it create separate string objects as strings are immutable or else, will it keep the same reference(as each time inside loop, we use the same name 'rslt') and assign new string value for this?

while(inc < numberOfColumns){
String rslt=rs.getString(inc+1);
rowArr[inc] = rslt;
inc++;
}

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

JSP :: Check Box Implementation

Sep 30, 2014

1. In jsp 1 , i have 3 checkboxes(chkbox1,chkbox2,chkbox3) , if i select check boxes (chkbox1, chkbox2), and click on submit, corresponding two text fields (chkbox1,chkbox2) will have to appear in the next jsp i.e., jsp 2.

2. In jsp 1 , i have 3 checkboxes(chkbox1,chkbox2,chkbox3) , if i select check boxes (chkbox2, chkbox3), and click on submit, corresponding two text fields(chkbox2,chkbox3) will have to appear in the next jsp i.e., jsp 2.

Like this, which ever checkbox i select, corresponding text fields should appear in the subsequent jsp.

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

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

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 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







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