Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m in java one and need help with this code. please follow the directions give

ID: 3685710 • Letter: I

Question

I'm in java one and need help with this code. please follow the directions given and test your program to make sure it works as it is required.please use a2 dimentional array . I will apreciate your help. thank you

A magic square is an arrangement of the numbers from 1 to n^2 in an nxn matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. It is not hard to show that this sum must be n(n2+1)/2.

Here is an example 4x4 magic square. Notice that all of the rows add up to 34 [which is (4*(42+1))/2] , all of the columns add up to 34, and both of the main diagonals add up to 34. Also notice that each of the numbers from 1 through 16 is used only once each in the square and that only the numbers 1 through 16 are used.

  

1

2

15

16

13

14

3

4

12

7

10

5

8

11

6

9

For this program, you are going to allow the user to enter the values for a 4x4 magic square and then validate the rows, columns and diagonals of the matrix to see if it is a magic square or not.

Your program will process 4x4 squares, though it should be easy to make your program handle any nxn array. Your program will prompt for the user to enter the four values for each row, row-by-row. Your program should output complete, detailed reasons as to why the square isn’t a magic square.

See the example runs for an idea of input and output.

FINAL OUTPUT

the output from the methods below, as its final output, your program must print one of the following statements on a line by itself, exactly as you see here:

MAGIC: YES

if the square is determined to be a magic square

MAGIC: NO

if the square is determined to not be a magic square

ARRAY REQUIREMENTS

You must use a two-dimensional array to solve this problem. Your program will not be graded if it does not use a two-dimensional array to store the matrix and to determine if the square is magic or not.

METHOD AND OUTPUT REQUIREMENTS

You need to write four methods in addition to the main method. These are checkRows(), checkColumns(), checkDiagonals() and checkRange(). Implement these methods like this:

public static boolean checkRows ( int theSquare[][] )

---This method takes the square to check as a two-dimensional array. The method should first determine what the magic number is based on the number of rows in the array. You can assume that the array passed in is a square array.

---It should check all rows for the magic value and return either true or false depending on if all the rows add up to the magic number.

---Additionally, while checking the rows, output should be done to identify which rows are bad if any are.

---This method should output on one line the text ROWS: (no underlining, obviously) followed by one of the following (see sample output for exact examples):

** A list of the row #s, separated by spaces, that do not add up to the magic number (row #s start at 0)

** The text VALID if all of the rows add up to the magic number.

public static boolean checkColumns ( int theSquare [][] )

---This method takes the square to check as a two-dimensional array. The method should first determine what the magic number is based on the number of rows in the array. You can assume that the array passed in is a square array.

----It should check all columns for the magic value and return either true or false depending on if all the columns add up to the magic number.

----Additionally, while checking the columns, output should be done to identify which columns are bad if any are.

----Since you can assume that the square/array is a square (the array has the exact same number of rows as columns), this may make processing the two-dimensional array to sum up columns easier than if you were working with a jagged array.

----This method should output on one line the text COLS: (no underlining, obviously) followed by one of the following (see sample output for exact examples):

** A list of the column #s, separated by spaces, that do not add up to the magic number (column #s start at 0)

** The text VALID if all of the columns add up to the magic number.

public static boolean checkDiagonals ( int theSquare [][])

----This method takes the square to check as a two-dimensional array. The method should first determine what the magic number is based on the number of rows in the array. You can assume that the array passed in is a square array.

----It should check both diagonals for the magic value and return either true or false depending on if both add up to the magic number.

----Additionally, while checking the diagonals, output should be done to identify which diagonals are bad if any are.

----This method should output on one line the text DIAG: (no underlining, obviously) followed by one of the following (see sample output for exact examples):

** A list of the diagonals, separated by spaces, that do not add up to the magic number (top-left to bottom-right is diagonal 0, bottom-left to top-right is diagonal 1)

** The text VALID if all of the diagonals add up to the magic number.

public static boolean checkRange ( int theSquare [][] )

----This method takes the square to check. It should check for the numbers in the range 1 through theSquare.length * theSquare.length (for a 4x4 magic square, this would result in numbers in the range from 1 to 16) being used once and only once each. The method should return either true or false depending on if the numbers are all used only once or if some are used multiple times or not at all.

----Additionally, while checking the range, output should be done to identify if the uniqueness quality is met.

----This method should output on one line the text RANG: (no underlining, obviously) followed by one of the following (see sample output for exact examples):

** A list of all offending numbers (dupes in 1-16, out-of-range), separated by spaces and listed row by row

** The text VALID if all numbers in the range 1 through 16 are used once and only once.

GOALS

Write a moderately difficult program using 2-dimensional arrays

Points to Think About

Use a 2-dimensional array of integers.

Allow the user to enter all values without checking them as they are input. In other words, allow the user to enter negative numbers, 0, or positive numbers for the numbers of the potential magic square. All of the validation will occur when you check the square's rows, columns, diagonals and range via the methods you write.

Grading Notes

> Your errors/validation output must occur in the order: DIAG, ROWS, COLS, RANG.

> You must have header documentation for all methods and you must follow the Program Coding Standards for this course.

> You must use at least one array to solve this program and it must be declared in the main method. The array must be 2- dimensional and store the values of the potential magic square.

> You may use other arrays as you see fit (a one-dimensional array of counters in checkRange() could be useful, for instance).

> Your program must identify all errors exactly as described in each method description for each potential magic square tested.

tested.

> Your program must clearly identify as described on page one of the assignment, at the end of all other output, whether the square is magic or not.

> Process only one square and have your program end.

sample program run

The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.

Please enter the 4 values for row 0, separated by spaces: 1 2 15 16

Please enter the 4 values for row 1, separated by spaces: 13 14 3 4

Please enter the 4 values for   row 2, separated by spaces: 12 7 10 5

Please enter the 4 values for row 3, separated by spaces: 8 11 6 9

Checking square for problems: DIAG: VALID

Sample Program Run (user input is underlined)

The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.

Please enter the 4 values for row 0, separated by spaces: 1 2 3 4

Please enter the 4 values for row 1, separated by spaces: 5 6 7 8

Please enter the 4 values for row 2, separated by spaces: 9 10 11 12

Please enter the 4 values for row 3, separated by spaces: 13 14 15 16

Checking square for problems:

DIAG: VALID

ROWS: 0 1 2 3

COLS: 0 1 2 3

RANG: VALID

MAGIC: NO


1

2

15

16

13

14

3

4

12

7

10

5

8

11

6

9

Explanation / Answer


/**The java progam MagicSquare that prompts user to enter
* four values in each row separated by space and
* returns if the square matrix is magic square or not*/
//MagicSquare.java
import java.util.Scanner;
public class MagicSquare {

   public static void main(String[] args) {

       Scanner scanner=new Scanner(System.in);
       //create an array of matrix of 4 rows and 4 cols
       int[][] matrix=new int[4][4];
       boolean valid=true;

       System.out.println("The magic value for your square is 34, which means that every row, " +
               "column and diagonal of your square must add up to that number.");


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

           System.out.println("Please enter the 4 values for row "+(i+1)
                   +", separated by spaces: ");
           for (int j = 0; j < matrix[i].length; j++) {
               //read values into the matrix
               matrix[i][j]=scanner.nextInt();
           }
       }

       System.out.print("Checking square for problems: DIAG:");
       if(checkDiagonals(matrix))
           System.out.println("VALID");
       else
       {
           System.out.println("INVALID");
           valid=false;
       }

       System.out.print("ROWS:");
       if(checkRows(matrix))
           System.out.println("VALID");
       else
       {
           System.out.println("INVALID");
           valid=false;
       }

