Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the JAVA code for this? A random walk is a process in which an object moves randomly in a space. In the case

image text in transcribedimage text in transcribedWhat is the JAVA code for this? A random walk is a process in which an object moves randomly in a space. In the case of one-dimension random walks, the object initially at the coordinate 0 and at each move, changes the coordinate by either adding 1 or subtracting 1. In the two-dimensional case, the initial coordinate is (0,0) and the coordinate change occurs independently on the two values. We will write two methods for the simulations, oneDimensional and twoDimensional. Both are void methods and receive an int named r as a parameter, where we will ensure that the actual value of r is strictly positive. In the one-dimensional case, the program uses four variables, xPos, xMax, xMin, and zeroCount, to monitor the progress of the simulation. The first, xPos, is for the present coordinate value. The second,xMax, is the largest coordinate value that the object reaches during the simulation. The third, xMin, is the smallest coordinate value that the object reaches during the simulation. The last, zeroCount, is the number of times that the coordinate goes back to 0. All the four variables should have the initial values of 0. At each round, our program generates a random double with a call to Math.random() and depending on whether the value is greater than or equal to 0.5, it either adds 1 to xPos or subtracts 1 from xPos. After the update, the program updates the remaining three variables, and reports the value with one character space and the new value of xPos, but with no newline. This means that the output during the simulation will appear in just one line. After completing the required number of rounds, the program prints the newline to go to the next line, and then reports the values of the three variables. In the case of two-dimensional simulations, the program uses two variables, xPos and yPos, to record the coordinates. The program also uses zeroCount to count the returns to (0,0). It also uses a pair of variables, xMax and yMax, to maintain the farthest point that the object reaches during the simulation. The distance measure the program uses is the Manhattan distance from (0,0), which is the sum of the absolute value of the x-coordinate and the absolute value of the y-coordinate.The main method uses one for-loop that iterates a variable i from 1 to one million. In the loop-body, the program asks for the number of rounds from the user. If the value is 0, the program terminates the loop using break; if the value is negative, the program skips the round using continue; otherwise, the program makes a call to each of the two simulation methods with thevalue the user has entered as the actual parameter

The goal of this lab is to write a program for simulating random walks in one dimension and in two dimensions. A random walk is a process in which an object moves randomly in a space. In the case of one- dimension random walks, the object initially at the coordinate 0 and at each move, changes the coordinate by either adding 1 or subtracting 1. In the two-dimensional case, the initial coordinate is (0,0) and the coordinate change occurs independently on the two values. We will write two methods for the simulations, oneDimensional and twoDimensional. Both are void methods and receive an int named r as a parameter, where we will ensure that the actual value of r is strictly positive. In the one-dimensional case, the program uses four variables, xPos, xMax, xMin, and zeroCount, to monitor the progress of the simulation. The first, xPos, is for the present coordinate value. The second, xMax, is the largest coordinate value that the object reaches during the simulation. The third, xMin, is the smallest coordinate value that the object reaches during the simulation. The last, zeroCount, is the number of times that the coordinate goes back to 0. All the four variables should have the initial values of 0. At each round, our program generates a random double with a call to Math.random() and depending on whether the value is greater than or equal to 0.5, it either adds 1 to xPos or subtracts 1 from xPos. After the update, the program updates the remaining three variables, and reports the value with one character space and the new value of xPos, but with no newline. This means that the output during the simulation will appear in just one line. After completing the required number of rounds, the program prints the newline to go to the next line, and then reports the values of the three variables. In the case of two-dimensional simulations, the program uses two variables, xPos and yPos, to record the coordinates. The program also uses zeroCount to count the returns to (0,0). It also uses a pair of variables, xMax and yMax, to maintain the farthest point that the object reaches during the simulation. The distance measure the program uses is the Manhattan distance from (0,0), which is the sum of the absolute value of the x-coordinate and the absolute value of the y-coordinate. The main method uses one for-loop that iterates a variable i from 1 to one million. In the loop-body, the program asks for the number of rounds from the user. If the value is 0, the program terminates the loop using break; if the value is negative, the program skips the round using continue; otherwise, the program makes a call to each of the two simulation methods with the value the user has entered as the actual parameter. Below is a sample execution of the program, where the characters in blue represent the user input. 1 2 ---Enter # of rounds (0 to stop): 5 3 One dimensional random walk 4. -1 -2 -3 -4 -3 5 The maximum is 0. 6 The minimum is -4. 7 # of returns to 0 is 0. 8 Two dimensional random walk 9 (1,-1) (2.0) (1,1) (2, 2) (1,1) 10 The farthest point is (2,2). 11 # of returns to 0 is 0. 12 13 -----Enter # of rounds (0 to stop): 10 14 One dimensional random walk 15 -1 -2 -1 0 1 0 1 0 -1 -2 16 The maximum is 1. 17 The minimum is -2. 18 # of returns to 0 is 3. 19 Two dimensional random walk 20 (1,1) (0,2) (-1,3) (0, 2) (-1,1) (-2,0) (-3,-1) (-4,0) (-5,1) (-4,0) 21 The farthest point is (-5,1). 22 # of returns to O is 0. 23 24 -----Enter # of rounds (0 to stop): 20 25 One dimensional random walk 26 -1 -2 -3 -4 -3 -2 -1 0 1 2 1 2 1 2 3 4 5 4 5 4 27 The maximum is 5. 28 The minimum is -4. 29 # of returns to 0 is 1. 30 Two dimensional random walk 31 (-1,-1) (0,0) (-1,1) (0,0) (-1,1) (0,2) (1,3) (2,4) (3,5) (2,6) (3,7) (4,8) (5,7) (4,6) (5,5) (6,4) (7,3) (6,2) (5, 1) (4,2) 32 The farthest point is (4,8). 33 # of returns to O is 2. 34 35 -Enter # of rounds (o to stop): 40 36 One dimensional random walk 37 -1 0 -1 0 -1 -2 -3 -2 -3 -4 -5 -6 -7 -8 -9 -8 -7 -8 -9 -8 -7 -6 -7 -6 -5 -6 -7 -8 -7 -8 -9 -10 -9 -8 -9 -10 -11 -10 -11 -10 38 The maximum is 0. 39 The minimum is -11. 40 # of returns to O is 2. 41 Two dimensional random walk 42 (1, 1) (2, 2) (3,1) (2.0) (1,-1) (0,-2) (1,-3) (2,-4) (1,-5) (2,-6) (3,-5) (2,-4) (3,-3) (2,-2) (1,-1) (0,-2) (1,-3) (0, -2) (-1,-3) (0,-4) (1,-3) (2,-4) (3,-5) (2,-4) (1,-3) (2,-2) (1,-1) (2,-2) (3,-1) (2,-2) (1,-3) (2,-2) (3,-1) (4,-2) (5, -1) (4,-2) (5,-3) (4,-4) (3,-5) (2,-6) 43 The farthest point is (2,-6). 44 # of returns to 0 is 0. 2 -Enter # of rounds (0 to stop): -2 45 46 47 48 49 50 -Enter # of rounds (o to stop): -3 -Enter # of rounds (0 to stop): 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago