Monday, July 27, 2009

Java problem?

I am supposed to write a program in java where a single queue is shared by multiple java programs.


I tried create a queue in super class and let sub classes access and update it.But when I run the program and check the size of queue in the sub classes it seems like the queue is initializing everytime the superclass is called in each sub class.


The code is as follow:


import java.util.*;


class Que


{public Que() {}


LinkedList%26lt;String%26gt; queue=new LinkedList%26lt;String%26gt;();


public void Qadd(String S){queue.addFirst(S);}


public void Qremove(){queue.remove();}


public ListIterator%26lt;String%26gt; QListiterator() {return queue.listIterator();}


public int Qsize(){int y=queue.size();return y;}}





class Que1 extends Que


{public static void main(String args[]){


Que q=new Que();q.Qadd("A");q.Qadd("B");q.Qadd("C"...


q.Qadd("D");q.Qremove();int size=q.Qsize();


System.out.println("size:"+size);}}





class Que2 extends Que{


public static void main(String args[])


Que q=new Que();q.Qadd("D");


int size=q.Qsize();}}

Java problem?
It would take a total re-write, but in my mind, such a scenario is a horizontal organization chart. subclassing (vertical organization chart) calls the constructor on super as the first order of business and we only have 1 chance to mod anything. The call to super has to be the first instruction.





class a


ArrayList al





class b extends a


super(al = 1,2,3,4..





--------------


// think of interface as "global variable"


// everybody gets a global





public interface QueCop {


LinkedList%26lt;Que%26gt; inProgress = new LinkedList%26lt;Que%26gt;();





public abstract class Qbase


Color a;


String b;





class Que


extends Qbase


public Que( Color c, String b)


private loadDuck( String s)





class Q1


extends Que


implements QueCop





// you get a global for free!!!!!!!!


// don't have to declare nor init it


inProgress.add( new Que(Color.red,


"bucket seats");





class Q2


extends Que


implements QueCop


inProgress.add( new Que(Color.blue,


"white walls");





which gives you one extra bonus


Que q = inProgress.get(x);


if( q instanceof Q2 )


openTheFloodGates()
Reply:Okay, are you actually calling both Que1 %26amp; Que2 while Que is alive? If you are running first one, and then the other, you will lose the queue variable from the first run, because the JVM doesn't need it anymore. I played with it, and got it to work correctly by having Que1 instantiate Que2, and doing it's thing, then printing out the results from both of them. I tweaked your code quite a bit to make it work, but here it is:





import java.util.*;


class Que


{


private static LinkedList%26lt;String%26gt; queue = new LinkedList%26lt;String%26gt;();





public Que()


{





}





public void Qadd(String S)


{


queue.addFirst(S);


}


public void Qremove()


{


queue.remove();


}


public ListIterator%26lt;String%26gt; QListiterator()


{


return queue.listIterator();


}


public int Qsize()


{


return queue.size();


}


}








class Que1 extends Que


{


Que q;


Que2 que2;





public Que1()


{


q = new Que();


}





private void doThing()


{


q.Qadd("A");


q.Qadd("B");


q.Qadd("C");


q.Qadd("D");


q.Qremove();


System.out.println("In Que1, Size: " + q.Qsize() );


que2 = new Que2();


que2.doThing();





System.out.println( "In Que1, Size: " + q.Qsize() );


}





public static void main(String args[])


{


Que1 que1 = new Que1();


que1.doThing();


}


}





class Que2 extends Que


{


Que q;





public Que2()


{


q = new Que();


}





public void doThing()


{


System.out.println("In Que2, Size: " + q.Qsize() );


q.Qadd("D");


System.out.println("In Que2, Size: " + q.Qsize() );


}





public static void main(String args[])


{


Que2 que2 = new Que2();


que2.doThing();


}


}





==============================





A last note:





Notice how when you are printing out the size, you don't need to store it as a virable first? Just grab it inside the println statement. Saves a little code, and a little time for the computer.

covent garden

No comments:

Post a Comment