Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the methods move() and Simulate(int steps). in move, the particle moves

ID: 3836039 • Letter: C

Question

Complete the methods move() and Simulate(int steps). in move, the particle moves one step in one of the possible 4 directions, left, right, forward, backwards. if moves left, x would decrease by 1, if moved right, x increases by 1. if moved backwards, y decreases by 1, if moved forward y increases by 1. For RandomMotion class, the particle randomly chooses to move between 4 directions. BaisedMotion class, the particle moves right with 50% probability, for the remaining 50%, it chooses randomly from the other 3 directions. Inside Simulate ( int steps) method, the particle moves for a certain number of time steps determined by the parameter int steps.

public class RandomMotion {
public int x;
public int y;
//initializes location
public RandomMotion(int x0,int y0)
{
x = x0;
   y = y0;
}
public void move()
{
   //insert code here
}
  
public void Simulate(int steps)
{
   //insert code here
}
}

************************************************************

public class BiasedMotion {
public int x;
public int y;
public BiasedMotion(int x0, int y0)
{
x = x0;
   y = y0;
}
  
public void move()
{

}
  
public void Simulate(int steps)
{
}
public static void main(String[] args)
{
     
  
}
}

Explanation / Answer

We have 4 possible directions to move: right, left, forward & backward. Lets assign numbers to the directions so that we can easily implement the requirements in program. So, 1 means right, 2 means left, 3 means forward and 4 means backward. For the first class RandomMotion, its given that the particle will move randomly in either of the 4 directions (With equal probabilities). Hence, we can choose a direction randomly. For that, we can use the class class defined in java.util.Random.

To generate a number between a range including the range(min,max), we can use the following code snippet:

The randomNumber in the above will hold a random number between the specified min and max numbers. So in our first case, we can use the following to generate a random number between 1 and 4:

Using the above, we can fill up the function defintion as follows with the given conditions:

import java.util.Random;

public class RandomMotion {
public int x;
public int y;
//initializes location
public RandomMotion(int x0,int y0)
{
x = x0;
y = y0;
}
public void move()
{
Random rand = new Random();
//Generate integer between 1 and 4, where 1 is right, 2 is left, 3 is forward and 4 is backwards
int randomNum = rand.nextInt(4) + 1;
//Do calculation according to the direction obtained.
switch(randomNum){
//Right
    case 1: x++;
    break;
//Left
    case 2: x--;
    break;
//Forward
    case 3: y++;
    break;
//Backward
    case 4: y--;
    break;
}
}
  
public void Simulate(int steps)
{
//Move steps number of times
for(int i=0;i<steps;i++)
    move();
}
}

For the second class BiasedMotion, we need to have a 50% bias towards the right direction and 50% for the rest of the directions. Keeping the previous idea in mind, we first generate a random number between 1 and 2 where 1 represents right and 2 represents all other directions combined. If the generated number is 1, we proceed according to the given condition. If 2 is generated, we will again generate a random number between 2 and 4. Where 2 is left, 3 is forward and 4 is backward. We will then proceed according to the given conditions:

import java.util.Random;

public class BiasedMotion {
public int x;
public int y;
public BiasedMotion(int x0, int y0)
{
x = x0;
y = y0;
}
  
public void move()
{
Random rand = new Random();
//Get random number between 1 & 2 where 1 represents right and 2 represents all other directions.
//If the generated number is 1, move right
//Else, again find a random number between 2 and 4 and move accordingly.
int randomNum = rand.nextInt(2) + 1;
if(randomNum == 1)
//Right
x++;
else
{
//Find a random number between 2 and 4. Move accordingly
randomNum = rand.nextInt(3) + 2;
switch(randomNum){
//Left
case 2: x--;
break;
//Forward
case 3: y++;
break;
//Backward
case 4: y--;
break;
}
}
}
  
public void Simulate(int steps)
{
for(int i=0;i<steps;i++)
move();
}
public static void main(String[] args)
{

}
}

Hope this helps, Please revert in case of further queries!