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 April 20, 2011, 09:45:12 am



Title: how to have a loop in codes to check ?
Post by: lookang on April 20, 2011, 09:45:12 am
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?  ;D
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;

  }


Title: Re: how to have a loop in codes to check ?
Post by: Fu-Kwun Hwang on April 20, 2011, 10:01:19 pm
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?


Title: Re: how to have a loop in codes to check ?
Post by: lookang on April 21, 2011, 06:58:00 am
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


Title: Re: how to have a loop in codes to check ?
Post by: Fu-Kwun Hwang on April 21, 2011, 11:13:52 am
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.


Title: Re: how to have a loop in codes to check ?
Post by: lookang on April 21, 2011, 04:47:04 pm
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!


Title: Re: how to have a loop in codes to check ?
Post by: Fu-Kwun Hwang on April 21, 2011, 05:12:07 pm
 I just provided the hits and you can solve the problem by yourself. Very good! :D


Title: Re: how to have a loop in codes to check ?
Post by: lookang on April 21, 2011, 05:58:27 pm
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 :) u r a great teacher !

Thanks !!


Title: Re: how to have a loop in codes to check ?
Post by: csesuvra on June 02, 2011, 01:37:44 pm
Great discussion and I am also like getting feedback. Thank you.