Question
The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The Lo Shu Magic Square has the
The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The Lo Shu Magic Square has the following properties:
The grid contains the numbers 1 through 9 exactly.
The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure 7-32.
In a program you can simulate a magic square using a two-dimensional array. Write a method that accepts a two-dimensional as an argument and determines whether the array is a Lo Shu Magic Square. Test the function in a program.
Figure 7-31 Figure 7-32
As a programmer, your tasks are to design the program in a class diagram, and then implement the design in Java program.
Lo Shu Magic Square Service Class
Name the service class MagicSquare.
The service class must 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 a 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 a Lo Shu magic square or not. These methods are helper functions 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.
Application Class
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.
You can use cases in the following screen shot to test your program:
***Remember to write and test code line-by-line and block-by-block! ***
[MAGIC SQUARE]
/** * Represents a 3x3 square and validates the square as a * Lo Shu Magic Square. */ public class MagicSquare { // Class attribute: 2D integer array /** * Constructor * @param newSquare a 3x3 integer array */ public MagicSquare(int[][] newSquare) { } /** * Print the contents of the square to the screen. */ public void showArray() { } /** * Validate the square as a Lo Shu Magic Square and print the results to the * screen. */ public void showResult() { } /** * Helper function. Ensure that all elements in the square are between 1 and 9 * @return true if all elements of the square are between 1 and 9; false otherwise. */ private boolean validRange() { return true; } /** * Helper function. Ensure that each numbers in the square is unique * @return true if each number in the square is unique; false otherwise. */ private boolean numbersAreUnique() { return true; } /** * Helper function. Ensure that all rows, columns, and diagonals sum to the same value. * @return true if all rows, columns, and diagonals sum to the same value; false otherwise. */ private boolean validSums() { return true; } /** * Helper function. Sum a specific row in the square. * @param row the index of the row to sum. * @return sum of the row. */ private int sumRow(int row) { return total; } /** * Helper function. Sum a specific column in the square. * @param col the index of the column to sum. * @return sum of the column. */ private int sumColumn(int col) { return total; } /** * Helper function. Sum the left diagonal of the square (Upper left to bottom right). * @return sum of the left diagonal. */ private int sumLeftDiagonal() { return total; } /** * Helper function. Sum the right diagonal of the square (upper right to bottom left). * @return sum of the right diagonal. */ private int sumRightDiagonal() { return total; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started