|
|
I have been using java2s for all my questions on this problem.
Updated Code: (Use the original posted code for classes not posted here)
public class CData implements Observer
{
public CData()
{
}
@Override
public void update(Observable arg0, Object arg1) {
if (itsAction != null)
{
try {
this.itsAction.doAction();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private CAction itsAction = null;
public CAction getItsAction() {
return itsAction;
}
public void setItsAction(CAction itsAction) {
this.itsAction = itsAction;
}
}
public class CValue extends Observable
{
private double itsValue = 0;
public double getItsValue() {
return itsValue;
}
public void setItsValue(double itsValue) {
if (this.itsValue != itsValue)
{
this.itsValue = itsValue;
System.out.print("Value set to " + itsValue + "\n");
setChanged();
notifyObservers();
}
}
public void startBehavior()
{
setChanged();
notifyObservers();
}
}
Observation: When I execute setChanged and then execute notifyObservers, the test program does a re-calculation on the new data. If the re-calculation doesn't change the value, then no new notifications are required. If there is a change in data from the re-calculation then the process is started all over again. I made a small modification to CMain to stop the notifications after a period of time and restart the re-calculation process again. This is eliminating the memory leak completely.
From these new revelations, attached is my new CMain code
public class CMain {
private final static int TIMEOUT = 5;
private static CValue Value;
private static CData AddFive;
private static CData AddTwo;
private static CData AddTen;
private static CData AddSeven;
private static CData MaxValueReached;
public static void main(String[] args) {
Value = new CValue();
AddFive = new CData();
AddFive.setItsAction(new CAction()
{
@Override
public void doAction() throws InterruptedException {
Thread.sleep(TIMEOUT);
if (Value.getItsValue() <= 20)
{
Value.setItsValue(Value.getItsValue() + 5);
}
}
});
Value.addObserver(AddFive);
AddTwo = new CData();
AddTwo.setItsAction(new CAction()
{
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 20 && Value.getItsValue() <= 40)
{
Value.setItsValue(Value.getItsValue() + 2);
}
}
});
Value.addObserver(AddTwo);
AddTen = new CData();
AddTen.setItsAction(new CAction(){
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 40 && Value.getItsValue() <= 80)
{
Value.setItsValue(Value.getItsValue() + 10);
}
}
});
Value.addObserver(AddTen);
AddSeven = new CData();
AddSeven.setItsAction(new CAction(){
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 80 && Value.getItsValue() <= 100)
{
Value.setItsValue(Value.getItsValue() + 7);
}
}
});
Value.addObserver(AddSeven);
MaxValueReached = new CData();
MaxValueReached.setItsAction(new CAction()
{
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() >= 100)
{
System.out.print("Maximum Value Reached\n");
}
}
});
Value.addObserver(MaxValueReached);
Value.startBehavior();
while (true)
{
if (Value.getItsValue() >= 100)
{
Value.setItsValue(0);
}
}
}
}
Again, thanks for the feedback
|
|
|
|
|
few things that you have to know. Role of the Observer is to notify each subscriber if any changes happen.
If you see in your code there is a recursive call exist. In your main class , at the end, you are setting value in Observer. So Observer value changed and it convey the notify message to the subscriber.Again If you see in doAction() ,you can see that each Subscriber receive the notify message and again set the Observer value with new one. There for this action again update the Observer value and again Observer doing the above thing and thus fall in recursive call.
Regards
Shubhashish
|
|
|
|
|
Where "Observer" is actually "Observable" as you got it right in your first post, since "Observer" and "Subscriber" are basically the same thing.
|
|
|
|
|
Hi,
I want to implement SVM in Java. I have implemented svm in Matlab before but I am new to Java and I found a library LIBSVM which I can use. Is there any other library that I could use?
Thanks
|
|
|
|
|
Presumably what you want is to USE it in your code rather than "implement" (code yourself).
In that case google is the place to start.
|
|
|
|
|
What are the basic deferences between Get & Set methods in Java?
|
|
|
|
|
this sounds like homework questions to me
MCAD
---
|
|
|
|
|
The same as they are in any language, and a read of the documantation or a search[^] would easily find the answer for you.
Veni, vidi, abiit domum
|
|
|
|
|
In Java, one typically tries to avoid having all data structures set as public.
So if it is set to private, getters are a way to access that data value, as the variable may be private, but the getter is public.
Same with setters, if you are trying to modify the value of the variable through another class, and if the variable is private to one class, then a public setter method can modify that value effectively.
|
|
|
|
|
Hey guys,
I'm trying to learn java and I've got stuck on this problem.
I made a program with GUI that when I input on a text field and press enter it will display the value that was entered on a separate JOptionPane dialog box.
I'm trying to combine 2 inputs before displaying them but I cant find the code that will do the trick, here is the code I think I need to change:
if (eventHand.getSource()==item1)
string = String.format("Field 1: %s",eventHand.getActionCommand());
if (eventHand.getSource()==item2)
string = String.format("Field 2: %s",eventHand.getActionCommand());
JOptionPane.showMessageDialog(null,"" +string);
the value is stored on the variable named string, I'm looking for a way store the 2 value on each variable first then display it on the same messagebox.
Thanks,
|
|
|
|
|
Something like:
if (eventHand.getSource()==item1) {
string.concat("Field 1: ");
string.concat(eventHand.getActionCommand());
string.concat(", Field 2: ");
string.concat(item2.Text);
}
if (eventHand.getSource()==item2) {
string.concat("Field 1: ");
string.concat(item1.Text);
string.concat(", Field 2: ");
string.concat(eventHand.getActionCommand());
}
JOptionPane.showMessageDialog(null, string);
Veni, vidi, abiit domum
|
|
|
|
|
Hi,
"Text" part of this (item2.Text); is not working but I think I can handle this already.
also adding a dedicated button also helps me to think it in an organized way.
Thanks,
|
|
|
|
|
KatsuneShinsengumi wrote: "Text" part of this (item2.Text); Since I did not know what type of object item2 was, I had to make a guess.
Veni, vidi, abiit domum
|
|
|
|
|
Yeah thanks, no problem there,.
|
|
|
|
|
1) GenK-tree algorithm,
2) algorithm for inclusion-exclusion principle.
how to implement these two algorithms using JGraphT (java language).
plz help me...
|
|
|
|
|
|
hi,
how can i generate or use neper number in java.
i want to write a function that do uniform distribution.
thanks,
|
|
|
|
|
|
thanks,
|
|
|
|
|
|
Hi Expert,
Can any 1 Help me how to develop the given algorithm in Java using JgraphT
1) GenK-tree algorithm
2) inclusion-exclusion algorithm
I need help regarding develop these above algorithm in Java with code and any tutorial or any same project or algorithm in Java so i can help from then and complete the above task
For reference follow the below link
1) http://jgrapht.org/[^]
2) http://jgrapht.org/visualizations.html[^]
Please help me to develop the application like above reference in Java
Thanks & Regards,
Mithun
mit_pnch86@yahoo.in
|
|
|
|
|
|
Sorry...
but this is my first post in CodeProject but next time i will be careful with your Forum.....
Thanks,
Mithun
|
|
|
|
|
i want to save a message structure and it's score to a data structure. but some of data structures are repetitious.i want to use two dimensional collection with duplicate key which collection can help me?
thanks,
|
|
|
|