The project involves simulating the random walk of your favorite bug. Here\'s th
ID: 3744667 • Letter: T
Question
The project involves simulating the random walk of your favorite bug. Here's the proble: Your bug is placed on a given square in the middle of a tile floor in a rectangular roon of size n × m tiles. The bug wanders randomly from tile to tile throughout the room. Assuming that t may move from its present tile to any of the eight tiles surrounding it (unless it is against a wall) with equal probability, how long will it take the bug to touch every tile on the floor at least once? 1 The Tasks 1. Write a class called RandomWalk. RandomWalk is to have a two-dimensional array as an instance variable. The two dimensional array is to represent the number of times each tile of the n x m rectangular array of tiles in the room is visited. 2. Write a constructor that takes two arguments representing the the dimensions of the room (in tiles). Write a default constructor that takes no arguments and initializes the size of the room to some default dimensions. Initialize the 2D-arr ay appropriately 3. Write a public method called runBug that takes no parameters and rus the simulation. 4. Write a (private) method that generates the next move for the bug. The method is to pick 5. Write a public method called get TotalSteps that takes no arguments and returns the total 6. Write a public method called printRoom that prints out the 2D tile array on the console one of the four adjacent tiles randomly. (See Section 2 below and Section 3.4 in the text.) number of steps taken by the bug during the simul (terminal) in a nicely formatted fashion ation. 7. Write a main method that creates several instances of the RandomWalk, calls runBug for each instance and prints the total number of steps taken and the contents of the 2D tile array cach case. Your simulation may require additional private instance variables and methods. 2 Generating a Random Number A random integer can be generated using the Random Java class. Here is an example: gennew Random) int step gen. next Int. (10); = step will have a value chosen randomly in the range 0 - 9 (note: 1 less than the argument to nextint). If you wanted a value ranging from, say, -5 to 5, you would write gen.nextint(11) -5 For this project you wl need to generate a random number for each of the coordinates in the array So, suppose your bug is at tile (5,3). The possible tiles it can move to are (4, 3), (5,2), (5, 4), (6,3) (up, left, right, down respectively). To generate one of these pairs you need to generate a number between-1 and 1 for each dimension and then add those to the current coordinates. If the generate move either leaves the bug where it is, moves it diagonally, or moves it off the floor, throw the pair away and generate another one until a valid move is computedExplanation / Answer
import java.util.Random; import java.util.Scanner; public class RandomWalk { private int [][] tiles; private int n; private int totalSteps; private int currentX; private int currentY; Random gen = new Random(); public RandomWalk(){ totalSteps = 0; n = 8; } public void initialize(int n) { tiles = new int[n][n]; } public void runBug(int size){ currentX = n/2; currentY = n/2; initialize(size); pickMove(); for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.