Java question, I need the code :( don\'t really understand Particle is wandering
ID: 3870784 • Letter: J
Question
Java question, I need the code :( don't really understand
Particle is wandering around a square room, which is a 10x10 frame. It starts in the middle of the room, and in each time-frame it goes one step in a random way (up, down, left right). If she tries to go outside the room, she does not move in that time-frame. We are interested in knowing how long she has to move until she has reached every single compartment of the 10x10 frame.
Write the Java application SlembiEind.java that simulates this trip of the end, but keep the frame (as a two-dimensional boolean array) and write out a number of steps when the end has come in all the compartments.
Explanation / Answer
import java.util.Random;
public class SlembiEind{
public static void main(String args[]){
boolean [][] frame=new boolean[10][10];
int count = 1, i ,j, dir,ans;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
frame[i][j] = false;
}
frame[4][4] = true;
count = 1;
i = 4 ;
j = 4 ;
ans = 0;
while(count != 100)
{
Random rn = new Random();
dir = rn.nextInt(4-1+1) + 1;
ans++;
if(dir == 1)
{
if(i!=0)
{
i--;
if(frame[i][j] == false)
{
frame[i][j] = true;
count++;
}
}
}
else if(dir == 2)
{
if(i!=9)
{
i++;
if(frame[i][j] == false)
{
frame[i][j] = true;
count++;
}
}
}
else if(dir == 3)
{
if(j!=0)
{
j--;
if(frame[i][j] == false)
{
frame[i][j] = true;
count++;
}
}
}
else
{
if(j!=9)
{
j++;
if(frame[i][j] == false)
{
frame[i][j] = true;
count++;
}
}
}
}
System.out.println("Number of steps required-" + ans);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.