Question

The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The • The sum of each row, each column, an
please use java language
please used ArrayList
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Enter size of array: 3 2 7 6 9 5 1 4 3 8

Add a comment
Know the answer?
Add Answer to:
please use java language please used ArrayList The Lo Shu Magic Square is a grid with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that h...

    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...

    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...

    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...

  • Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of...

    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...

  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    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...

  • Java Magic Square: A n x n matrix that is filled with the numbers 1, 2,...

    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; Java code

    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...

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    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...

    // 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:    */...

  • Java language only **Mandatory Rules** Put Descriptive comments on the code displayed on top of previous...

    Java language only **Mandatory Rules** Put Descriptive comments on the code displayed on top of previous comments alreadys displayed On Top of Code write summary of what code will do in comments At the end of the code have the output in comment form I Will thumbs up good Work thanks! public class MagicSquare { static int[][] createMagicSquare(int square[][]) { // Initialize position for 1 int i = 3/2; int j = 3-1; // One by one put all values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT