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

site at access the book\'s companion Web can site TIP: Read the cont of the Winn

ID: 3824713 • Letter: S

Question


site at access the book's companion Web can site TIP: Read the cont of the Winners.txt file into a list. When the n enters the name a team, the program should step through the list, countin the ber of times the selected team appears. user 11. Lo Shu Magic Square and 3 columns, shown in Figure 7.8. The Lo Shu Magic square is a grid with 3 rows Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. all add up to the same n The sum of each row, each column, and each diagonal This is shown in Figure 7-9. In a program you can simulate a magic square using a two-dimensional list. Write a func. accepts a two-dimensional list as an argument and determines whether the list is a Lo Shu Magic Square. Test the function in a program.

Explanation / Answer

LoShuMagicSquare.java

import java.util.Scanner;

public class LoShuMagicSquare {

   public static void main(String[] args) {
   int value;
   int square[][]=new int[3][3];
  
   //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
  
   //here This Nested loop will take the user given values and fill the two-dimensional array
   for (int row = 0; row < 3; row++)
   {
   for (int col = 0; col < 3; col++)
   {
   System.out.print("Input value for row "+ (row+1) + " column " + (col+1)+"::");
   value=sc.nextInt();
   square[row][col] = value;
   }
   }
   //Calling The Function by passing the 2- Dimensional array
   boolean bool=checkLoShuMagicSquare(square);
   if(bool)
   {
   System.out.println(":: This is a Lo Shu Magic Square ::");
   }
   else
   {
       System.out.println(" :: This is not a Lo Shu Magic Square :: ");
   }


   }

   private static boolean checkLoShuMagicSquare(int[][] square) {
      
   //Creating arrays and declaring variables
   int sumDiag[]=new int[2];
   int sumCol=0,sumRow=0;
   int roww[]=new int[3];
   int coll[]=new int[3];
  
  
   //Calculating the sum of each row
   for (int row = 0; row < 3; row++)
   {
   sumRow=0;
   for (int col = 0; col < 3; col ++)
   {
   sumRow+= square[row][col];
   roww[row]=sumRow;
   }
  
   //Displaying the sum of each row
   System.out.println("sum row "+row+"::"+sumRow );
   }
  
  
   //calculating the Sum of Each column
   for (int col = 0; col < 3; col++)
   {
   sumCol=0;
   for (int row = 0; row < 3; row ++)
   {
   sumCol += square[row][col];
   coll[col]=sumCol;
   }
   //Displaying the sum of each column
   System.out.println("sum columns "+col+"::"+sumCol);
   }
  
   sumDiag[0]=0;
   //calculating the Sum of Diagonal0
   for (int row = 0; row < 3; row++)
   {
   sumDiag[0] += square[row][row];
   }
  
   //Displaying the sum of elements in the diagonal0
   System.out.println("sum diagonal 0 "+"::"+sumDiag[0]);
  
   sumDiag[1]=0;
   //calculating the Sum of Diagonal 1
   for(int row = 0; row < 3; row++)
   {
   sumDiag[1] += square[row][3 - 1 - row];
   }
  
   //Displaying the sum of elements in the diagonal1
   System.out.println("sum diagonal 1 " + "::" +sumDiag[1]);
  
   /* checking whether sum of elements in each row,each column and each diagonal are equal or not
   * If yes, The square array is as LoShu Magic Square
   * if not, The square array is not LoShu Magic Square
   */
     
   //if (sumRow==sumCol && sumCol==sumDiag[0] && sumDiag[0]==sumDiag[1] && sumDiag[1]==sumRow) {
   if (roww[0]==roww[1] && roww[1]==roww[2] && coll[0]==coll[1] && coll[1]==coll[2] && roww[0]==sumDiag[0] && sumDiag[0]==sumDiag[1] && sumDiag[1]==coll[0])
   return true;
   else
   return false;

   }

}

___________________

Output:

Input value for row 1 column 1::4
Input value for row 1 column 2::9
Input value for row 1 column 3::2
Input value for row 2 column 1::3
Input value for row 2 column 2::5
Input value for row 2 column 3::7
Input value for row 3 column 1::8
Input value for row 3 column 2::1
Input value for row 3 column 3::6
sum row 0::15
sum row 1::15
sum row 2::15
sum columns 0::15
sum columns 1::15
sum columns 2::15
sum diagonal 0 ::15
sum diagonal 1 ::15
:: This is a Lo Shu Magic Square ::

________Thank You