# A Dual loop and methods

• December 8th, 2011, 09:40 PM
rarman
A Dual loop and methods
Hi all,
Im getting to grips with some java but having trouble with this loop. I'll explain it just so we're on the same page :)

im trying to create a loop whereby the time and velocity change each time. I have all the classes and methods necessary and compile up to this loop.

By this i mean, i have the user to ask how many data points they wish to consider (excluding zero as a data point)
i do this by
Code :

```double m = input.nextDouble(); double step = t/m;```

In normal words time is divided by the number of data points hence this is ideal in the loop to us j+=step rather than say 1, 2, 3, 4... which restricts user ability and freedom.

Now, because of the physics (don't worry - simple stuff), the velocity is going to change at each time step. I.e. initial values the user enters e.g. at t = 0, v = (1,0,0)

Again don't worry about the vector, i have solved that part in terms of print, display and so forth.

But how should i do this loop? Here was an attempt which failed:

Code :

```for(double j = 0; j < v; j+=step)/**needs two loops i.e. begins with t=0, u=v (in this case), then t = step, u = old vf, then t = 2step, u = (new)old vf ...*/ { fetch.calcVelocityf(v, g, t); v = fetch.getVelocityf(v, g, t); System.out.print(v.returnString() + "\t");   for (double i = 0; i < t; i+=step) { System.out.print(t); }   } }```

In reality terms i want it to give me the velocity and time at each step (data point, user entry see above) and have it sent to print.
I believe the time approach is fine, but unsure on the velocity, v as it is a vector hence j < v means nothing as one is double and the other Vector clas assigned.

Ps don't worry about g, that is the acceleration - also needed!

Note:
fetch is from:
Vector fetch = Vector(); [This is the class i use to make them as vectors, print as vectors via returnString etc]
velocityf is simly final velocity which in this case is changing up until we get the correct number of data points.

If you feel like i could be more informative let me know :)
• December 11th, 2011, 06:40 AM
Freaky Chris
Re: A Dual loop and methods
Welcome rarman,

Excellent first post, thank you for actually making it so!

Now if I understand your question correctly you are trying to figure out what your conditional statement for your first for loop is, you are going to require a finishing point, which unless I missed you don't seem to have defined, thise could be a t value or a velocity value.

Chris