Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose of this lab is to work with two dimensional arrays and the running sum operation. 1. A Magic Square is an n-by-n matrix (two-dimensional

Purpose of this lab is to work with two dimensional arrays and the running sum operation. 1. A Magic Square is an n-by-n matrix (two-dimensional array) that is filled with the numbers 1,2,3, , n2 , such that the sum of the elements in each row, in each column, and in the two diagonals is the same value. For example, 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 is a Magic Square since all of the numbers from 1 to 16 are used exactly once; and, all row sums, column sums, and both diagonal sums are equal to 34. Only one of these two-dimensional arrays qualifies as a magic square: 5 5 5 2 7 6 2 5 6 5 5 5 9 5 1 9 1 4 5 5 5 4 3 8 3 8 7 2. Write a Java program that can determine if a square, 2-dimensional array is or is not a magic square. (continued on back) Based on the return result of the magicSquare instance-method, the main method should output a message that states if the two-dimensional array IS or IS NOT a Magic Square. Place all of the square tables in a single multi-dimensional (3-D) array: int [][][] matrices = { /* matrices[0] */ { { 5, 5, 5 }, { 5, 5, 5 }, { 5, 5, 5 } } /* matrices[1] */ { { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } }, /* matrices[2] */ { { 9, 8, 7 }, { 6, 5, 4 }, { 3, 2, 11 }}, /* matrices[3] */ { { 1 }, { 6, 7 }, { 9, 8, 4 } }, /* matrices[4] */ { {16, 3, 2, 13}, {5, 10, 11, 8}, { 9, 6, 7, 12}, {4, 15, 14, 1} } };. // For each 2-D matrix in the array of matrices: for ( int [][] matrix : matrices ) { MagicSquare table = new MagicSquare( matrix ); // display the current matrix with toString(). // determine if the current matrix is or is not a magic square // display an appropriate message: // The matrix is a magic square // The matrix is NOT a magic square } // end for-loop // continued on the next page. public class MagicSquare { /* 1. Declare an empty private 2-D array instance field of type int (square). 2. Write a constructor to create a deep copy of an input 2-D array (square). 3. Write the toString method to return a String containing the 2D-array (square). 4. Write an instance method to determine if the 2- Dimensional array (square) is or is not a magic square. Return true if it is a magic square, otherwise return false. */ }

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

Students also viewed these Databases questions

Question

=+ What topics are contained in the contracts?

Answered: 1 week ago