Question: .In this assignment, you will add functionality to the Table class that was created in the lab assignment. 1) Add a constructor that receives a

.In this assignment, you will add functionality to the Table class that was created in the lab assignment.

1) Add a constructor that receives a single integer parameter, call it n. The maze is to be initialized as a square array of size n-by-n. Then, call the function init with the appropriate parameters.

2) Write a class method called shrink that will be similar to expand but where the new size of the table is actually smaller than the old table size. It should have the same parameters as the expand method, but the for-loops should be based on the new number of rows, and the number of elements to be copied in the call to arraycopy should be equal to the new number of columns. Add an extra test for this in main.

3) Write a method called prettyOutput for the maze where you output it in a format that is easier to visualize. For this, print a border around the table, like a + in each corner, a row of minuses above and below the table, and a | at the beginning and at the end of each row. Then when you print the elements, check if the value is equal to 0, and if it is, print two spaces, if it's equal to 1, then print a * character followed by one space, and if it is equal to 2, print a period (.) followed by one space.

4.)Write a method makeBorder that creates a border in the table made up of the value 2. For this, use the function Arrays.fill() to fill in the first (0) and last (rows-1) rows with the value 2. Then for all the rows in between, assign to the first (0) and last (columns-1) elements the value 2. In the main, after expanding the array by calling the method from the lab, call the function makeBorder, then pretty output the array to see the result. For example, if you were to call the function makeBorder on a maze that was just printed.

5.)Add two functions searchFirst and searchLast, that search for the first and last occurrence of a specific value in the maze, and output the coordinates if they find it, and a message saying that it's not there if not. In the main, add a call to each of these functions with the value 1.

The table java file that was created in a previous problem, please add on to using the steps above:

import java.util.Scanner;

import java.util.Random;

import java.util.Arrays;

public class Table {

int[][] maze; int rows, columns; static Scanner scan = new Scanner(System.in); static Random rand = new Random();

public Table(int m, int n){ /* Constructor with given numbers of rows and columns */ init(m,n); }

public Table (){ /* Default constructor */ rows = 0; columns = 0; }

public void init(int m, int n){ /* Initializes the maze with this number of rows and columns*/

if (m > 0 && n > 0) { maze = new int[m][n]; rows = m; columns = n; } }

public void randomize (float percent){ /* Generates random value of 1 in the maze based on the percent parameter*/

int i, j; float r; for (i = 0; i

public void rawOutput(){ /* Outputs the values in the maze one row per line, as integers*/

for (int i = 0; i

public void expand (int m, int n){ int[][] mazeCopy = maze; init(m,n); for (int i=0; i < mazeCopy.length; i++){ System.arraycopy(mazeCopy[i], 0, maze[i], 0, maze[i].length); } } public static void main(String[] args) { int n; float p; Table board = new Table(); System.out.println("Enter the dimension of the table"); n = scan.nextInt(); board.init(n, n); System.out.println("Enter the percentage of 1s in the maze as a number between 0 and 1"); p = scan.nextFloat(); board.randomize(p); board.rawOutput(); System.out.println("Enter a new size for th maze"); n = scan.nextInt(); board.expand(n, n); board.rawOutput(); } }

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!