NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
October 22, 2014, 04:27:10 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
The most important medicine is tender love and care. ..."Mother Teresa(1910-1997, Roman Catholic Missionary, 1979 Nobel Peace Prize)"
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

Pages: [1]   Go Down
  Print  
Author Topic: how to make the logic to detect 12 or 12.00 when the answer is 12.0?  (Read 5127 times)
0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member
*****
Offline Offline

Posts: 1728


http://weelookang.blogspot.com


WWW
«
Embed this message
on: November 17, 2009, 09:22:31 pm » posted from:Singapore,,Singapore

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 Smiley


* Screen shot 2009-11-17 at PM 10.01.30.png (6.88 KB, 85x33 - viewed 874 times.)

*** There are 1 more attached files. You need to login to acces it!
« Last Edit: November 17, 2009, 09:49:07 pm by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3057



WWW
«
Embed this message
Reply #1 on: November 18, 2009, 12:09:16 pm » posted from:Taipei,T'ai-pei,Taiwan

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.
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1728


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #2 on: November 18, 2009, 12:29:27 pm » posted from:Singapore,,Singapore

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?
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1728


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #3 on: November 18, 2009, 02:10:41 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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 Grin
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
« Last Edit: November 18, 2009, 02:30:31 pm by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3057



WWW
«
Embed this message
Reply #4 on: November 18, 2009, 02:30:13 pm » posted from:Taipei,T'ai-pei,Taiwan

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

 }
}
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1728


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #5 on: November 18, 2009, 03:39:53 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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

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


*** There are 2 more attached files. You need to login to acces it!
« Last Edit: November 18, 2009, 04:36:20 pm by lookang » Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1728


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #6 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!
Logged
Pages: [1]   Go Up
  Print  
The most important medicine is tender love and care. ..."Mother Teresa(1910-1997, Roman Catholic Missionary, 1979 Nobel Peace Prize)"
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
Displacement and Distance travelled logic in Easy java simulation
Questions related to EJS
lookang 7 9857 Last post October 15, 2008, 04:37:23 pm
by Fu-Kwun Hwang
is it possible to make applet make a sound when i condition is met?
Questions related to EJS
lookang 8 7577 Last post December 22, 2008, 12:10:01 pm
by lookang
how to make the vectorField2D appears as zero when the value is zero?
Questions related to EJS
lookang 3 2609 Last post April 15, 2010, 08:10:10 pm
by ahmedelshfie
how to detect a point is inside an area that is rotatable by cta ?
Questions related to EJS
lookang 2 2455 Last post April 22, 2010, 10:25:02 pm
by lookang
How to detect that the objects are just touching each other, and set the dv/dt=0
Questions related to EJS
lookang 2 2708 Last post June 29, 2010, 02:44:28 pm
by lookang
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.056 seconds with 22 queries.since 2011/06/15