public class DrawHourGlass { public static void main(String[] args) { System.out.println("An hourglass of size 4:"); drawHourGlass(4); // draw an hourglass of size 4 printChars('\n', 2); // skip two lines by printing two newline chars System.out.println("An hourglass of size 20:"); drawHourGlass(20); // draw an hourglass of size 20 } //Draws the hourglass related to the size. public 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 } //Outside of the hourglass. public static void drawBorder(int size) { System.out.print('|'); printChars('\"', 2*size); // print 2*size dashes System.out.print('|'); System.out.println(); } //Top half of hourglass. public static void drawTopHalf(int size) { for (int k=1; k <= size; k = k+1) { printChars(' ', k); // print k-1 spaces System.out.print('\\'); // print a backslash printChars(':', 2*(size-k)); // print 2*size-2k dots System.out.print('/'); // print a slash System.out.println(); // skip to next line } } //Bottom half of hourglass. public static void drawBottomHalf(int size) { for (int k=1; k <= size; k = k+1) { printChars(' ', size-k); // print size-k spaces System.out.print('/'); // print a slash printChars(':', 2*(k)); // print 2*k-2 System.out.print('\\'); // print a backslash printChars(' ', size-k); // print size-k spaces System.out.println(); // skip to next line } } //Prints the amount of charcter and numbers input. public static void printChars(char m, int n) { for (int i=1; i <= n; i = i+1) { System.out.print(m); } //Prints in invidual line. } }
I have too many methods. I don't have a constant.
... I'm not sure if I'm allowed to use printChars either.
2hnt1s0.jpg