Question
Create a project called lab2. Inside this project, create a class called Table. 2) In this class, add an attribute called maze that is a
Create a project called lab2. Inside this project, create a class called Table. 2) In this class, add an attribute called maze that is a two-dimensional array of integers. Next, add two integer attributes called rows and columns. 3) Write a constructor for the Table class that accepts two integer parameters (call them m and n). These parameters represent the number of rows and columns. Additionally, write a no-argument constructor where both dimensions are initialized to 0. 4) Write a public method called init that accepts two integer parameters for the number of rows and columns. The table object will be initialized by allocating the array using these dimensions. Then, call this method from the constructor (the one with two parameters) instead of initializing the array directly. 5) At the top of the file, inside the package but before the class, add a statement to import the module java.util.Scanner. Then add a class attribute (static) called scan of type Scanner and initialize it with new Scanner(System.in). 6) In the main, declare a Table object called board and use the default constructor to create the object. Declare an integer variable, n, and ask the user to provide a value for it. After you receive the input, initialize the table with dimensions n-by-n. This will be a square table. 7) Random numbers will be required. To prepare to use a random number generator in Java, import the module java.util.Random. Then declare another static attribute rand of type Random and assign it a new Random(). 8) Add a public method called randomize with one parameter of type float, assumed to be a number between 0 and 1. Then, traverse the maze array and for each cell, generate a random number of type float between 0 and 1 using the method nextFloat(). Then, check if the number is less than or equal to the parameter of the function, and if it is, then assign to the cell of the maze the value 1
Add a public method called rawOutput with no parameters and of type void. In this function, traverse the maze array and write all the values out, one row per output line, separated by a space. 10) Then in the main, ask the user for a percentage of maze occupancy, and input this value into a float variable. After you initialize the table, call the function randomize with this variable as parameter and then output the table to see the result.
Expanding the Array: 1) Write a method that expands the array using a method from the Java class Arrays. For this, first import the module java.util.Arrays. Then add a public void method called expand, that takes two parameters n and m, like in the init method. In this method, we want to initialize the array with a bigger size, then copy the values from the old array into the new one. The new elements should be 0s. To do this, declare a local variable called mazeCopy, also of type 2D array of integers, and assign to it a reference to the maze (not to an element, but to the whole array). Then call the function init with the new size, and after that, traverse the array mazeCopy and use the function System.arraycopy() to copy a row at a time from the mazeCopy into maze. In the main, ask the user again for a value for n, then call the method expand with the new size and output the maze again after that to verify that the old values are still there.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started