C211 Problem Solving and Programming II

C211 I211 Homework 2

Due Date: Tuesday, September 5, 2023.

Make sure to complete Lab 2 before doing the homework.

In this homework, you will continue adding functionality to the class Table created in Lab 2.

Ex. 1. Table Class

Continue working on the Table class from the project lab2 created in the lab.

a. Constructor

Add a constructor with a single parameter n, where the maze is initialized as a square array of n-by-n. In this constructor, call the function init with the appropriate parameters.

b. Shrink

Write another class method called shrink similar to expand but where the new size of the table is actually smaller than the old one. It should have the same parameters as the function expand, but the for loop 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 it in the main.

c. Search

Add two bool functions searchFirst and searchLast, that search for the first and last occurrence of a specific value in the maze, and return true if the value is found and false if it isn't. The value should come in as a parameter int target. The functions should output the coordinates (row and column) of the first or last place where the value is found, 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 for the target.

The function searchFirst is best dealt with by having a return when the value is found. For nested loops, a break will only break out of the inner loop, so it's not sufficient here.

For the function searchLast, one idea is to use loops that scan the table backwards, starting from the largest index in each dimension and going down towards 0. Then a return when the value is found would also work. The second idea is to store the values of the indexes where the value is found in some local variables and print their values at the end. Either version is fine (or some other idea you can think of).

d. (optional, 3 extra credit points) Pretty Output

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 a couple of spaces, if it's equal to 1, then print a * character followed by a space, and if it's equal to 2, print a period (.) followed by a space.

For example, if the raw output of the table gives us

1 0 0 1 0
0 0 1 0 0
1 1 0 0 0
0 1 0 0 0
0 0 0 1 1

then the pretty output of the table should be



+----------+
|*     *   |
|    *     |
|* *       |
|  *       |
|      * * |
+----------+

Hint. For this exercise, the maze itself will not be changed. This is not about replacing the numbers in the table with characters. The numbers remain as they are.

For the top and bottom borders, you have to start by printing a "+" string. Then you need a loop going over the number of columns, where for each element you output the string "--". Then after the loop you need another "+".

For the part in between, you need a couple of nested loops, like in the function rawOutput. For each element of the maze, test if it is 0, and print the string " " in that case, and so on. You also need to print "|" before and after each row for the vertical borders.

e. (optional, 3 extra credit points). Border

Write a method makeBorder that creates a border in the table made of the value 2. For this, use the function Arrays.fill() to fill in the first (index of 0) and last (index of 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. You'll need a loop for the second part.

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, the raw output would give you

2 2 2 2 2
2 0 1 0 2
2 1 0 0 2
2 1 0 0 2
2 2 2 2 2

then the pretty output of the table should be

+----------+
|. . . . . |
|.   *   . |
|. *     . |
|. *     . |
|. . . . . |
+----------+
Homework Submission

Upload the file Table.java to Canvas, Assignments, Homework 2: a single file for both the lab and the homework.