/* solution for week 5 lab, Program B */ import java.io.*; import java.util.*; public class week05b { public static void main (String[] args) throws IOException { String type; char display; int size1, size2; Scanner keys = new Scanner(System.in); System.out.print("Enter input filename: "); Scanner infile = new Scanner(new FileReader(keys.nextLine())); while (infile.hasNext()) { type = infile.next(); display = infile.next().charAt(0); size1 = infile.nextInt(); switch (type.charAt(0)) { case 'r': size2 = infile.nextInt(); rectangle(display, size1, size2); break; case 't': triangle(display, size1); break; case 'd': diamond(display, size1); break; } } } // end of main /** Draw a rectangle using the character c of the specified width and height */ public static void rectangle(char c, int height, int width) { for (int row = 1; row <= height; row++) { for (int col=1; col <= width; col++) System.out.print(c); System.out.println(); } } /** Draw a (right) triangle using the character c of the specified size */ public static void triangle(char c, int size) { for (int row = 1; row <= size; row++) { for (int col=1; col <= row; col++) System.out.print(c); System.out.println(); } } /** Draw a diamond using the character c of the specified size */ public static void diamond(char c, int size) { int row, col; // print top half for (row = 1; row <= size; row = row + 2) { // print leading spaces for (col = 1; col <= (size - row) / 2; col++) System.out.print(" "); // print characters for (col = 1; col <= row; col++) System.out.print(c); System.out.println(); } // print bottom half for (row = size-2; row >= 1; row = row - 2) { // print leading spaces for (col = 1; col <= (size - row) / 2; col++) System.out.print(" "); // print characters for (col = 1; col <= row; col++) System.out.print(c); System.out.println(); } } } // end of program