Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Ex. 1. a. Table class. Open either Eclipse or NetBeans. Create a project called lab2 and a class called Table inside it. Attributes. Add

Java

Ex. 1. a. Table class.

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. Then call this method from the constructor with parameters instead of initializing the array directly.

b. Input

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.

c. Randomize and Output

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.

d. Expanding the Array

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.

e. Reading the Table from a File

Add a class method that reads the array from a file with the name fileRead, taking one parameter of type String representing the name of the file. After creating the file, use a scanner initialized with the file object to read the data from the file.

We'll assume that the file contains the number of rows and columns on the first line, separated by a space, and then each row on its own line, again, with the numbers separated by simple spaces.

After inputting the number of rows and columns, you should call the function init to initialize the array properly. Then you can read in all of the numbers. Close the scanner when the operation is complete.

In the main, add a test for this function. You will need to create a text file containing the data in a table as described above first. Place this file in the folder where the Java project is.

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. 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 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.

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

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

I need a screenshot of the program showing the whole code and classes, please

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started