NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on November 17, 2009, 09:22:31 pm



Title: how to make the logic to detect 12 or 12.00 when the answer is 12.0?
Post by: lookang on November 17, 2009, 09:22:31 pm
i only recently noticed the wonderful input field you have designed here http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=52.0
I admired your applet's codes to differentiate the " Check the number of digits again "


i been thinking how to detect the logic of 12.0 as different from 12 or 12.00 or 12.00000?

any tips ?

it is a wonderful feature, if you intend to add it the Ejs version, alert me, i go see how u do it!  http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=567.0

i am making a super applet here http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=684.0
Ejs open source Vernier calipers java applet with objects and zero error logic is yours too :)


Title: Re: how to make the logic to detect 12 or 12.00 when the answer is 12.0?
Post by: Fu-Kwun Hwang on November 18, 2009, 12:09:16 pm
If the input is a number field, then you would not be able to detect the difference between 12.0 and 12

However, if it is a string field, then you can check the format of the input.
You also need to transform the string into a number in order to check for it's value.


Title: Re: how to make the logic to detect 12 or 12.00 when the answer is 12.0?
Post by: lookang on November 18, 2009, 12:29:27 pm
oic...

does http://java.sun.com/javase/6/docs/api/java/lang/Double.html helps?



i will change to text field

compare string with string?




some references that might be useful

use the function
   valueOf(String s)
          Returns a Double object holding the double value represented by the argument string s.




toString

public static String toString(double d)

helps?


Title: Re: how to make the logic to detect 12 or 12.00 when the answer is 12.0?
Post by: lookang on November 18, 2009, 02:10:41 pm
i create a custom function

public double ConvertStringToDouble(String aString)
{

    out = Double.parseDouble(aString);
return out ;
  }

but the out is not showing the Double from the String.

I modified the codes from here thinking i only need Double.parseDouble.
I copy and paste everything into custom function but it does not work ;D
I farmed the internet already, it is close but not working yet. any idea how to ConvertStringToDouble ?

reference:
http://www.java2s.com/Code/Java/Language-Basics/Convertstringtodouble.htm


Title: Re: how to make the logic to detect 12 or 12.00 when the answer is 12.0?
Post by: Fu-Kwun Hwang on November 18, 2009, 02:30:13 pm
If you want to check out if user has enter correct value, you should have a correct answer already.
For example: If the correct answer is 12.0 and the user input is uvalue (which is a String)
You need to define a string with correct value: ans="12.0"

if(ans.compareTo(uvalue)==0){// exactly the same

}else {// different string but might be correct value
 double a,b;
 a=new Double(ans).doubleValue(); //or  a= Double.parseDouble(ans);
 b=new Double(uvalue).doubleValue(); // or  b=Double.parseDouble(uvalue);
 if(Math.abs(a-b)<1.e-6){// same value but different digits or incorrect format

 }else {// different value

 }
}


Title: Re: how to make the logic to detect 12 or 12.00 when the answer is 12.0?
Post by: lookang on November 18, 2009, 03:39:53 pm
Thanks for the help !!


 b=new Double(uvalue).doubleValue(); // or  b=Double.parseDouble(uvalue);
 

I am experiencing problem getting this code to work.

i modified into
enterK1f= new Double(l_enterK1f).doubleValue(); // no value is assigned to enterK1f
enterK1f=Double.parseDouble(l_enterK1f); // also no value is assigned to enterK1f

// when this above line(s) is/are active, the applet cannot move? , commented lined then applet can move. ???

l_enterK1f is the text input field and it is working !.
but strangely no value is assigned to enterK1f from the string  l_enterK1f.

Is the code correct?  b=new Double(uvalue).doubleValue(); // or b=Double.parseDouble(uvalue);

the rest is actually implemented nicely i think, need to test again after getting   enterK1f to show the value.


my fixed relations is now

// enterK1f= new Double(l_enterK1f).doubleValue();
// enterK1f=Double.parseDouble(l_enterK1f);
// enterK1f= valueOf(l_enterK1f);

if(l_answer.compareTo(l_enterK1f)==0){// exactly the same
K1fcorrect = "Great! You have the right answer!";
K1fcorrectbackground = java.awt.Color.GREEN;
}
else {// different string but might be correct value
// double a,b;
// a=new Double(l_answer).doubleValue(); //or  a= Double.parseDouble(ans);
 // or  b=Double.parseDouble(uvalue);
 if(Math.abs(answer-enterK1f)<1.e-6){// same value but different digits or incorrect format
K1fcorrect = " Check the number of digits ";
K1fcorrectbackground = java.awt.Color.RED;
 }
 
 else {// different value
//double c;
// c=new Double(l_enterK1f).doubleValue();
K1fcorrect = " Incorrect, Your input is out by "+ (answer - enterK1f) ;
K1fcorrectbackground = java.awt.Color.RED;
 }
}


Title: Re: how to make the logic to detect 12 or 12.00 when the answer is 12.0?
Post by: lookang on November 18, 2009, 07:40:31 pm
i think i fixed the bug

i added on the input text field of the l_enterK1f

action

enterK1f= Double.parseDouble(l_enterK1f);

and it is working now!

thanks!

will ask again if need. Have a great day Prof Hwang!