Write a method matricize that takes a one-dimensional


Magic Square:

a. Write a method "createArithmeticSeq" that prompts the user toinput two numbers, "first" and "diff". The method thencreates a one-dimensional array of 16 elements ordered in anarithmetic sequence. It also outputs the arithmeticsequence. For example, if "first = 21" and "diff = 5", thearithmetic sequence is: 21 26 31 36 41 46 51 56 61 66 71 7681 86 91 96

b. Write a method "matricize" that takes a one-dimensional array of16 elements and a two-dimensional array of 4 rows and 4 columns asparameters. This method puts the elements of theone-dimensional array into the two-dimensional array. Forexample, if "A" is the one-dimensional array created in part(a) and"B" is a two dimensional array, then after putting the elements of"A" into "B", the array "B" is:

21 26 31 36
41 46 51 56
61 66 71 76
81 86 91 96

c. Write a method "reverseDiagonal" that reverses both diagonals ofa two-dimensional array. For example, if the two dimensionalarray is as shown in part(b), after reversing the diagonals, thetwo-dimensional array is:

96 26 31 81
41 71 66 56
61 51 46 76
36 86 91 21

d. Write a method "magicCheck" that takes a one-dimensional arrayof size 16, a two dimensional array of 4 rows and 4 columns, andthe sizes of the arrays as parameters. By adding all of theelements of the one-dimensional array and dividing by 4, thismethod determines the "magicNumber". The method then addseach row, each column, and each diagonal of the two-dimensionalarray and compares each sum with the magic number. If the sumof each row, each column, and each diagonal is equal to the"magicNumber", the method outputs "It is a magic square";otherwise, it outputs "It is not a magic square". Do notprint the sum of each row, each column, and the diagonals.

e. Write a method "printMatrix" that ouputs the elements of atwo-dimensional array, one row per line. This output shouldbe as close to a square form as possible.

f. The methods written in parts (a) through (e) should be generalenough to apply to an array of any size. (The work should be doneby using loops.) The method "magicCheck" works better if itcalls two seperate methods: one to add the rows and columns, andone to add the diagonals. (In this case, the program comparesthe magic number with each sum [row, column, and diagonal] in therespective method.)

Test the methods you wrote in parts (a) through (f) using thefollowing class and the method main:

public class Ch9_PrExercise13
{
static final int rows = 4;
static final int columns = 4;

static final int listSize = 16;
...
public static void main (String[] args)
{
int[] list = new int[listSize];
int [][] matrix = new int[rows][columns];

createArithmeticSeq(list);
matricize(list, matrix);
printMatrix(matrix);
reverseDiagonal(matrix);
printMatrix(matrix);
magicCheck(list, matrix);
}

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Write a method matricize that takes a one-dimensional
Reference No:- TGS0141999

Expected delivery within 24 Hours