CSCI A201/A597

Solutions

Assignment Three: Crossed.java


Here's the code, with comments (in blue):
public class Crossed {                                   // name of class
    public static void main(String[] args) {             // boilerplate 
        int width = Integer.parseInt(args[0]);           // horizontal size  
	int height = width;                              // vertical size 
        int lineNo, rowNo;                               // or use i, j 
        for (lineNo = 0; lineNo < height; lineNo++) {    // for all lines 
	    for (rowNo = 0; rowNo < width; rowNo++) {    // now line is given 
		// so for all rows on that line (all characters on the line) 
                // now we are working on cell located at (lineNo, rowNo) 
		if (lineNo == 0 ||                       // if top line 
		    lineNo == (height - 1) ||            // or bottom line 
                    rowNo == 0 ||                        // or left border 
		    rowNo == (width - 1) ||              // or right border 
                    lineNo == rowNo ||                   // or first diagonal 
		    lineNo + rowNo == (width - 1)) {     // or second diagonal 
		    System.out.print(" *");              // turn cell "on" 
		} else {
		    System.out.print("  ");              // turn cell "off" 
		} // end of if 
                // so at this point we have printed a character for the cell 
                // if cell satisfies our criteria we place a * in it, if not
                // we leave the cell empty by printing a space for it
                // but notice that all cells are one char wide! 
                // at this point rowNo is incremented and condition tested
	    } // end of for that works on one line char by char (row by row) 
            // at this point line lineNo is printed 
            // we need to get ready to print a new line 
	    System.out.println(); // so we print a new line 
            // next, lineNo will be incremented and condition tested 
	} // end of for that processes the lines 
    } // end of main 
} // end of class  

There will be additional info posted on the exams page in anticipation of the practical.