import java.util.Scanner; // for reading input /** Java program that draws "hourglass" figures. Based upon code in the ** "Building Java Programs" text by Reges and Stepp. ** This version augments its predecessor (which has no '2' in its name) ** by allowing the user to choose the size of the hourglass to be drawn. ** ** Author: R. McCloskey ** Last Modified: March 10, 2014 */ public class DrawHourGlass2 { public static void main(String[] args) { int r = getIntFromUser("Enter size of hourglass to draw: "); System.out.println("An hourglass of size " + r + ":"); drawHourGlass(r); // draw an hourglass of size r } /** Returns an int value entered at the keyboard in response to the ** prompt received via the formal parameter (prompt). */ private static int getIntFromUser(String prompt) { Scanner keyboard = new Scanner(System.in); System.out.print(prompt); int response = keyboard.nextInt(); return response; } /** Draws an hourglass figure of the specified size. (Here, size ** corresponds to one-half the number of lines that the figure ** occupies, excluding the borders at the top and bottom.) */ private static void drawHourGlass(int size) { drawBorder(size); // draw top line drawTopHalf(size); // draw top half drawBottomHalf(size); // draw bottom half drawBorder(size); // draw bottom line } /** Draws the border that belongs at the top and bottom of an hourglass ** figure of the specified size, which is received via the formal parameter. */ private static void drawBorder(int size) { System.out.print('+'); printChars('-', 2*size); // print 2*size dashes System.out.print('+'); System.out.println(); } /** Draws the "top half" of an hourglass figure of the specified size, ** which is received via the formal parameter. */ private static void drawTopHalf(int size) { for (int k=1; k <= size; k = k+1) { System.out.print('|'); // print a vertical bar printChars(' ', k-1); // print k-1 spaces System.out.print('\\'); // print a backslash printChars('.', 2*(size-k)); // print 2(size-k) dots System.out.print('/'); // print a slash printChars(' ', k-1); // print k-1 spaces System.out.print('|'); // print a vertical bar System.out.println(); // skip to next line } } /** Draws the "bottom half" of an hourglass figure of the specified size, ** which is received via the formal parameter. */ private static void drawBottomHalf(int size) { for (int k=1; k <= size; k = k+1) { System.out.print('|'); // print a vertical bar printChars(' ', size-k); // print size-k spaces System.out.print('/'); // print a slash printChars('.', 2*(k-1)); // print 2(k-1) dots System.out.print('\\'); // print a backslash printChars(' ', size-k); // print size-k spaces System.out.print('|'); // print a vertical bar System.out.println(); // skip to next line } } /** Prints the specified character (received via formal parameter ch) ** the specified # of times (received via formal parameter n). */ private static void printChars(char ch, int n) { for (int i=1; i <= n; i = i+1) { System.out.print(ch); } } }