Question

Help pls! and kindly explain how you do this as well. Thaank you Write a program...

Help pls! and kindly explain how you do this as well. Thaank you

Write a program to test whether a square is a 3x3 magic square. A magic square is a grid with 3 rows and 3 columns, like the figure below. A magic square has the following properties:

the grid contains only the numbers 1 through 9

the sum of each row, each column, and each diagonal all add up to the same number

Notes:

I have provided the start to the program, which creates and displays the square.

public class MagicSquare {

   public static void main(String[] args) {
       int[][] magicSquare = { {4,9,2}, {3,5,7}, {8,1,6} };
       printSquare(magicSquare);
       System.out.println("Magic Square? " + isMagic(magicSquare));
   }

   public static boolean isMagic(int[][] square) {
       // YOUR CODE HERE
       return false;
   }
  
   public static void printSquare(int[][] square) {
       for(int rows=0; rows<square.length; rows++) {
           for(int cols=0; cols<square[rows].length; cols++) {
               System.out.print(square[rows][cols] + " ");
           }
           System.out.println();
       }
   }
}

You will complete the missing method: isMagic.

You should try different test values to make sure your program works properly.

Your program only has to work for 3x3 squares. It does not need to work more generally for other-sized magic squares.

If you are going to hard-code in positions to take advantage of this, for full credit, use constants instead of numbers to improve readability.

For example, square[1][2] isn't as clear as square[MIDDLE_ROW][RIGHT_COL].

180px-Magicsquareexample.svg.png

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Help pls! and kindly explain how you do this as well. Thaank you Write a program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 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...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • IN C++ Write a program that uses a 3x3 array and randomly places each integer from...

    IN C++ Write a program that uses a 3x3 array and randomly places each integer from 1 to 9 into the nine squares. The program calculates the "magic number" by adding all the numbers in the array and then dividing the sum by 3. The 3x3 array is said to be a "magic square" if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must use at least the following functions:...

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

  • Java 1. Can you explain it how it will be printed step by step? Thanks!! What...

    Java 1. Can you explain it how it will be printed step by step? Thanks!! What is printed by the following? for (int i=1; i<4; i++) { for (char c=’a’; c<=’c’; c++) { if (i%2==0) { i++; System.out.println(i + " " + c); } else { c++; System.out.println(c + " " + i); } } } 2. Is there a compiler error in this code? If so, what is it? If not, what’s printed? Is there a compiler error in...

  • KINDLY Describe the approach (in plain English) used to completing the coding below and explain the major decisions made...

    KINDLY Describe the approach (in plain English) used to completing the coding below and explain the major decisions made in designing the program. As part of your explanation, be sure to identify the Java constructs you used that are specific and relevant to the program below. Depending on the program, these may include the mechanisms for output, input, selection statements, loops, methods, and so forth. ***********BEGINNING OF CODE*************** import java.util.*; public class CountOccurances{ public static void main(String[] args) { Scanner...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

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

  • kindly Explain, briefly IN PLAIN ENGLISH, how this exercise was completed and the algorithm you used...

    kindly Explain, briefly IN PLAIN ENGLISH, how this exercise was completed and the algorithm you used /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ // package u10a1_ooconsoleregisterforcourse; import java.util.Scanner; /** * * @author omora */ public class U10A1_OOConsoleRegisterForCourse { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO...

  • C++ there is an issue with my if/else statements, I have tried several different ways to...

    C++ there is an issue with my if/else statements, I have tried several different ways to make it match the instructions but each time i get different errors. Need help geting this to match the instructions peoperly. *******************instructions***************************** Function: nextGeneration This function has no parameters. This function returns an int. The purpose of this function is to modify the grid to represent the next generation. Here's how you are supposed to do that for this assignment: Set each element of...

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