Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Ex. 1. a. Table class.(JAVA)

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

More Books

Students also viewed these Databases questions