Program Specification: An n x n matrix that is filled with the whole numbers 1,
ID: 3663404 • Letter: P
Question
Program Specification: An n x n matrix that is filled with the whole 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 diagonals is the same value. Here is a magic square where n = 3: 8 1 6 3 5 7 4 9 2 Write a program that reads n2 numbers from standard input and tests whether they form a magic square when put into matrix form. The value of n is NOT an input to the program; n must be determined from the number of inputs. For example, the input that creates the example matrix above is 8 1 6 3 5 7 4 9 2. The output is a single word, "true" if the input produces a magic square, "false" otherwise. Your program may assume that each input token is a whole number. The program must verify: 1. The proper number of input values was provided. 2. Each of the numbers between 1 and n2 occurs exactly once in the input. 3. When the numbers are arranged in a matrix, the sum of the rows, columns, and diagonals must be the same value.
Explanation / Answer
Important: Do NOT add static void main(String args) method.use another class with main to test your class.
Here's my driver program: another class with main to test your class, YOU CAN ALSO INCLUDE YOUR OWN MAIN CLASS INSTEAD OF MINE CLASS.
// ****************************************************************
// SquareTest.java
//
// Uses the Square class to read in square data and tell if
// each square is magic.
//
// ****************************************************************
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
public class SquareTest {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(new File("isMagicData"));
// make sure that the file magicData is in the current directory
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
Square s = new Square(size);
// call its read method to read the values of the square
System.out.println(" ***** Square " + count + " *****");
// print the square
s.printSquare();
for (int row = 0; row < size; row++) {
System.out
.println("Sum of row " + row + "is: " + s. fullSquare());
}
System.out.println();
// print the sum of the main diagonal
System.out.println("The sum of the main diagonal is: "
+ s. diagonalRight ());
System.out.println();
// print the sum of the other diagonal
System.out.println("The sum of the other diagonal is: "
+ s. diagonalLeft());
System.out.println();
// determine and print whether it is a magic square
System.out.println("Is it a magic square: " + s.isMagic());
// get size of next square
size = scan.nextInt();
count++;
}
}
}
Here's my class file:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Square
{
private int [][] sq;
private int position = 0;
public boolean fullSquare()
{
int sum=0;
int numcheck = 1;
boolean found = false;
while (numcheck < sq.length*sq.length)
{
for(int i = 0; i < sq[0].length; i++)
{
for(int j = 0; j < sq[0].length; j++)
{
sum = sum + sq[j][i];
}
}
}
//use nested for loops to loop through array sq.
//if the value in sq == numcheck, set found to true
//After the loop, check to see if numcheck was found
//if not found, return false, otherwise set found to false
//and increment numcheck to be ready to check the next number
return true;
}
public boolean isMagic()
{
int targetNum = 0;
boolean stuff = false;
for (int c = 0; c < sq[0].length; c++)
{
targetNum += sq[0][c];
}
if(rowAndColumn (targetNum) == true && diagonalLeft(targetNum) == true && diagonalRight(targetNum) == true && fullSquare() == true)
{
return true;
}
else
return false;
//if the rows, columns, diagonals and fullSquare are all
//true, return true, otherwise, return false.
}
public Square(int size)
{
sq = new int[size][size];
}
/**
* adds value to the matrix at the given position, row,col
*/
public void add(int value, int row, int col)
{
sq[row][col] = value;
// fill in code here.
}
public boolean diagonalLeft(int tN)
{
int sum = 0;
//write loop to see if the diagonal from
//lower left to upper right is the same as tN
for(int d = 0; d < sq[0].length; d++)
{
sum = sum + sq[d][(sq.length-1) - d];
}
return (sum == tN);
}
public boolean diagonalRight (int tN)
{
int sum = 0;
//write loop to see if the diagonal from upper left
//to lower right is the same as tN
for (int d = 0; d < sq[0].length; d++)
{
sum = sum + sq[d][d];
}
return (sum == tN);
}
public boolean rowAndColumn(int tN)
{
int rowsum = 0, colsum = 0;
for(int i = 0; i < sq[0].length; i++)
{
for(int j = 0; j < sq[0].length; j++)
{
rowsum = rowsum + sq[i][j];
colsum = colsum + sq[j][i];
}
if(rowsum != tN || colsum != tN)
return false; //no point checking the rest if the sums doesn't match
rowsum = 0; //reset row count
colsum = 0; //reset col count
}
return true; //if it doesn't return by here, all the sums match
}
public String toString()
{
String s = "";
for (int r = 0; r < sq.length; r++)
{
for (int c = 0; c < sq[r].length; c++)
{
s += sq[r][c] + " ";
}
s+= " ";
}
return s;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.