Need help writing the checkRows() method for this Java program that involves a m
ID: 3813353 • Letter: N
Question
Need help writing the checkRows() method for this Java program that involves a magic square. A description is given in the documentation below.
import java.util.Scanner;
import java.io.File;
public class MagicSquare {
private static Scanner input = new Scanner ( System.in );
public static void main ( String args [] ) {
try {
Scanner keyboard = new Scanner ( System.in );
System.out.println ( " Enter input filename: " );
String inFile;
inFile = keyboard.next();
System.out.println();
File file = new File ( inFile );
Scanner input = new Scanner ( file );
int first = input.nextInt();
final int ROWS = first;
final int COLS = first;
int[][] array = new int[ROWS][COLS];
while ( input.hasNext() ) {
for ( int r = 0; r < array.length; r++ ) {
for ( int c = 0; c < array[r].length; c++ )
array[r][c] = input.nextInt();
}
}
int magic = 0;
for ( int i = 0; i < array[0].length; i++ ) {
magic += array[i][0];
}
displaySquare(array, magic);
}
catch ( Exception ex ) {
System.out.println ( "An error has occurred!" );
ex.printStackTrace();
}
}
/**
* displaySquare()
*
* This method displays the square in nice, tidy rows and columns.
* Each row should be labeled as [XX], where XX is the number of the
* row, and each column should have an [XX] above it as a header
* where XX is the column number.
*
* The method should also display a textual header about the size of
* the square and the magic number of the square. See the example runs
* for an idea what the output should look like.
*
* @param square a 2-d int array representing the square
* @param magicnum an int containing the calculated magic number
* @return None
*/
public static void displaySquare( int square[][], int magicnum ) {
int size = square.length;
System.out.printf ( "Checking a %d x %d Magic Square! ", size, size );
System.out.printf ( "The magic number is %d ", magicnum );
System.out.println();
int countCol = 1;
int countRow = 1;
System.out.print ( " " );
while ( countCol <= size ) {
if ( countCol <= 9 )
System.out.printf ( "[ %d] ", countCol );
else
System.out.printf ( "[%d] ", countCol );
countCol++;
}
System.out.println();
for ( int x = 0; x < square.length; x++ ) {
if ( countRow <= 9 )
System.out.printf ( "[ %d]", countRow );
else
System.out.printf ( "[%d]", countRow );
for ( int y = 0; y < square[x].length; y++ )
System.out.printf ( "%5d", square[x][y] );
System.out.println();
countRow++;
}
}
/**
* checkRows()
*
* This method checks each row to see if it is a valid row, with all
* values adding up to the magic number sent in as an argument. Label
* the row outputs starting at 1.
*
* The output should be either the text GOOD or BAD, depending on the
* status of the individual row. If the row is BAD, also include what
* that row added up to instead of the magic number. See the example
* runs for an idea of what the output should look like.
*
* The method should return true if all of the rows are valid and false
* otherwise.
*
* @param square a 2-d int array representing the square
* @param magicnum an int containing the calculated magic number
* @return boolean true or false if all of the rows are valid
*/
public static boolean checkRows ( int square [][], int magicnum ) {
return value;
}
}
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
import java.io.File;
public class MagicSquare {
private static Scanner input = new Scanner ( System.in );
public static void main ( String args [] ) {
try {
Scanner keyboard = new Scanner ( System.in );
System.out.println ( " Enter input filename: " );
String inFile;
inFile = keyboard.next();
System.out.println();
File file = new File ( inFile );
Scanner input = new Scanner ( file );
int first = input.nextInt();
final int ROWS = first;
final int COLS = first;
int[][] array = new int[ROWS][COLS];
while ( input.hasNext() ) {
for ( int r = 0; r < array.length; r++ ) {
for ( int c = 0; c < array[r].length; c++ )
array[r][c] = input.nextInt();
}
}
int magic = 0;
for ( int i = 0; i < array[0].length; i++ ) {
magic += array[i][0];
}
displaySquare(array, magic);
}
catch ( Exception ex ) {
System.out.println ( "An error has occurred!" );
ex.printStackTrace();
}
}
/**
* displaySquare()
*
* This method displays the square in nice, tidy rows and columns.
* Each row should be labeled as [XX], where XX is the number of the
* row, and each column should have an [XX] above it as a header
* where XX is the column number.
*
* The method should also display a textual header about the size of
* the square and the magic number of the square. See the example runs
* for an idea what the output should look like.
*
* @param square a 2-d int array representing the square
* @param magicnum an int containing the calculated magic number
* @return None
*/
public static void displaySquare( int square[][], int magicnum ) {
int size = square.length;
System.out.printf ( "Checking a %d x %d Magic Square! ", size, size );
System.out.printf ( "The magic number is %d ", magicnum );
System.out.println();
int countCol = 1;
int countRow = 1;
System.out.print ( " " );
while ( countCol <= size ) {
if ( countCol <= 9 )
System.out.printf ( "[ %d] ", countCol );
else
System.out.printf ( "[%d] ", countCol );
countCol++;
}
System.out.println();
for ( int x = 0; x < square.length; x++ ) {
if ( countRow <= 9 )
System.out.printf ( "[ %d]", countRow );
else
System.out.printf ( "[%d]", countRow );
for ( int y = 0; y < square[x].length; y++ )
System.out.printf ( "%5d", square[x][y] );
System.out.println();
countRow++;
}
}
/**
* checkRows()
*
* This method checks each row to see if it is a valid row, with all
* values adding up to the magic number sent in as an argument. Label
* the row outputs starting at 1.
*
* The output should be either the text GOOD or BAD, depending on the
* status of the individual row. If the row is BAD, also include what
* that row added up to instead of the magic number. See the example
* runs for an idea of what the output should look like.
*
* The method should return true if all of the rows are valid and false
* otherwise.
*
* @param square a 2-d int array representing the square
* @param magicnum an int containing the calculated magic number
* @return boolean true or false if all of the rows are valid
*/
public static boolean checkRows ( int a [][], int magicnum ) {
boolean value = true;
for (int row = 0; row < a.length; row++){
int sum = 0;
for (int col = 0; col< a[row].length; col++)
sum += a[row][col];
if (magicnum != sum){
System.out.println("BAD");
System.out.println("Sum of row "+(row+1)+" is "+sum);
value = false;
}else{
System.out.println("GOOD");
}
}
return value;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.