Lo Shu Magic Square, on page 494 in the text book. Your program has to be implem
ID: 3675543 • Letter: L
Question
Lo Shu Magic Square, on page 494 in the text book.
Your program has to be implemented in OOP. Otherwise the assignment receives 0 for grade!
As a programmer, your tasks are to design the program in a class diagram, and then implement the design in Java program.
Create a class diagram
Name the service class MagicSquare.
The class contain
a 2-D array as a private data member
a constructor with a 2-D array as a parameter
a public void method, showArray(), to display the values of elements in the array using loop structure.
a public void method, showResult() to display whether the 2-D array is Lo Shu magic square or not
Create one or more private methods returning a boolean value to check whether the 2-D array is Lo Shu magic square or not. These methods are called in the showResult() method, so they should be private.
Need to check the range of value (1 to 9)
Need to check if each number is unique
Need to sum each row, each column, and each diagonal, and check whether all sums have the same value.
Implement the design in Java code
Your program structure should reflect the class diagram structure.
Write Javadoc comments for all classes, and for all methods in the service class(es).
Remember to write and test code line by line and block by block!
To sum each row, each column, and each diagonal, you can use either loop or non-loop structures.
Create an application class to test the service class:
Use the initializer list to create two 2-D arrays with the data in the screen shot below. One of the arrays is for a non-magic square, another for a magic square.
Instantiate two objects of the MagicSquare class, and pass an array as the argument to the constructor.
Call methods to display the array and result for each object.
Explanation / Answer
public class MagicSquare {
private int a[][];
public MagicSquare(int arr[][]) {
this.a = arr;
}
// function to print matrix
public void showArray(){
for(int i=0; i<a.length; i++){
for(int j=0; j<a[0].length; j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
// function to check whether satisfies all conditions to be Lo shu
private boolean check(){
int sum,s;
sum=0;
for (int col = 0; col< a[0].length; col++) //first set the sum (15 always?)
sum += a[0][col];
//check the next two rows
s = 0;
for (int row = 1; row < a.length; row++){
s = 0;
for (int col = 0; col< a[0].length; col++)
s += a[row][col];
if (s != sum)
return false;
}
//check columns
for (int col = 0; col< a[0].length; col++) {
s = 0;
for (int row = 0; row < a.length; row++){
s += a[row][col];
}
if (s != sum)
return false;
}
//check diagonal 1
s =0;
for (int row = 0; row < a.length; row++){
s += a[row][row];
}
if (s != sum)
return false;
//check diagonal 2
s =0;
for (int row = 0; row < a.length; row++){
s += a[row][2 - row];
}
if (s != sum)
return false;
//check that the numbers 1..9 appear
//We use an array of booleans and mark each one as we find it.
boolean[] appears = new boolean[9];
for (int i=0; i < 9; i++){
appears[i] = false;
}
//Check each one
for (int col = 0; col< a[0].length; col++) {
for (int row = 0; row < a.length; row++){
int num = a[row][col];
if (appears[num-1]) //its easy to forget that -1.
return false; //oops, this number appears twice!
appears[num-1] = true;
}
}
//I could check here that all the values in appears are true but
// since there are 9 positions in both appears and a[][], I know
// that if we get to this point then all the values in appears[]
// must be true!
return true;
}
//showResult
public void showResult(){
if(check())
System.out.println("Matrix is Lo Shu Magic Square");
else
System.out.println("Matrix is not Lo Shu Magic Square");
}
}
// Test class
class Test{
public static void main (String[] args){
//Test 1
int[][] loshu = {{4,9,2},{3,5,7},{8,1,6}};
MagicSquare ms1 = new MagicSquare(loshu);
ms1.showArray();
ms1.showResult();
System.out.println();
int[][] no1 = {{4,9,2},{3,7,5},{8,1,6}};
MagicSquare ms2 = new MagicSquare(no1);
ms2.showArray();
ms2.showResult();
int[][] no2 = {{4,9,2},{3,3,5},{8,1,6}};
System.out.println();
MagicSquare ms3 = new MagicSquare(no2);
ms3.showArray();
ms3.showResult();
}
}
/*
Output:
4 9 2
3 5 7
8 1 6
Matrix is Lo Shu Magic Square
4 9 2
3 7 5
8 1 6
Matrix is not Lo Shu Magic Square
4 9 2
3 3 5
8 1 6
Matrix is not Lo Shu Magic Square
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.