# Java代写 | COMP 1020 Lab 4 Multidimensional arrays

Notes:

The three exercises are independent – you can do them in any order.

Only one of the three exercises is required. The Gold exercise is not much more difficult than the
Silver exercise this time (some students may actually find it easier).

1. Download the file RandomArrayTemplate.java and save it as RandomArray.java before using it. The
supplied TestLab4Bronze.java program will expect it to be named this way.

2. This class creates rectangular two-dimensional arrays filled with random positive integer values. Some
of the class is written for you. Complete it by adding code in the four places marked //***ADD YOUR
CODE HERE***.

a. Add a private instance variable that will hold a 2-dimensional array of integers.

b. Complete the constructor, which will create the 2-dimensional array, with the specified number
of rows and columns, containing values from 0 to range-1. The code to generate such random
numbers is supplied in the comments.

c. Complete the getRow method, which will return a copy (clone or deep copy) of the specified
row of the array, as an array of integers of the correct length. In this method, use the built-in
System.arraycopy method to copy the values from the original array into the result, as
demonstrated in class.

d. Complete the getCol method, which will return a copy (clone or deep copy) of the specified
column of the array, as an array of integers of the correct length. In this method, you will have
to copy the values yourself using a for loop since the System.arraycopy method cannot be
used in this situation.

3. Run the supplied TestLab4Bronze.java program to test your class. Sample output from this program
is shown below. User input is in blue. Your numbers will be different.

How many rows?

How many columns?

The rows contain:

Row 0: [15, 92, 48, 87]
Row 1: [74, 32, 24, 10]
Row 2: [55, 0, 50, 14]

The columns contain:

Column 0: [15, 74, 55]
Column 1: [92, 32, 0]
Column 2: [48, 24, 50]
Column 3: [87, 10, 14]

1. Download the file MagicSquareTemplate.java and save it as MagicSquare.java before using it.
The supplied TestLab4Silver.java program will expect it to be named this way.

2. This class creates a square two-dimensional array which is a “magic square”. A magic square
of “order n” is a square with n rows and n columns that contains each of the numbers from 1 to
n2. The sum of every row, every column, and the two diagonals, must be the same. For
example, an order 5 magic square is shown below. Every row, column, and diagonal adds up to
the same thing (65). Such squares are very old, with early examples dating back to 2200BC.

11 24 7 20 3
17 5 13 21 9
23 6 19 2 15
4 12 25 8 16
10 18 1 14 22

3. Some of the class is written for you. Complete it by adding code in the three places marked

a. Add a private instance variable that will hold the magic square as a 2-dimensional array
of integers.

b. Complete the constructor, which will create a magic square with a given size (n), which
must be an odd number. (You don’t have to check.) To do this, use a very old technique
called “de la Loubere’s algorithm” (note that this will give a different square from the
one shown above):

i. Place the numbers in the square in order, from 1 to n2
, starting with the 1 in the
centre of the bottom row.

ii. If the number you just placed is divisible by n, then the next one goes
immediately above it.

iii. If the number you just placed is not divisible by n, then the next one goes
diagonally down and to the right.

iv. Both the rows and the columns are cyclic. If you go off the top of the square, re-
enter at the bottom. If you go off the bottom of the square, re-enter at the top.
Do the same for the columns. Hint: This can easily be accomplished using the
mod operator (%).

v. See the sample output below for examples of squares constructed this way.

c. Complete the toString method, which will return the proper String to use to print the
magic square. It should contain tabs (\t) between the columns and newlines (\n)
between the rows.

E-mail: itcsdx@outlook.com  微信:itcsdx