// ============================================================================= /** * Print a variety of basic 2D patterns to the console. * * @author sfkaplan@cs.amherst.edu */ public class PrintPattern { // ============================================================================= // ========================================================================= // CONSTANTS /** The character to use in printing the boundaries of each pattern. */ final public static char mark = '+'; /** The character to use in printing the spaces within each pattern. */ final public static char space = ' '; // ========================================================================= // ========================================================================= /** * The program's entry point. * * @param args The command-line arguments provided by the user. */ public static void main (String[] args) { // If an incorrect number of arguments was provided on the command-line, // then print the correct usage and exit. if (args.length != 2) { showUsageAndExit(); } // Grab the arguments. String shape = args[0]; int size = 0; try { size = Integer.parseInt(args[1]); } catch (NumberFormatException e) { showUsageAndExit(); } // Call the appropriate method for the requested pattern. if (shape.equals("square")) { printSquare(size); } else if (shape.equals("triangle")) { printTriangle(size); } else if (shape.equals("octagon")) { printOctagon(size); } else if (shape.equals("serpinksi")) { printSierpinkski(size); } else { showUsageAndExit(); } } // main() // ========================================================================= // ========================================================================= /** * Print the correct form for running this program and exit with an error * code. */ public static void showUsageAndExit () { System.err.println("USAGE: java PrintPattern " + " "); System.exit(1); } // showUsageAndExit() // ========================================================================= // ========================================================================= /** * Print an empty square with size characters to each side. * * @param size The size of a the square on each size (as a number of * characters). */ public static void printSquare (int size) { for (int i = 0; i < size; i += 1) { System.out.print(mark); } System.out.println(); for (int i = 0; i < size - 2; i += 1) { System.out.print(mark); for (int j = 0; j < size - 2; j += 1) { System.out.print(space); } System.out.println(mark); } for (int i = 0; i < size; i += 1) { System.out.print(mark); } System.out.println(); } // printSquare() // ========================================================================= // ========================================================================= /** * Print an empty isosceles triangle with size characters for * the base and height. * * @param size The size of the base and height of the triangle (as a number * of characters). */ public static void printTriangle (int size) { // REPLACE ME WITH REAL CODE! System.out.println("Triangles NOT YET IMPLEMENTED!"); } // printTriangle () // ========================================================================= // ========================================================================= /** * Print an empty octagon with size characters to each side. * * @param size The number of characters to each side of the octagon. */ public static void printOctagon (int size) { // REPLACE ME WITH REAL CODE! System.out.println("Octagon NOT YET IMPLEMENTED!"); } // printOctagon () // ========================================================================= // ========================================================================= /** * Print the first rows of Sierpinkski's Gasket as a sequence of * mark and space characters. * * @param rows The number of rows of the gasket to print. */ public static void printSierpinkski (int rows) { // REPLACE ME WITH REAL CODE! System.out.println("Sierpinkski NOT YET IMPLEMENTED!"); } // printSierpinkski () // ========================================================================= // ============================================================================= } // class PrintPattern // =============================================================================