run an entire program a certain number of times via input

784 views Asked by At

So my current code effectively runs the "random walk" problem and then uses the pythagorean theorem to figure out actual distance in units walked but now I need to modify my program so that I can do a certain number of trials of said walk and then calculate the mean square distance. Not really looking for just an answer, I really also need an explanation so that I may be able to learn and recreate, I think I just need another while loop but I'm not sure where to put it.

import javax.swing.JOptionPane;
String a = JOptionPane.showInputDialog("Enter # of footsteps."); 
int z = Integer.valueOf(a);
int x= 0; // starting x position
int y= 0; // starting y position
double r;
int counterZ = 0;
if (z < counterZ ){
    System.out.println("Error");
}
while ( z > counterZ){
    r=Math.random();

    if (r<0.25){
        x=x+1;
    }

    else if(r > .25 && r<0.50){
        x=x-1;
    }
        else if(r > .5 && r<0.75){ 
        y=y+1;
    }

    else{
        y=y-1;
    }
    counterZ = counterZ + 1;
    System.out.println("(" + x + "," + y + ")");

}
System.out.println("distance = " + round(sqrt((x*x)+(y*y))));
2

There are 2 answers

0
Thirumalai Parthasarathi On BEST ANSWER

Correct me if i am wrong, My understanding is that you want to run the walk cycle a certain number of times and calculate the average distance walked on the sum of the distance of the cycles. If that is the case, then all you have to do is this,

int noc = Integer.valueOf(JOptionPane.showInputDialog("Enter # of cycles: "));
String a = JOptionPane.showInputDialog("Enter # of footsteps."); 
int z = Integer.valueOf(a);
int sum = 0;
double avg = 0.0;

for(int i=0;i<noc;i++) {
   sum+= randomWalk(z);
}
avg=(double)sum/noc;
System.out.println("the average distance walked in "+ noc + "cycles is "+avg);

the randomWalk() method should be like the following if you are calling it from the main method without creating an object for the class randomWalk() is residing in.

public static int randomWalk(int z) {
    //place your code here, starting from the `int x=0;`
    //at last instead of printing the distance walked use the following code
    return (int) Math.round(Math.sqrt((x*x)+(y*y)));
}

you have also missed to call the methods round() and sqrt() using there class Math. I have correct them for you as Math.round() and Math.sqrt(). without the class name you will get a compiler error like Symbol not found. i also assume you have imported the java.lang.Math class into your program.

3
Simon On

I'd suggest starting by indenting the code tidily so that it is more understandable.

To address your question directly, I'd suggest modifying the program so that the substance of the progam is embedded in a method (you might call it randomWalk(), perhaps) and the main() method just calls randomWalk() and does the I/O. Having done that, it would be very easy to modify the main() method to call randomWalk() many times from within a while loop.