Coursera - Computer Science: Programming With A Purpose

Week 2: Conditionals And Loops - Band Matrices

Write a program BandMatrix.java that takes two integer command-line arguments n and width and prints an n-by-n pattern like the ones below, with a zero (0) for each element whose distance from the main diagonal is strictly more than width, and an asterisk (*) for each entry that is not, and two spaces between each 0 or *.

Here, distance means the minimum number of cells you have to move (either left, right, up, or down) to reach any element on the main diagonal.

~/Desktop/loops> java BandMatrix 8 0
*  0  0  0  0  0  0  0  
0  *  0  0  0  0  0  0  
0  0  *  0  0  0  0  0  
0  0  0  *  0  0  0  0  
0  0  0  0  *  0  0  0  
0  0  0  0  0  *  0  0  
0  0  0  0  0  0  *  0  
0  0  0  0  0  0  0  *  

~/Desktop/loops> java BandMatrix 8 1
*  *  0  0  0  0  0  0  
*  *  *  0  0  0  0  0  
0  *  *  *  0  0  0  0  
0  0  *  *  *  0  0  0  
0  0  0  *  *  *  0  0  
0  0  0  0  *  *  *  0  
0  0  0  0  0  *  *  *  
0  0  0  0  0  0  *  * 

~/Desktop/loops> java BandMatrix 8 2
*  *  *  0  0  0  0  0  
*  *  *  *  0  0  0  0  
*  *  *  *  *  0  0  0  
0  *  *  *  *  *  0  0  
0  0  *  *  *  *  *  0  
0  0  0  *  *  *  *  *  
0  0  0  0  *  *  *  *  
0  0  0  0  0  *  *  *  

~/Desktop/loops> java BandMatrix 8 3
*  *  *  *  0  0  0  0  
*  *  *  *  *  0  0  0  
*  *  *  *  *  *  0  0  
*  *  *  *  *  *  *  0  
0  *  *  *  *  *  *  *  
0  0  *  *  *  *  *  *  
0  0  0  *  *  *  *  *  
0  0  0  0  *  *  *  *  

Note: you may assume that n and width are non-negative integer.

Band matrices are matrices whose nonzero entries are restricted to a diagonal band. They arise frequently in numerical linear algebra.

Note: the above description is copied from Coursera and converted to markdown for convenience

Solution:

public class BandMatrix {

    public static void main(String[] args) {
        final int n = Integer.parseInt(args[0]);
        final int width = Integer.parseInt(args[1]);
        for (int y = 0; y < n; y++) {
            for (int x = 0; x < n; x++) {
                if (Math.abs(x - y) > width) {
                    if (x == 0) {
                        System.out.print("0");
                    } else {
                        System.out.print("  0");
                    }
                } else {
                    if (x == 0) {
                        System.out.print("*");
                    } else {
                        System.out.print("  *");
                    }
                }
            }
            System.out.println();
        }
    }
}

Link To: Java Source Code