       System.out.print("COLS:");
       if(checkRows(matrix))
           System.out.println("VALID");
       else
       {
           System.out.println("INVALID");
           valid=false;
       }
      
       System.out.print("RANG:");
       if(checkRange(matrix))
           System.out.println("VALID");
       else
       {
           System.out.println("INVALID");
           valid=false;
       }
      
       System.out.print("MAGIC: ");
       if(valid)
           System.out.println("YES");
       else
           System.out.println("NO");
   }

  
   /**The method checkDiagonals sum is 34 returns true
   * otherwise returns the false*/
   public static boolean checkDiagonals ( int theSquare [][]){

       int sum=0;

       int magicNumber=(theSquare.length*(theSquare.length*theSquare.length+1))/2;

       for (int i = 0; i < theSquare.length; i++) {
           sum+=theSquare[i][i];
       }


       if(sum==magicNumber)
           return true;
       else
           return false;

   }

   /**The method checkRows that takes the matrix as input
   * and returns true if the row sum values are equal to 34
   * otherwise return false*/
   public static boolean checkRows ( int theSquare[][] ){

       int sum=0;
       boolean valid=true;

       int magicNumber=(theSquare.length*(theSquare.length*theSquare.length+1))/2;

       for (int i = 0; i < theSquare.length &&valid; i++) {
           for (int j = 0; j < theSquare.length && valid; j++) {

               sum+=theSquare[i][j];                  
           }

           if(sum!=magicNumber)
               valid=false;
           else
               sum=0;
       }
       return valid;

   }
  
   /**The method checkColumns that takes the matrix as input
   * and returns true if the columns sum values are equal to 34
   * otherwise return false*/
   public static boolean checkColumns ( int theSquare [][] ){
       int sum=0;
       boolean valid=true;

       int magicNumber=(theSquare.length*(theSquare.length*theSquare.length+1))/2;

       for (int i = 0; i < theSquare.length &&valid; i++) {
           for (int j = 0; j < theSquare.length && valid; j++) {

               sum+=theSquare[j][i];                  
           }

           if(sum!=magicNumber)
               valid=false;  
           else
               sum=0;
       }
       return valid;

   }
  
   /**The method checkRange that takes matrix and returns
   * true if the values are unique values inthe range
   * of 1 to 16*/
   public static boolean checkRange ( int theSquare [][]){      
       boolean valid=true;
       for (int i = 0; i < theSquare.length &&valid; i++) {
           for (int j = 0; j < theSquare.length && valid; j++) {
               if(count(theSquare[i][j],theSquare)>1)
                   valid=false;
           }
       }
       return valid;
   }

   /**The Helper method that returns the count of a key value in the
   * matrix and returns the count */
   private static int count(int key, int[][] theSquare) {

       int count=0;
       for (int i = 0; i < theSquare.length ; i++) {
           for (int j = 0; j < theSquare.length; j++) {

               if(theSquare[i][j]==key)
                   count++;                              
           }
       }

       return count;
   }
}

------------------------------------------------------------------------------------------------------------------------------------------------

Sample output:

The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.
Please enter the 4 values for row 1, separated by spaces:
1 2 3 4
Please enter the 4 values for row 2, separated by spaces:
5 6 7 8
Please enter the 4 values for row 3, separated by spaces:
9 10 11 12
Please enter the 4 values for row 4, separated by spaces:
13 14 15 16
Checking square for problems: DIAG:VALID
ROWS:INVALID
COLS:INVALID
RANG:VALID
MAGIC: NO

sample output2:

The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.
Please enter the 4 values for row 1, separated by spaces:
1 2 15 16
Please enter the 4 values for row 2, separated by spaces:
13 14 3 4
Please enter the 4 values for row 3, separated by spaces:
12 7 10 5
Please enter the 4 values for row 4, separated by spaces:
8 11 6 9
Checking square for problems: DIAG:VALID
ROWS:VALID
COLS:VALID
RANG:VALID
MAGIC: YES

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote