Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment To Do: MagicSquareTest.java - contains JUnit test cases for the Magic

ID: 3891604 • Letter: A

Question

Assignment To Do:

MagicSquareTest.java - contains JUnit test cases for the MagicSquare class. Your code must pass all these tests.

MagicSquare.java - contains static methods to determine if a two-dimensional array of ints is a Magic Square:isMagicSquare() - returns true if a two-dimensional array of ints meets all the criteria to be a Magic Square. This method is already completed for you. It uses all of the following methods:

isSquare() - returns true if a two-dimensional array of ints is a square - in other words, the lengths of all rows and columns are the same. This method is already completed for you.

containsAllNumbers() - returns true if a square two-dimensional array of ints contains all integers from 1 to n*n, where n is the length of the rows and columns. This method is already completed for you.

diagonal1Sum() - returns the sum of the numbers in the upper-left to lower-right diagonal of a square two-dimensional array of ints. You must write the body for this method.

diagonal2Sum() - returns the sum of the numbers in the upper-right to lower-left diagonal of a square two-dimensional array of ints. You must write the body for this method.

rowSums() - if all the rows of a square two-dimensional array of ints have the same sum, returns that sum. Otherwise, returns -1. You must write the body for this method. It uses the following:

rowSum() - returns the sum of the numbers in a given row. You must write the body for this method.

colSums() - if all the columns of a square two-dimensional array of ints have the same sum, returns that sum. Otherwise, returns -1. You must write the body for this method. Uses:

colSum() - returns the sum of the numbers in a given column. You must write the body for this method.

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Code

public class MagicSquare
{
/**
* Determines if an array is a Magic Square.
*
* @param square a two-dimensional array of ints
* @return true if the array is a Magic Square
*/
public static boolean isMagicSquare(int[][] square) {
/* If the array is not square and
* does not contain all numbers from 1 to n*n,
* where n is the length of the sides,
* it's not a Magic Square
*/
if (!isSquare(square) || !containsAllNumbers(square)) {
return false;
}
  
/* Get the sum of the first diagonal, and remember it
* If the other diagonal's sum doesn't match, it's not a Magic Square
*/
int sum = diagonal1Sum(square);
if (sum != diagonal2Sum(square)) {
return false;
}
  
// If the sums of each of the rows does not match, it's not a Magic Square
if (sum != rowSums(square)) {
return false;
}

// If the sums of each of the columns does not match, it's not a Magic Square
if (sum != colSums(square)) {
return false;
}
  
// Passed all tests - it is a Magic Square
return true;
}
  
/**
* Determines if an array is a square.
*
* @param square a two-dimensional array of ints
* @return true if the array is a square (all rows and cols are same length, and not zero length)
*/
public static boolean isSquare(int[][] array) {
int rows = array.length;
if (rows == 0) {
return false;
}
  
boolean square = true;
for (int col = 0; col < rows; col++) {
if (array[col].length != rows) {
square = false;
}
}
return square;
}
  
/**
* Determines if the array contain all numbers from 1 to n*n,
* where n is the length of the sides
*
* @param square a square, two-dimensional array of ints
* @return true if the array contain all numbers from 1 to n*n
*/
public static boolean containsAllNumbers(int[][] square) {
ArrayList<Integer> numList = new ArrayList<Integer>();
for (int row = 0; row < square.length; row++) {
for (int col = 0; col < square[row].length; col++) {
numList.add(square[row][col]);
}
}
  
boolean all = true;
for (int num = 1; num <= square.length * square.length; num++) {
if (numList.indexOf(num) == -1) {
return false;
}
}
return true;
}
  
/**
* Get the sum of the first diagonal
* (upper-left to lower-right)
*
* @param square a square, two-dimensional array of ints
* @return sum of the numbers in the diagonal
*/
public static int diagonal1Sum(int[][] square) {
// to be completed by student
return 0; // to keep the compiler happy - remove this
}
  
/**
* Get the sum of the second diagonal
* (upper-right to lower-left)
*
* @param square a square, two-dimensional array of ints
* @return sum of the numbers in the diagonal
*/
public static int diagonal2Sum(int[][] square) {
// to be completed by student
return 0; // to keep the compiler happy - remove this
}
  
/**
* Get the sum of each row, if they are all the same
*
* @param square a square, two-dimensional array of ints
* @return -1 if the sums don't match, sum of the first row otherwise
*/
public static int rowSums(int[][] square) {
// to be completed by student
return 0; // to keep the compiler happy - remove this
}

/**
* Get the sum of each col, if they are all the same
*
* @param square a square, two-dimensional array of ints
* @return -1 if the sums don't match, sum of the first ol otherwise
*/
public static int colSums(int[][] square) {
// to be completed by student
return 0; // to keep the compiler happy - remove this
}
  
/**
* Get the sum of a given row
*
* @param square a square, two-dimensional array of ints
* @return sum of the numbers in the given row
*/
public static int rowSum(int[][] square, int row) {
// to be completed by student
return 0; // to keep the compiler happy - remove this
}

/**
* Get the sum of a given column
*
* @param square a square, two-dimensional array of ints
* @return sum of the numbers in the given column
*/
public static int colSum(int[][] square, int col) {
// to be completed by student
return 0; // to keep the compiler happy - remove this
}
}

