Due Date: Tuesday, September 5, 2023.
In this lab, we're going to create a class storing a 2D array and perform some operations with it.
Open either Eclipse or NetBeans. Create a project called lab2 and a class called Table inside it.
Attributes. Add an instance attribute to this class called maze as a 2D array of type integer. Also add two instance attributes called rows and columns.
Constructors. Write a constructor to this class that takes two attributes, m and n, both integers, standing for the number of rows and columns. Also add a default constructor with no parameters where both dimensions are initialized as 0.
Init. Write a public method called init that takes two integer parameters for the number of rows and columns, and initializes the table object by allocating the array with these dimensions. Update the attributes row and col in this function. Then call this method from the constructor with parameters instead of initializing the array directly.
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).
In the main, declare a Table object called board and use the default constructor to create the object. Declare an integer variable n, then ask the user for a value for it. After you input it, initialize the table with dimensions n-by-n. This will be a square table.
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().
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, with 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.
Then in the main, ask the user for a percentage of maze occupancy, and input a 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.
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. 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.
Upload the file Table.java in Canvas, Assignments, Homework 2 after you finish the homework, since the homework will continue with the same project.