CSE142 Sample Program handout #6 Output Produced +------+ |\ /| | \ / | | \/ | | /\ | | / \ | |/ \| +------+ Program File DrawFigure.java ---------------------------- // This program produces an hourlgass figure as output. public class DrawFigure { public static final int SUB_HEIGHT = 3; // height of each half public static void main(String[] args) { drawLine(); drawTop(); drawBottom(); drawLine(); } // Produces a solid line public static void drawLine() { System.out.print("+"); for (int column = 1; column <= (2 * SUB_HEIGHT); column++) System.out.print("-"); System.out.println("+"); } // This produces the top half of the hourglass figure public static void drawTop() { for (int line = 1; line <= SUB_HEIGHT; line++) { System.out.print("|"); for (int column = 1; column <= (line - 1); column++) System.out.print(" "); System.out.print("\\"); for (int column = 1; column <= 2 * (SUB_HEIGHT - line); column++) System.out.print(" "); System.out.print("/"); for (int column = 1; column <= (line - 1); column++) System.out.print(" "); System.out.println("|"); } } // This produces the bottom half of the hourglass figure public static void drawBottom() { for (int line = 1; line <= SUB_HEIGHT; line++) { System.out.print("|"); for (int column = 1; column <= (SUB_HEIGHT - line); column++) System.out.print(" "); System.out.print("/"); for (int column = 1; column <= 2 * (line - 1); column++) System.out.print(" "); System.out.print("\\"); for (int column = 1; column <= (SUB_HEIGHT - line); column++) System.out.print(" "); System.out.println("|"); } } }
Stuart Reges
Last modified: Wed Oct 6 10:52:48 PDT 2004