------------------------------------------------------

Magic Square Test

public class MagicSquareTest
{
int[][] square4x4Magic = {{16, 3, 2,13},
{ 5,10,11, 8},
{ 9, 6, 7,12},
{ 4,15,14, 1}};
int[][] square4x4NotMagic = {{16, 2, 3,13},
{ 5,10,11, 8},
{ 9, 6, 7,12},
{ 4,15,14, 1}};
int[][] square4x4NotAllNums = {{16, 3, 3,13},
{ 5,10,11, 8},
{ 9, 6, 7,12},
{ 4,15,14, 1}};
int[][] square1x1Magic = {{1}};
int[][] notSquare0x0 = new int[0][0];
int[][] notSquare1x2 = {{1,2}};
int[][] ragged = {{1,2,3},
{4,5,6},
{7,8,9,10}};
  
/**
* Default constructor for test class MagicSquareTest
*/
public MagicSquareTest()
{
}

/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
@Before
public void setUp()
{
}

/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
@After
public void tearDown()
{
}

@Test
public void testIsSquare() {
assertTrue(MagicSquare.isSquare(square1x1Magic));
assertTrue(MagicSquare.isSquare(square4x4Magic));
assertTrue(MagicSquare.isSquare(square4x4NotMagic));
assertTrue(MagicSquare.isSquare(square4x4NotAllNums));
assertFalse(MagicSquare.isSquare(notSquare1x2));
assertFalse(MagicSquare.isSquare(notSquare0x0));
assertFalse(MagicSquare.isSquare(ragged));
}

@Test
public void testContainsAllNums() {
assertTrue(MagicSquare.containsAllNumbers(square4x4Magic));
assertTrue(MagicSquare.containsAllNumbers(square4x4NotMagic));
assertFalse(MagicSquare.containsAllNumbers(square4x4NotAllNums));
}
  
@Test
public void testIsMagicSquare() {
assertTrue(MagicSquare.isMagicSquare(square4x4Magic));
assertFalse(MagicSquare.isMagicSquare(square4x4NotAllNums));
assertFalse(MagicSquare.isMagicSquare(square4x4NotMagic));
}
}

Explanation / Answer

Given below is the completed code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

import java.util.ArrayList;

