1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Dvinal [7]
3 years ago
7

Write a class called MagicSquare, which contains a single method called check that accepts a two-dimensional array as an argumen

t, and determines whether the array is a Magic Square. Write a class called MagicSquareDemo, which demonstrates the above class/method.
Computers and Technology
1 answer:
ira [324]3 years ago
5 0

Answer:

public class MagicSquare {

   public static void main(String[] args) {

       int[][] square = {

               { 8, 11, 14, 1},

               {13, 2, 7,12},

               { 3, 16, 9, 6},

               {10, 5, 4, 15}

       };

       System.out.printf("The square %s a magic square. %n",

               (isMagicSquare(square) ? "is" : "is not"));

   }

   public static boolean isMagicSquare(int[][] square) {

       if(square.length != square[0].length) {

           return false;

       }

       int sum = 0;

       for(int i = 0; i < square[0].length; ++i) {

           sum += square[0][i];

       }

       int d1 = 0, d2 = 0;

       for(int i = 0; i < square.length; ++i) {

           int row_sum = 0;

           int col_sum = 0;

           for(int j = 0; j < square[0].length; ++j) {

               if(i == j) {

                   d1 += square[i][j];

               }

               if(j == square.length-i-1) {

                   d2 += square[i][j];

               }

               row_sum += square[i][j];

               col_sum += square[j][i];

           }

           if(row_sum != sum || col_sum != sum) {

               return false;

           }

       }

       return d1 == sum && d2 == sum;

   }

}

You might be interested in
Your help will help me understand my answers by comparing to yours. Your kind contribution is very much appreciated.
OLEGan [10]
4. B
for # 5 what illustration are u talking about
6. D
7. D
8. don't understand that question

sorry these questions might be wrong

6 0
3 years ago
Write the working of dot matrix printer?<br><br>no links<br>​
lbvjy [14]

The working of dot matrix printer is provided in the picture.

4 0
3 years ago
Pros and cons of toyota's organizational structure?
GarryVolchara [31]
In 2013, Toyota changed its organizational structure from the centralized structure to: 
- the Global hierarchy, 
- the Geographic divisions, and 
- the Product-based divisions. 

This change was made to adapt the consumer's demand in each of the regional markets all over the world. The most important element of this structure is the speed of handling issues and problems of all Toyota's branches. However, this structure also has a weakness which is the decreasing of headquarter's control over the global organization.
4 0
4 years ago
Which practice is the best option for desktop security?
ziro4ka [17]
You should make a unique password and change it daily
6 0
3 years ago
Read 2 more answers
Which of the following statements is true of WANs?
vovangra [49]
C is the correc answer. This is because WANs stand for wide area networks.
5 0
3 years ago
Read 2 more answers
Other questions:
  • - The __________ is called a single-selection statement.
    10·1 answer
  • The design activity key question, "how will this system interact with other systems..." is part of which design activity?​
    7·1 answer
  • Information storage:Select one:a. Eliminates the need for professional judgment.b. Keeps data in a form accessible to informatio
    9·1 answer
  • ________ reality is the addition of digital information directly into our reality, either to add more detail or to remove unwant
    7·1 answer
  • With a two-dimensional array, the ____ field holds the number of rows in the array.
    6·1 answer
  • Translation of a file into a coded format that occupies less space than the original file is called
    15·1 answer
  • The LPN/LVN cares for an older client admitted to the hospital for treatment of a fractured femur. The LPN/LVN notes the client
    10·2 answers
  • According to a case study in one of our weekly chapter reading, nearly..................... percent of all employees send work e
    11·2 answers
  • In which situations would it be most helpful to filter a form? Check all that apply.
    8·1 answer
  • Select the correct text in the passage.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!