CPSC 115L: Introduction to Computing Fall 2010

Homework 8

CPSC 115-01: due Monday, December 6
CPSC 115-02: due Tuesday, December 7

1. Magic squares

A magic square is an n × n matrix of integers in which every integer between 1 and n2 appears exactly once, and every row, column and diagonal adds up to the same total. For this assignment, using a 2-dimensional array, you are to write a Java application to test whether any given square of integers is a magic square.

2. The class MagicSquare

Your Java application should run on a square of any size, say up to 100 × 100, according to the following format:
Enter the size of a square: 3

Now, enter a square by rows.

Enter row 0: 6 7 2
Enter row 1: 1 5 9
Enter row 2: 8 3 4

The square you entered

   6   7   2
   1   5   9
   8   3   4
 
is a magic square!
Implement your program in a class named MagicSquare using the following code segment as a template.
import java.io.*;
import java.util.*;

public class MagicSquare {

  private int n;     // the size of a square
  private int M[][]; // an array reference for a square

  public MagicSquare() { }

  public void readSquare() throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
    System.out.print("Enter the size of your square: ");
    st = new StringTokenizer(br.readLine()) ;
    n = Integer.parseInt(st.nextToken());
    System.out.println();
    M = new int[n][n];
    for (int i = 0; i < n; i++) {
      System.out.print("Enter row " + i + ": ");
      st = new StringTokenizer(br.readLine());
      for (int j = 0; j < n; j++)
        M[i][j] = Integer.parseInt(st.nextToken());
    }
  }

  public static void main(String args[]) throws IOException {

    // To be completed.

  }

}
In the following exercises, you will be implementing six additional methods in the class MagicSquare.

2.1. The method readSquare() reads the size of a square into the variable n and a square into the array M. Now, to print a square in a nice format, implement the method

public void printSquare(). This method prints the array M, properly aligning each column.

For proper alignment, the following tabPrint() method will be useful.
public void tabPrint(int k) {
  int space = 0;
  String tab = "";

  if (k < 10) space = 3;
  if (k >=10 && k < 100) space = 2;
  if (k >=100 && k < 1000) space = 1;
  for(int i = 0; i < space; i++)
    tab = tab + " ";
  System.out.print(tab + k);
}
2.2. You should next implement the following three methods to check if every row, column and diagonal adds up to the same total.

public int checkRows(). This method returns the sum of a single row if every row adds up to the same total and -1 otherwise.

public int checkColumns(). This method returns the sum of a single column if every column adds up to the same total and -1 otherwise.

public int checkDiagonals(). This method returns the sum of a single diagonal if the two diagonals adds up to the same total and -1 otherwise.

In the main method, write a code segment to verify the correctness of these methods. Run your program on five squares, including the following two:
   6   7   2     17  24   1   8  15
   1   5   9     23   5   7  14  16 
   8   3   4      4   6  13  20  22
                 10  12  19  21   3
                 11  18  25   2   9
Save the snapshots of your test runs in a text file named checkSums.out.

2.3. In order to conclude “magicness&rdquo, your program must also check if, in a given n × n square, every integer between 1 and n2 appears exactly once. For this, implement the method

public boolean areDistinct(). This method returns true if, in a given n × n square, every integer between 1 and n2 appears exactly once and false otherwise.

Finally, implement the method

public boolean isMagic(). This method returns true if a square defined by the array M is a magic square and false otherwise.

Modify your main method to complete your program. To verify correctness, run your program on five squares, including the following two:
   6   7   2     17  24   1   8  15
   1   5   9     23   5   7  14  16
   8   3   4      4   6  13  20  22
                 10  12  19  21   3
                 11  18  25   2   9
Save the snapshots of your test runs in a text file named checkDistinctness.out.

What to hand in

Submit the following in paper. Be sure to put a file header at the top of each file.

Plagiarism and academic dishonesty

Please remember our course policy on plagiarism and academic dishonesty: You are encouraged to consult with one another when you work on homework assignments, but in the end everyone must do one's own work to hand in. In particular, discussion of homework assignments should be limited to brainstorming and verbally going through strategies, but it must not involve one student sharing written solutions with another student. In the end everyone must write up solutions independently. If you have discussed with classmates or used any outside source, you must clearly indicate so on your solutions and provide all references. Turning in another person's work under your name is plagiarism and qualifies as academic dishonesty. Academic dishonesty is a serious intellectual violation, and the consequences can be severe. For more details, read the Student Handbook 2010–2011, pp. 21–29.


* CPSC 115L home page
Valid HTML 4.01!