public class MagicSquare
{
/**
* Determines if an array is a Magic Square.
*
* @param square a two-dimensional array of ints
* @return true if the array is a Magic Square
*/
public static boolean isMagicSquare(int[][] square) {
/* If the array is not square and
* does not contain all numbers from 1 to n*n,
* where n is the length of the sides,
* it's not a Magic Square
*/
if (!isSquare(square) || !containsAllNumbers(square)) {
return false;
}

/* Get the sum of the first diagonal, and remember it
* If the other diagonal's sum doesn't match, it's not a Magic Square
*/
int sum = diagonal1Sum(square);
if (sum != diagonal2Sum(square)) {
return false;
}

// If the sums of each of the rows does not match, it's not a Magic Square
if (sum != rowSums(square)) {
return false;
}

// If the sums of each of the columns does not match, it's not a Magic Square
if (sum != colSums(square)) {
return false;
}

// Passed all tests - it is a Magic Square
return true;
}

/**
* Determines if an array is a square.
*
* @param square a two-dimensional array of ints
* @return true if the array is a square (all rows and cols are same length, and not zero length)
*/
public static boolean isSquare(int[][] array) {
int rows = array.length;
if (rows == 0) {
return false;
}

boolean square = true;
for (int col = 0; col < rows; col++) {
if (array[col].length != rows) {
square = false;
}
}
return square;
}

/**
* Determines if the array contain all numbers from 1 to n*n,
* where n is the length of the sides
*
* @param square a square, two-dimensional array of ints
* @return true if the array contain all numbers from 1 to n*n
*/
public static boolean containsAllNumbers(int[][] square) {
ArrayList<Integer> numList = new ArrayList<Integer>();
for (int row = 0; row < square.length; row++) {
for (int col = 0; col < square[row].length; col++) {
numList.add(square[row][col]);
}
}

boolean all = true;
for (int num = 1; num <= square.length * square.length; num++) {
if (numList.indexOf(num) == -1) {
return false;
}
}
return true;
}

/**
* Get the sum of the first diagonal
* (upper-left to lower-right)
*
* @param square a square, two-dimensional array of ints
* @return sum of the numbers in the diagonal
*/
public static int diagonal1Sum(int[][] square) {
// to be completed by student
int sum = 0;
for(int i = 0; i < square.length; i++)
sum += square[i][i];

return sum;
}

/**
* Get the sum of the second diagonal
* (upper-right to lower-left)
*
* @param square a square, two-dimensional array of ints
* @return sum of the numbers in the diagonal
*/
public static int diagonal2Sum(int[][] square) {
// to be completed by student

int sum = 0;
int n = square.length;
for(int i = 0; i < square.length; i++)
sum += square[i][n - i - 1];

return sum;
}

/**
* Get the sum of each row, if they are all the same
*
* @param square a square, two-dimensional array of ints
* @return -1 if the sums don't match, sum of the first row otherwise
*/
public static int rowSums(int[][] square) {
// to be completed by student

int sum = 0;
int row0Sum = rowSum(square, 0);
for(int row = 0; row < square.length; row++){
sum = rowSum(square, row);

if(sum != row0Sum)
return -1;

}
return row0Sum;
}

/**
* Get the sum of each col, if they are all the same
*
* @param square a square, two-dimensional array of ints
* @return -1 if the sums don't match, sum of the first ol otherwise
*/
public static int colSums(int[][] square) {
// to be completed by student
int sum = 0;
int col0Sum = colSum(square, 0);
for(int col = 0; col < square.length; col++){
sum = colSum(square, col);

if(sum != col0Sum)
return -1;

}
return col0Sum;
}

/**
* Get the sum of a given row
*
* @param square a square, two-dimensional array of ints
* @return sum of the numbers in the given row
*/
public static int rowSum(int[][] square, int row) {
// to be completed by student
int sum = 0;
for(int col = 0; col < square[row].length; col++)
sum += square[row][col];
return sum;
}

/**
* Get the sum of a given column
*
* @param square a square, two-dimensional array of ints
* @return sum of the numbers in the given column
*/
public static int colSum(int[][] square, int col) {
// to be completed by student
int sum = 0;
for(int row = 0; row < square.length; row++)
sum += square[row][col];
return sum;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote