Question

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

The goal of this program is to output a matrix with all rows, columns and diagonals having the same sum – this is called the Magic Square Matrix. The maximum digit in the matrix should not exceed 9 and all numbers in the matrix are UNIQUE (meaning that each number does not appear more than once in the matrix). You will keep generating matrices and outputting whether it is a Magic Square Matrix or not. Once you output a Magic Square Matrix, you will SAVE the magic square into a text file called output.txt and then, the program will exit.

You will be using a two-dimensional Array for this program.

Example:

User Enters: start

After displaying many 3x3 matrices, a Magic Square is finally found:

2 7 6
9 5 1
4 3 8

Because:

-3 rows and 3 columns and 3 is not even.

-Rows: 2+7+6=15, 9+5+1=15, 4+3+8=15 -

Columns: 2+9+4=15, 7+5+3=15, 6+1+8=15

-Diagonals: 2+5+8=15,6+5+4=15

-All the numbers in the matrix are from :1→3^2 or (9); therefore, it doesn’t exceed 9

You must have the following methods created and used within your program:

1. sumRows(int [][]array) → The method will calculate the sum of the rows in your generated matrix. Return the sum of rows if they are all equal else return -1 if they are not equal.

2. sumColumns(int [][]array)→The method will calculate the sum of the columns in your generated matrix. Return the sum of columns if they are all equal else return -1 if they are not equal.
3. sumDiagonals(int [][]array) → The method will calculate the sum of the diagonals in your generated matrix and will return the sum of the diagonals if they are all equal, else return -1 if they are not equal.

Hint: Diagonals indexes are:

- Diagonal 1: [0][0],[1][1], → [2][2]

- Diagonal 2: [size-1][0], [size-2][1] → [0][2]

4. isUnique(int [][] array, int num) → Checks if num exists in the array. If num exists in the array, return false. If it does not exist in the array, return true.

5. displayMagicSquare(int [][] array) → Prints the two-dimensional Array.

6. fillMatrix(int [][] array) → Fills the two dimensional Array with Unique random numbers from 1 → 9 And returns the array.

7. isMagicSquare(int [][]array) → returns true only if:

- The array has sum of its rows equal to its columns and equal to its diagonals

-Rows, columns, and diagonals’ sums are above 0.

8. saveArray(int [][] array) → void method but saves the array to a file

To store something to a file, you will need to import the PrintWriter object. Look at the Java API on how to import printwriter object.

The code to save a 2d array to a file is as follows:

PrintWriter pr = new PrintWriter("output");

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

for (int j=0; j<array.length ; j++){

pr.print (array[i][j]);

}

pr.println();

}

pr.close();
You are also free to use any additional methods if you choose to.
TIP:

If you get stuck, output your variables OR Use Debugging. You might be holding wrong numbers in your variables!

-Make sure you reset your matrix to all 0’s if your matrix is not a Magic Square before calling fillMatrix() again!















Sample Run:

A Magic Square is a matrix in which all rows, columns, and diagonals, when summing its integer elements, are equal.

Enter start to begin the search! start

8    1 3

4    2 9

7    6 5

Sum of Rows:-1

Sum of Columns: -1

Sum of Diagonals: -1

Matrix is not a Magic Square

9    8 1

3    4 2

5    7 6

Sum of Rows:-1

Sum of Columns: -1

Sum of Diagonals: -1

Matrix is not a Magic Square

2 3 8

7 4 9

1 5 6

Sum of Rows:-1

Sum of Columns: -1

Sum of Diagonals: -1

Matrix is not a Magic Square

3 7 5

6 8 9

2 1 4

Sum of Rows:-1

Sum of Columns: -1

Sum of Diagonals: -1

Matrix is not a Magic Square
5 6 1

8 3 7

9 4 2

Sum of Rows:-1

Sum of Columns: -1

Sum of Diagonals: -1

Matrix is not a Magic Square

8 6 1

2 4 9

5 7 3

Sum of Rows:

15 Sum of Columns:

15 Sum of Diagonals:

15 Matrix is a Magic Square!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// MagicSquare.java

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

public class MagicSquare {

   public static void main(String[] args) {

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       System.out
               .println("A Magic Square is a matrix in which all rows, columns, and diagonals, when summing its integer elements, are equal.");

       System.out.println("Enter start to begin the search! ");
       String mesg = sc.next();

       if (mesg.equalsIgnoreCase("Start")) {
           while(true)
           {
               int array[][] = new int[3][3];
               fillMatrix(array);
               displayMagicSquare(array);
               int rowSum = sumRows(array);
               System.out.println("Sum of Rows:" + rowSum);
               int colSum = sumColumns(array);
               System.out.println("Sum of Columns:" + colSum);
               int diagSum=sumDiagonals(array);
               System.out.println("Sum of Diagonals:" + diagSum);
              
               if(rowSum==-1 && colSum==-1 && diagSum==-1)
               {
                   System.out.println("Matrix is not a Magic Square");
               }
               else if(rowSum==colSum && colSum==diagSum && diagSum==rowSum)
               {
                   System.out.println("Matrix is a Magic Square");
                   PrintWriter pr;
                   try {
                       pr = new PrintWriter("output.txt");
                       for (int i=0; i<array.length ; i++){

                           for (int j=0; j<array.length ; j++){

                           pr.print (array[i][j]+" ");

                           }

                           pr.println();

                           }

                           pr.close();
                   } catch (FileNotFoundException e) {
                  
                       e.printStackTrace();
                   }

                   break;
                  
               }
           }  
           }
          

   }

