NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
October 21, 2017, 08:25:56 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  
"It is not the strongest of the species that survive, but the one most responsive to change." ..."Darwin(1809-1882, English naturalist Evolution)"
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 have a loop in codes to check ?  (Read 4957 times)
0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
on: April 20, 2011, 09:45:12 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

Hi Prof!
i want to check for phase difference in multiple of integers, if the answer is
phi = 6.28
student can key in 0, 6.28, 6.28*2,6.28*3 etc until a large number

my initial codes are here


if (Math.abs(phi-2*0*Math.PI-enterphi) < 0.1||Math.abs(phi-2*1*Math.PI-enterphi) < 0.1||Math.abs(phi-2*2*Math.PI-enterphi) < 0.1||Math.abs(phi-2*3*Math.PI-enterphi) < 0.1)
{

phicorrect = " Yes";
phicorrectbackground = java.awt.Color.GREEN;
}
else
{
phicorrect = " No";
phicorrectbackground = java.awt.Color.RED;

}

My question is how to do a loop?

i try
for (int counter=0; counter<100 /* Iterations */ ; counter++) {
  if (Math.abs(phi-2*counter*Math.PI-enterphi) < 0.1) {
   phicorrect = " Yes";
   phicorrectbackground = java.awt.Color.GREEN;
  }
  else {
   phicorrect = " No";
   phicorrectbackground = java.awt.Color.RED;

  }

but it did not check the phi is multiple of counter to accept answer as correct.
how to make the codes elegant? Grin
thanks!

quite silly to write
  if (Math.abs(phi-2*0*Math.PI-enterphi) < 0.1||Math.abs(phi-2*1*Math.PI-enterphi) < 0.1||Math.abs(phi-2*2*Math.PI-enterphi) < 0.1||Math.abs(phi-2*3*Math.PI-enterphi) < 0.1||Math.abs(phi+2*1*Math.PI-enterphi) < 0.1||Math.abs(phi+2*2*Math.PI-enterphi) < 0.1||Math.abs(phi+2*3*Math.PI-enterphi) < 0.1) { // valid only for 3 rounds left and right
  phicorrect = "  Yes";
  phicorrectbackground = java.awt.Color.GREEN;
  }
  else {
  phicorrect = "  No";
  phicorrectbackground = java.awt.Color.RED;

  }
« Last Edit: April 20, 2011, 10:07:31 am by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #1 on: April 20, 2011, 10:01:19 pm » posted from:Taipei,T'ai-pei,Taiwan

I do not really undestand what you really need!
Do you mean student can key in 0, 6.28, 12.56,18.84,... etc
 But it is incorrect if the input value is one of 6.27, 6.29,12.55,12.57, 11.0,... etc?
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #2 on: April 21, 2011, 06:58:00 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

Yes.
Phase lead is correct for phi+ 2.counter.pi where counter is integer n zero
http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=2178msg8065;topicseen#msg8065
Has my implementation phi
« Last Edit: April 21, 2011, 07:00:54 am by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #3 on: April 21, 2011, 11:13:52 am » posted from:Taipei,T'ai-pei,Taiwan

if v is the input value(double), define pi2=2*Math.PI

then v%pi2 will give you remainder.

i.e.
3.2 % 2.0 will give you 1.2
6.5 % 2.0 will give you 1.5
6.29 % 6.28 will give you 0.01
6.27 % 6.28 will give you 6.27
12.57 % 6.28 will give you 0.01

If the remainder is less than 0.01 or larger than 6.27 will be good value for your case.
You do not need to use loop.

I hope this will help.
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #4 on: April 21, 2011, 04:47:04 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

fantastic tip prof!

if (Math.abs(phi%(2*Math.PI)-enterphi%(2*Math.PI)) < 0.1){
 phicorrect = " Yes";
   phicorrectbackground = java.awt.Color.GREEN;
  }
  else {
   phicorrect = " No";
   phicorrectbackground = java.awt.Color.RED;

  }

it is working very well http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=2178msg8065;topicseen#msg8065

thanks!
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #5 on: April 21, 2011, 05:12:07 pm » posted from:Taipei,T'ai-pei,Taiwan

 I just provided the hits and you can solve the problem by yourself. Very good! Cheesy
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #6 on: April 21, 2011, 05:58:27 pm » posted from:Singapore,,Singapore

U r the master of skillful facilitation and simulation making.
At lot of people in the world are benefiting from your wisdom and work like me.
Hope to be like u some day Smiley u r a great teacher !

Thanks !!
« Last Edit: April 21, 2011, 06:00:02 pm by lookang » Logged
csesuvra
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #7 on: June 02, 2011, 01:37:44 pm » posted from:Dhaka,Dhaka,Bangladesh

Great discussion and I am also like getting feedback. Thank you.
Logged

Suvra Cse
Pages: [1]   Go Up
  Print  
"It is not the strongest of the species that survive, but the one most responsive to change." ..."Darwin(1809-1882, English naturalist Evolution)"
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
Current Loop in magnetic field (How motor works!)
Electromagnetism
Fu-Kwun Hwang 11 41845 Last post April 16, 2011, 09:19:33 am
by Fu-Kwun Hwang
Things to check for forum administrators:
How to prevent spam in forums
Fu-Kwun Hwang 0 4696 Last post February 28, 2009, 08:46:21 pm
by Fu-Kwun Hwang
what is the voltage across the split ring commutator when the loop is rotating?
Question related to Physics or physics related simulation
lookang 4 12519 Last post March 05, 2010, 09:09:20 pm
by HY
how make loop for array for y coordinates?
Questions related to EJS
lookang 14 12267 Last post May 14, 2010, 11:22:41 pm
by Fu-Kwun Hwang
Current Loop in magnetic field (How motor works!)
electromagnetism
ahmedelshfie 0 4001 Last post June 08, 2010, 01:40:56 am
by ahmedelshfie
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 5.07 seconds with 23 queries.since 2011/06/15