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

Need help writing the next method called checkUnique() for a magic square progra

ID: 3813549 • Letter: N

Question

Need help writing the next method called checkUnique() for a magic square program. The specifications for it are written in the documentation towards the end of the program. Output needs to look like the following picture where it says: "UNIQUE: 5 6".

NOTE: I am unable to use HashMap to write this method as it has not been covered in lecture yet.

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);
boolean result = checkRows(array, magic);
System.out.println();
boolean resultTwo = checkColumns(array, magic);
System.out.println();
boolean resultThree = checkDiagonals(array, magic);
System.out.println();
}
  
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 ) {
boolean result = true;
int count = 1;
for ( int r = 0; r < square.length; r++ ) {
int sum = 0;
for ( int c = 0; c < square[r].length; c++ )
sum += square[r][c];
if ( magicnum != sum ) {
System.out.printf ( " ROW %d:BAD (%d instead of %d)", count, sum, magicnum );
result = false;
}
else {
System.out.printf ( " ROW %d:GOOD", count );
}
count++;

}
return result;
}

/**
* checkColumns()
*
* This method checks each column to see if it is a valid column, with all
* values adding up to the magic number sent in as an argument. Label
* the column outputs starting at 1.
*
* The output should be either the text GOOD or BAD, depending on the
* status or the individual column. If the column is BAD, also include what
* that column 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 the columns 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 columns are valid
*/
public static boolean checkColumns ( int square [][], int magicnum ) {
boolean resultTwo = true;
int count = 1;
for ( int r = 0; r < square.length; r++ ) {
int sum = 0;
for ( int c = 0; c < square[r].length; c++ )
sum += square[c][r];
if ( magicnum != sum ) {
System.out.printf ( " COL %d:BAD (%d instead of %d)", count, sum, magicnum );
resultTwo = false;
}
else {
System.out.printf ( " COL %d:GOOD", count );
}
count++;

}
return resultTwo;
}

/**
* checkDiagonals()
*
* This method checks the two major diagonals to see if they are valid,
* with all values adding up to the magic number sent in as an argument.
* Label the diagonal from upper-left corner (position [0][0]) to the
* bottom-right corner (position [n-1][n-1]) as DIAG 1. Label the
* other diagonal as DIAG 2.
*
* The output should be either the text GOOD or BAD, depending on the
* status of the individual diagonal. If the diagonal is BAD, also include
* what that diagonal 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 both of the diagonals 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 both of the diagonals are valid
*/
public static boolean checkDiagonals ( int [][] square, int magicnum ) {
boolean resultThree = true;
int sumOne = 0;
int sumTwo = 0;
for ( int r = 0; r < square.length; r++ ) {
int c = 0;
while ( c < square[r].length ) {
if ( r == c )
sumOne += square[r][c];
c++;
if ( r == (( square[r].length) - c))
sumTwo += square[r][c-1];
}
}
if ( sumOne == magicnum && sumTwo == magicnum ) {
System.out.printf ( " DIAG 1:GOOD" );
System.out.printf ( " DIAG 2:GOOD" );
}
else {
System.out.printf ( " DIAG 1:BAD (%d instead of %d)", sumOne, magicnum );
System.out.printf ( " DIAG 2:BAD (%d instead of %d)", sumTwo, magicnum );
resultThree = false;
}
return resultThree;
}

/**
* checkUnique()
*
* This method checks to see if each value used in the potential magic
* square has been used once and only once in the entirety of the square.
*
* Different-sized squares will have different potential ranges of values
* to test. For instance, a 4x4 square will have 16 potential values
* to test (1-16). A 7x7 square will have 49 potential values to test.
*
* If all values in the appropriate range have been used only once,
* the output should be the text GOOD for the label UNIQUE.
*
* If a value was used more than once or wasn't used at all, it should
* be listed in order from smallest to largest with a space between each
* offending value.
*
* See the example runs for an idea of what the output should look like.
*
* The method should return true if all values have been used exactly once
* and false otherwise.
*
* @param square a 2-d int array representing the square
* @return boolean true or false if all values are used exactly once
*/
public static boolean checkUnique ( int [][] square ) {
boolean resultFour = true;
return resultFour;
}
}

Enter input filename: badms3.txt. Checking a 3 x 3 Magic square! The magic number is 15 1] 2] 3] 11 2 7 6 21 9 6 1 3] 4 3 8 ROW 1:GOOD ROW 2:BAD (16 instead of 15) ROW 3:GOOD COL 1:GOOD COL 2:BAD (16 instead of 15) COL 3:GOOD DIAG 1: BAD (16 instead of 15) DIAG 2: BAD (16 instead of 15) UNIQUE 5 6

Explanation / Answer

code:

/**
* checkUnique()
*
* This method checks to see if each value used in the potential magic
* square has been used once and only once in the entirety of the square.
*
* Different-sized squares will have different potential ranges of values
* to test. For instance, a 4x4 square will have 16 potential values
* to test (1-16). A 7x7 square will have 49 potential values to test.
*
* If all values in the appropriate range have been used only once,
* the output should be the text GOOD for the label UNIQUE.
*
* If a value was used more than once or wasn't used at all, it should
* be listed in order from smallest to largest with a space between each
* offending value.
*
* See the example runs for an idea of what the output should look like.
*
* The method should return true if all values have been used exactly once
* and false otherwise.
*
* @param square a 2-d int array representing the square
* @return boolean true or false if all values are used exactly once
*/
public static boolean checkUnique ( int [][] square ) {
boolean resultFour = true;
System.out.print("UNIQUE:");
int size = (square.length * square.length)+ 1;

// Create an array to store the number of times each number appears.
int[] freq = new int[size];
int num = 0;
// Set each frequency to zero initially.
// Note: We will NOT use index 0 and we will store how many 1s we see
// in index 1, etc.
for (int i = 1; i < size; i++)
freq[i] = 0;
// Loop through each value in the square.
for (int i = 0; i < square.length; i++)
{
for (int j = 0; j < square.length; j++)
{
// Increase the frequncy for this number.
freq[square[i][j]]++;
}
}
// If any number does not appear exactly once then the square is not magic.
for (int i = 1; i < size; i++)
if (freq[i] != 1)
{
num++;
resultFour = false;
}
int[] unique = new int[num];
int x = 0;
for (int i = 1; i < size; i++)
if (freq[i] != 1)
{
unique[x] = i;
x++;
}
if(unique.length == 0)
System.out.print(" GOOD");
else
{
int temp;
for (int i = 0; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (unique[i] > unique[j])
{
temp = unique[i];
unique[i] = unique[j];
unique[j] = temp;
}
}
}
for (int i = 0; i < unique.length; i++)
System.out.print(" " + unique[i]);

}
System.out.println();
return resultFour;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote