Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise #5 There is a problem. We have not checked the diagonals to ensure they add up to the magic constant as well. For example,

Exercise #5

There is a problem. We have not checked the diagonals to ensure they add up to the magic constant as well. For example, if we add this square to the text file:

5 23 6 19 2 15 11 24 7 20 3 4 12 25 8 16 17 5 13 21 9 10 18 1 14 22 

the program would report it as a magic square, even though it is not. The magic constant is 65 but the sums of the diagonals 23+24+25+21+22=115 and 15+20+25+5+10=75 are both incorrect.

Download the complete magic squares program and add the following features to it:

Write a method checkDiagonals that sums both diagonals and compares them to the magic constant. Like the other check methods, it should count and print the number of invalid diagonals (or zero if none) and return a boolean value of true on success. Hint: one loop counter can be used to move along both dimensions of the array, so you do not need nested loops.

Modify the check method to check the diagonals.

Submit your checkDiagonals and check methods (and no other parts of the program) by the due date specified in the course schedule.

import java.io.*; public class Activity5D { public static void main(String[] args) { processSquaresFromFile("squares.txt"); System.out.println(" End of processing."); } public static void processSquaresFromFile(String filename) { BufferedReader inputFile; String firstLine; int[][] square; try { inputFile = new BufferedReader(new FileReader(filename)); firstLine = inputFile.readLine(); while (firstLine != null) { square = readSquare(inputFile, firstLine); printSquare(square); check(square); firstLine = inputFile.readLine(); } inputFile.close(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); ioe.printStackTrace(); } } public static int[][] readSquare(BufferedReader inputFile, String firstLine) throws IOException { String[] split; String inputLine; int[][] square; int squareSize; squareSize = Integer.parseInt(firstLine); System.out.println(" Size: " + squareSize); square = new int[squareSize][squareSize]; for (int row = 0; row < squareSize; row++) { inputLine = inputFile.readLine(); System.out.println(inputLine); split = inputLine.split("\\s+"); for (int col = 0; col < squareSize; col++) { square[row][col] = Integer.parseInt(split[col]); } } return square; } public static void printSquare(int[][] magicSquare) { for (int row=0; row < magicSquare.length; row++) { for (int col=0; col < magicSquare[row].length; col++) { System.out.print(magicSquare[row][col] +"\t"); } System.out.println(); } } public static void check(int[][] array) { boolean overall; boolean rows, columns; boolean values; int magicConstant; if (!isSquare(array)) { System.out.println("The array is not square."); overall = false; } else { magicConstant = magicConstant(array); System.out.println("The magic constant is " + magicConstant); rows = checkRows(array, magicConstant); columns = checkColumns(array, magicConstant); values = checkValues(array, magicConstant); overall = rows && columns && values; } if (overall) { System.out.println("This is a magic square."); } else { System.out.println("This is NOT a magic square."); } } public static boolean isSquare(int[][] array) { boolean result; if (array.length == 0) { result = true; } else { result = array.length == array[0].length; } return result; } public static int magicConstant(int[][] square) { return (square.length * (square.length * square.length + 1)) / 2; } public static boolean checkRows(int[][] square, int magicConstant) { int length; int wrong; int sum; length = square.length; wrong = 0; for (int row = 0; row < length; row++) { sum = 0; for (int col = 0; col < length; col++) { sum += square[row][col]; } if (sum != magicConstant) { wrong++; } } System.out.println("There were " + wrong + " invalid rows."); return wrong == 0; } public static boolean checkColumns(int[][] square, int magicConstant) { int length; int wrong; int sum; length = square.length; wrong = 0; for (int col = 0; col < length; col++) { sum = 0; for (int row = 0; row < length; row++) { sum += square[row][col]; } if (sum != magicConstant) { wrong++; } } System.out.println("There were " + wrong + " invalid columns."); return wrong == 0; } public static boolean checkValues(int[][] square, int magicConstant) { int[] counts; int length; int wrong; length = square.length; counts = new int[length * length + 1]; wrong = 0; for (int row = 0; row < length; row++) { for (int col = 0; col < length; col++) { if (square[row][col] < 1 || square[row][col] > length * length) { wrong++; } else { counts[square[row][col]]++; } } } for (int i = 1; i <= length * length; i++) { if (counts[i] > 1) { wrong++; } } System.out.println("There were " + wrong + " repeated or invalid values."); return wrong == 0; } } 

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

LO2 Explain the nature of the psychological contract.

Answered: 1 week ago

Question

LO1 Discuss four different views of motivation at work.

Answered: 1 week ago