HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

"Magic Squares" assignment

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
assignmentmagicsquares

Problem

The assignment is to take a "Magic Square" .txt file (where the horizontal lines, vertical lines and diagonal line all add up to the same number), read it and prove that they are in fact magic squares.

Is anything in this answer bad practice or just bad? I'm using a lot of Lists and for loops.

```
package magicSquares;

public MagicSquaresCormac() {
return;
}

private List> getNumberList(String file) throws IOException {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

String line = null;
Integer num = null;
List lineList = new ArrayList<>();
List charList;
List> listOfNums = new ArrayList<>();
List numlist;
List> numListList = new ArrayList<>();

while ((line = br.readLine()) != null) {
line.split("\t");
if (!line.equals("")) {
lineList.add(line);
}
}

for (String lineFromList : lineList) {
// lineFromList.split(" ");
numlist = new ArrayList<>();
charList = Arrays.asList(lineFromList.split("\\s"));
listOfNums.add(charList);
for (String character : charList) {
num = Integer.valueOf(character);
numlist.add(num);
}
numListList.add(numlist);
}

br.close();
return numListList;
}

private boolean horizontalLine(String file) throws IOException {
List> numListList = getNumberList(file);
boolean isMagic = true;
Integer lastSum = -1;
Integer sum;

for (List listNum : numListList) {
sum = 0;
for (Integer i : listNum) {
sum = sum + i;
}
if (lastSum == -1)
lastSum = sum;

else if (!lastSum.equals(sum)) {
isMagic = false;
}
}

return isMagic;
}

private boolean diagonalLine(String file) throws IOException {
List> numListList = getNumberList(file);
boolean isMagic = true;
Integer sum;
Integer sumDiagonal = 0;

for (int j = 0; j >

Solution

I suspect that you should read the matrices from files only once and store them in a two-dimensional integer array. Once you have that, it is trivial to verify whether the squares are magic.

I had in mind something like this:

private static int[][] loadFile(File file) {
     // Do file I/O for loading the square
     return square;
}

private static int getRowMagic(int[][] square, int row) {
    int sum = 0;

    for (int column = 0; column < square.length; ++column) {
        sum += square[row][column];
    }

    return sum;
}

private static int getColumnMagic(int[][] square, int column) {
    int sum = 0;

    for (int row = 0; row < square.length; ++row) {
        sum += square[row][column];
    }

    return sum;
}

private static int getDescendingDiagonalMagic(int[][] square) {
    int sum = 0; 

    for (int i = 0; i < square.length; ++i) {
        sum += square[i][i];
    }

    return sum;
}

private static getAscendingDiagonalIsMagic(int[][] square) {
    int sum = 0;

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

    return sum;
}

public static boolean isMagicSquare(int[][] square) {
    int sum = getRowMagic(square, 0);

    for (int i = 0; i < square.length; ++i) {
        if (sum != getRowMagic(square, i) || sum != getColumnMagic(square, i)) {
            return false;
        }
    }

    return getAscendingDiagonalMagic(square) == sum &&
           getDescendingDiagonalMagic(square) == sum;
}


Hope that helps.

Code Snippets

private static int[][] loadFile(File file) {
     // Do file I/O for loading the square
     return square;
}

private static int getRowMagic(int[][] square, int row) {
    int sum = 0;

    for (int column = 0; column < square.length; ++column) {
        sum += square[row][column];
    }

    return sum;
}

private static int getColumnMagic(int[][] square, int column) {
    int sum = 0;

    for (int row = 0; row < square.length; ++row) {
        sum += square[row][column];
    }

    return sum;
}

private static int getDescendingDiagonalMagic(int[][] square) {
    int sum = 0; 

    for (int i = 0; i < square.length; ++i) {
        sum += square[i][i];
    }

    return sum;
}

private static getAscendingDiagonalIsMagic(int[][] square) {
    int sum = 0;

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

    return sum;
}

public static boolean isMagicSquare(int[][] square) {
    int sum = getRowMagic(square, 0);

    for (int i = 0; i < square.length; ++i) {
        if (sum != getRowMagic(square, i) || sum != getColumnMagic(square, i)) {
            return false;
        }
    }

    return getAscendingDiagonalMagic(square) == sum &&
           getDescendingDiagonalMagic(square) == sum;
}

Context

StackExchange Code Review Q#122743, answer score: 6

Revisions (0)

No revisions yet.