Advertisement
brilliant_moves

HollowOctagon.java

Apr 27th, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.95 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HollowOctagon {
  4.  
  5.     public static void drawHorizontal(int leftAlign, char ch, int s) {
  6.         for (int i=0; i<leftAlign; i++) {
  7.             System.out.print(" ");
  8.         } // for
  9.         for (int i=0; i<s; i++) {
  10.             System.out.print(ch);
  11.         } // for
  12.         System.out.println();
  13.     } // drawHorizontal()
  14.  
  15.     public static void drawVerticals(int leftAlign, char ch, int s) {
  16.         for (int i=0; i<s; i++) {
  17.             for (int j=0; j<leftAlign-s+1; j++) {
  18.                 System.out.print(" ");
  19.             } // for
  20.             System.out.print(ch);
  21.             for (int sp=0; sp<3*(s-1)-1; sp++) {
  22.                 System.out.print(" ");
  23.             } // for sp
  24.             System.out.println(ch);
  25.         } // for i
  26.     } // drawVerticals()
  27.  
  28.     public static void drawTopDiagonals(int leftAlign, char ch, int s) {
  29.         for (int i=0; i<s-2; i++) {
  30.             for (int j=0; j<leftAlign-(i+1); j++) {
  31.                 System.out.print(" ");
  32.             } // for
  33.             System.out.print(ch);
  34.             for (int sp=0; sp<s+2*i; sp++) {
  35.                 System.out.print(" ");
  36.             } // for sp
  37.             System.out.println(ch);
  38.         } // for i
  39.     } // drawTopDiagonals()
  40.  
  41.     public static void drawLowerDiagonals(int leftAlign, char ch, int s) {
  42.         for (int i=s-3; i>=0; i--) {
  43.             for (int j=0; j<leftAlign-(i+1); j++) {
  44.                 System.out.print(" ");
  45.             } // for
  46.             System.out.print(ch);
  47.             for (int sp=0; sp<s+2*i; sp++) {
  48.                 System.out.print(" ");
  49.             } // for sp
  50.             System.out.println(ch);
  51.         } // for i
  52.     } // drawLowerDiagonals()
  53.  
  54.     public static void main(String[] args) {
  55.         char symbol = '*';
  56.         int size = 10;
  57.         int leftAlign = 10;
  58.  
  59.         Scanner scan = new Scanner(System.in);
  60.         System.out.print("Enter size of octagon (up to 10): ");
  61.         size = scan.nextInt();
  62.         if (size>10) size=10;
  63.         if (size<2) size = 2;
  64.  
  65.         System.out.println();
  66.  
  67.         drawHorizontal(leftAlign, symbol, size);
  68.         drawTopDiagonals(leftAlign, symbol, size);
  69.         drawVerticals(leftAlign, symbol, size);
  70.         drawLowerDiagonals(leftAlign, symbol, size);
  71.         drawHorizontal(leftAlign, symbol, size);
  72.     } // main()
  73.  
  74. } // class HollowOctagon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement