Question: 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

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.

// Import Libraries import java.util.Scanner; import java.util.Random; import java.util.Arrays;

// Create a class "Table" public final class Table { // Class Attributes private int[][] maze; // 2D Array Integer private int rows;// Number of Rows private int columns; // Number of Columns // Scanner and Random Objects private static Scanner scan = new Scanner(System.in); private static Random rand = new Random(); // No Attribute Constructor public Table() { this.rows = 0; this.columns = 0; }

// Argument Constructor public Table(int m, int n) { init(m,n); }

// Init Method public void init(int m, int n) { // Set Rows and Columns Size and Initialize Array this.rows = m; this.columns = n; this.maze = new int[m][n]; }

// Method to Randomly Fill Array public void randomize(float val) { // Traverse Rows for(int i=0; i

// Check if less than parameter value, assign 1 if(num<=val) { this.maze[i][j] = 1; } } } } // Method to Print Table public void rawOutput() { for(int i=0; i

// Initialize New Size init(m,n);

// Copy elements of copied array for(int i=0; i

// Main Method public static void main(String[] args) { // Create Table Object Table board = new Table();

// Take User-Input Size System.out.print(" Enter N: "); int n = scan.nextInt();

// Initialize Board board.init(n,n);

// Get User-Input Occupancy Percentage System.out.print("Percentage of Maze Occupancy: "); float oc = scan.nextFloat();

// Fill Randomly and Print board.randomize(oc); board.rawOutput();

// Take New Size and Print System.out.print(" Enter new n: "); n = scan.nextInt(); board.expand(n,n); board.rawOutput(); } }

What am I missing here ?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!