LoShuMagicSquare.java:
import java.util.ArrayList;
import java.util.Scanner;
public class LoShuMagicSquare {
/**
* fill magic square arrayList
* @param magicSquare array List to be filled
*/
static void fillMagicSquare(ArrayList<ArrayList<Integer>> magicSquare) {
int i = magicSquare.size() / 2;
int j = magicSquare.size() - 1;
for (int number=1; number<=magicSquare.size() * magicSquare.size();) {
if (i == -1 && j == magicSquare.size()) {
j = magicSquare.size() - 2; i = 0;
} else {
j = (j == magicSquare.size()) ? 0 : j; // if column exceed the column size
i = (i < 0) ? (magicSquare.size() - 1) : i; // if row is negative set row to arraySize - 1
}
if (magicSquare.get(i).get(j) != 0) { // if current position is already filled
j -= 2; i++;
} else {
magicSquare.get(i).set(j, number);
j++; i--; number++;
}
}
}
/**
* print magic square method
* @param magicSquare arraylist of magic square
*/
static void printMagicSquare(ArrayList<ArrayList<Integer>> magicSquare){
for (int i=0; i<magicSquare.size(); i++){
for (int j=0; j<magicSquare.size(); j++){
System.out.print(magicSquare.get(i).get(j) + " ");
}
System.out.println();
}
}
public static void main(String... args){
ArrayList<ArrayList<Integer>> magicSquare = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.printf("Enter size of array: ");
int n = sc.nextInt();
if (n % 2 == 0){
System.out.println("please enter an odd number: ");
return;
}
// fill each element of array by 0
for (int i=0; i<n; i++){
magicSquare.add(new ArrayList<>());
for (int j=0; j<n; j++){
magicSquare.get(i).add(0);
}
}
// fill magic square
fillMagicSquare(magicSquare);
// print magic square
printMagicSquare(magicSquare);
}
}
Output:

please use java language please used ArrayList The Lo Shu Magic Square is a grid with...
9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that 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 the following figure · 15 4 9 2-15 3 5 7-15 81 615 15 15 15 15 Write a program that simulates a magic square using...
I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. 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...
I need help as quick as possible, thanks beforehand. Please
provide the test output
The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...
Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...
Concepts tested by the program:
Working with one dimensional parallel arrays
Use of functions
Use of loops and conditional statements
Description
The Lo Shu Magic Square is a grid with 3 rows and 3 columns
shown below.
The Lo Shu Magic Square has the following properties:
The grid contains the numbers 1 – 9 exactly
The sum of each row, each column and each diagonal all add up
to the same number.
s is shown below:
Write a program that...
Java Magic Square:
A n x n matrix that is filled with the numbers 1, 2, 3,.... n^2 is
a magic square if the sum of the elements in each row, in each
column, and in the two diagonals is the same value.
I need to write an application that gets 16 values as user input,
in any order. Store these values in an ArrayList. When the numbers
are put into a square, this would be a 4x4 two-dimensional
array....
Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum ofthe elements in each row, in each column, and in the two diagonals is the same value. For example,16 3 2 135 10 11 89 6 7 124 15 14 1Write a program that reads in n2 values from the keyboard and tests whether they form a magic squarewhen arranged as a square matrix. You...
Write a Python program that generate randomly a magic square of 3 x 3 elements. For example: 4 3 8 9 5 1 2 7 6 = Matrix1 Matrix 1 above is an example of magic square. The rows total, the columns total, and the diagonal totals are all 15. Your program should randomly generate a 3x3 matrix. Check if it is a magic square. If it is a magic square, then print some information and quit. If the...
C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...
// Use the following
Project: Perfect Square Table
A “Perfect Square Table” is a square of positive
integers such that the
sum of each row, column, and diagonal is the same
constant.
This program reads square tables from files, checks if
they are perfect squares,
and displays messages such as “This is a Perfect
Square Table with a constant of 34!”
or “This is not a Perfect Square Table”.
NAME:
IDE:
*/...