Squarejava is done need help with squareTestjava Squareja
Square.java is done. need help with squareTest.java.
// ***************************************************************
// Square.java
//
// Define a Square class with methods to create and read in
// info for a square matrix and to compute the sum of a row,
// a col, either diagonal, and whether it is magic.
//
// ***************************************************************
import java.util.*;
public class Square {
static int[][] square = new int[3][3];
public int sumRow(int[][] row)
{
// Check each row.
for (int i = 0; i < 3; i++)
{
// Find the sum of row #i.
int sum = 0;
for (int j = 0; j < 3; j++)
sum += row[i][j];
// If this row does not equal 15, then it is not a magic square
if (sum != 15)
return 0;
}
return 1;
}
public int sumCol(int[][] col)
{
// Check each column.
for (int j = 0; j < 3; j++)
{
// Find the sum of column #j.
int sum = 0;
for (int i = 0; i < 3; i++)
sum += col[i][j];
// If this column does not equal 15, then it is not a magic square
if (sum != 15)
return 0;
}
return 1;
}
public int sumMainDiag()
{
// Check forward diagonal.
if (square[0][0] + square[1][1] + square[2][2] != 15)
return 0;
return 1;
}
public int sumOtherDiag()
{
// Check backward diagonal.
if (square[0][2] + square[1][1] + square[2][0] != 15)
return 0;
return 1;
}
public static void main(String[] args) {
Scanner size = new Scanner(System.in);
System.out.println(\"Please enter your magic square.\");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
square[i][j] = size.nextInt();
Square s=new Square();
int a=s.sumRow(square);
int b=s.sumCol(square);
int c=s.sumMainDiag();
int d=s.sumOtherDiag();
if(a==1 && b==1)
{
if(c==1 && d==1)
{
if(magic(square)==1)
{
System.out.println(\"You have a magic square\");
}
else
System.out.println(\"Not a magic square\");
}
else
System.out.println(\"Not a magic square\");
}
else
System.out.println(\"Not a magic square\");
}
private static int magic(int[][] square)
{
int[] freq = new int[10];
for (int i = 1; i < 10; i++)
freq[i] = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (square[i][j] < 1 || square[i][j] > 9)
return 0;
freq[square[i][j]]++;
}
}
for (int i = 1; i < 10; i++)
if (freq[i] != 1)
return 0;
return 1;
}
}
------------------------------------------------------------------------------------------------------------------------------------
// ***************************************************************
// SquareTest.java
//
// Uses the Square class to read in square data and tell if
// each square is magic.
//
// ***************************************************************
import java.util.Scanner;
public class SquareTest
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(new File(\"magicData\"));
int count = 1; //count which square we\'re on
int size = scan.nextInt(); //size of next square
//Expecting -1 at bottom of input file
while (size != -1)
{
//create a new Square of the given size
//call its read method to read the values of the square
System.out.println(\"\ ******** Square \" + count + \" ********\");
//print the square
//print the sums of its rows
//print the sums of its columns
//print the sum of the main diagonal
//print the sum of the other diagonal
//determine and print whether it is a magic square
//get size of next square
size = scan.nextInt();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------
magicData
3
8 1 6
3 5 7
4 9 2
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
4
48 9 6 39
27 18 21 36
15 30 33 24
12 45 42 3
3
6 2 7
1 5 3
2 9 4
4
3 16 2 13
6 9 7 12
10 5 11 8
15 4 14 1
5
17 24 15 8 1
23 5 16 14 7
4 6 22 13 20
10 12 3 21 19
11 18 9 2 25
7
30 39 48 1 10 28 19
38 47 7 9 18 29 27
46 6 8 17 26 37 35
5 14 16 25 34 45 36
13 15 24 33 42 4 44
21 23 32 41 43 12 3
22 31 40 49
2 20 11
-1
Solution
SquareTest.java:
package com.sai;
 import java.util.Scanner;
 import java.io.*;
 public class SqureTest {
  
    public static void main(String[] args) throws IOException
    {
    Scanner scan = new Scanner(new File(\"magicData.txt\"));
   int count = 1;
    int size = scan.nextInt();
   while (size != -1)
    {
        Square s=new Square(size);
s.readSquare(scan);
   System.out.println(\"\ ******** Square \" + count + \" ********\");
   
    s.printSquare();
    System.out.println(\"Row Sums\ Row\\tSum\");
    for(int i=0;i<size;i++)
    System.out.println((i+1)+\"\\t\"+s.sumRow(i));
    System.out.println();
   System.out.println(\"Column Sums\ Col\\tSum\");
    for(int i=0;i<size;i++)
    System.out.println((i+1)+\"\\t\"+s.sumCol(i));
    System.out.println();
   System.out.println(\"Diagonol Sums\ Diagnol\\tSum\");
    System.out.println(\"Main\\t\"+s.sumMainDiag());
System.out.println(\"Other\\t\"+s.sumOtherDiag());
   if(s.magic())
    System.out.println(\"Square is magic\");
    else
    System.out.println(\"Square is not magic\");
    size = scan.nextInt();
    count++;
    }
   }
    }