   private static int sumDiagonals(int[][] array) {
       int sumDiag[]=new int[2];
   sumDiag[0] = 0;
   int sum=0;
   // calculating the Sum of Diagonal0
   for (int row = 0; row < 3; row++)
   {
   sumDiag[0] += array[row][row];
   sum=sumDiag[0];
   }
  
   sumDiag[1] = 0;
   // calculating the Sum of Diagonal 1
   for (int row = 0; row < 3; row++)
   {
   sumDiag[1] += array[row][3 - 1 - row];
   }
  
   if(sumDiag[0]==sumDiag[1])
   return sum;
   else
       return -1;
   }

   private static int sumColumns(int[][] array) {
       int col[] = new int[3];
       int sum = 0;
       for (int i = 0; i < array[0].length; i++) {
           sum = 0;
           for (int j = 0; j < array.length; j++) {
               sum += array[j][i];
           }
           col[i] = sum;
       }

       boolean bool = true;
       for (int i = 0; i < 3; i++) {
           bool = bool && (sum == col[i]);
       }
       if (bool)
           return sum;
       else

           return -1;
   }

   private static int sumRows(int[][] array) {
       int row[] = new int[3];
       int sum = 0;
       for (int i = 0; i < array.length; i++) {
           sum = 0;
           for (int j = 0; j < array[0].length; j++) {
               sum += array[i][j];
           }
           row[i] = sum;
       }

       boolean bool = true;
       for (int i = 0; i < 3; i++) {
           bool = bool && (sum == row[i]);
       }
       if (bool)
           return sum;
       else

           return -1;
   }

   private static void displayMagicSquare(int[][] array) {
       for (int i = 0; i < array.length; i++) {
           for (int j = 0; j < array[0].length; j++) {
               System.out.print(array[i][j] + " ");
           }
           System.out.println();
       }

   }

   private static void fillMatrix(int[][] array) {
       // Creating a random Class object
       Random r = new Random();
       for (int i = 0; i < array.length; i++) {
           for (int j = 0; j < array[0].length; ) {
               int num = r.nextInt(9) + 1;
               if (isUnique(array, num)) {
                   array[i][j] = num;
                   j++;
               }
           }
       }

   }

   private static boolean isUnique(int[][] array, int num) {
       for (int i = 0; i < array.length; i++) {
           for (int j = 0; j < array[0].length; j++) {
               if (array[i][j] == num)
                   return false;
           }
       }
       return true;
   }

}

__________________________

Output:

4 8 1

2 9 5

3 6 7

Sum of Rows:-1

Sum of Columns:-1

Sum of Diagonals:-1

Matrix is not a Magic Square

1 3 7

8 9 6

2 4 5

Sum of Rows:-1

Sum of Columns:-1

Sum of Diagonals:-1

Matrix is not a Magic Square

3 4 8

2 5 6

1 7 9

Sum of Rows:-1

Sum of Columns:-1

Sum of Diagonals:-1

Matrix is not a Magic Square

8 1 6

3 5 7

4 9 2

Sum of Rows:15

Sum of Columns:15

Sum of Diagonals:15

Matrix is a Magic Square

____________________________

Output.txt

8 1 6
3 5 7
4 9 2

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Use basic java for this after importing PrintWriter object You will make a simple Magic Square...
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
  • 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...

  • please use java language please used ArrayList The Lo Shu Magic Square is a grid with...

    please use java language please used ArrayList 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, and each diagonal all add up to the same number 20. Lo Shu Magic Square Lo Shu Magic Square has the following properties: • The grid contains the numbers 1 through 9 exactly. This is shown in Figure 7-32. In a program you can simulate a...

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

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

  • how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagona...

    how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagonals have the same sum. For example, A- 3 5 7 4 9 2 Complete the following magic matrices 17? ?? 3 ? 2 ? 2? ? Do the following steps in each case: 1. Write the system of equations and put it under the...

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

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

  • Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement...

    Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement of the numbers 1, 2, ., N2, in which the sum of rows, columns, and diagonals are the same. The users (i.e., TAs) will specify the size of the square matrix: N. The value N must be an odd number between 3 and 15. Example Run For example, you program is expected to run as the following way. NOTE: We only list 5 magic...

  • 8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic...

    8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic square if the sum of the elements in each row, in each column, and in the two main diagonals is the same value. For example, 16 3 213 5 10 11 8 9 6 7 12 4 15 14 1 Write the program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4...

  • Magic Square question for Python

    You have this solution in Java, I am interested in this same solution for Python.One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and bothdiagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code todetermine whether a square is magic.You should find that the first, second, and...

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