Advertisement
JeffGrigg

SixSidedStar

Jul 22nd, 2018
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.44 KB | None | 0 0
  1. // original facebook post:
  2. // https://www.facebook.com/groups/javaharikrishna/permalink/1596271237165097/
  3.  
  4. // JUnit test class:
  5. // https://pastebin.com/zmdNaJ7k
  6.  
  7. // Related "Intersecting Triangles" classes:
  8. // Implementation:
  9. // https://pastebin.com/xjyyaBm0
  10. // JUnit test classes:
  11. // https://pastebin.com/iiUDpLZG
  12. // https://pastebin.com/9CCBV1C9
  13.  
  14. public class SixSidedStar {
  15.  
  16.     public static void printSixSidedStar(final int n) {
  17.         if (n > 0) {
  18.             printTopLines(n);
  19.             printHorizonalLineOfStars(n);
  20.             printMiddleLines(n);
  21.             printHorizonalLineOfStars(n);
  22.             printBottomLines(n);
  23.         } else if (n < 0) {
  24.             throw new IllegalArgumentException("Argument value for 'n' must not be negative.");
  25.         }
  26.     }
  27.  
  28.     private static void printTopLines(final int n) {
  29.         for (int line = 0; line < n; ++line) {
  30.             printTopOrBottomLine(n, line);
  31.         }
  32.     }
  33.  
  34.     private static void printBottomLines(final int n) {
  35.         for (int line = n - 1; line >= 0; --line) {
  36.             printTopOrBottomLine(n, line);
  37.         }
  38.     }
  39.  
  40.     private static void printTopOrBottomLine(final int n, final int line) {
  41.         System.out.print(spaces(n * 3 - line));
  42.         final String triangleStars = triangleStars(line);
  43.         System.out.print(triangleStars);
  44.         System.out.println();
  45.     }
  46.  
  47.     private static void printHorizonalLineOfStars(final int n) {
  48.         final int numberOfStars = n * 3 + 1;
  49.         System.out.println(spaces(numberOfStars * 2).replace("  ", "* ").trim());
  50.     }
  51.  
  52.     private static void printMiddleLines(final int n) {
  53.         final int boundry = n - 1;
  54.         for (int offset = -boundry; offset <= boundry; ++ offset) {
  55.             final int line = Math.abs(offset);
  56.  
  57.             final int leftIndent = n - line;
  58.             System.out.print(spaces(leftIndent));
  59.             final String triangleStars = triangleStars(line);
  60.             System.out.print(triangleStars);
  61.             System.out.print(spaces((n + leftIndent) * 2 - 1));
  62.             System.out.println(triangleStars);
  63.         }
  64.     }
  65.  
  66.     private static String triangleStars(final int line) {
  67.         if (line == 0) {
  68.             return "*";
  69.         } else {
  70.             return "*" + spaces(line * 2 - 1) + "*";
  71.         }
  72.     }
  73.  
  74.     private static String spaces(final int count) {
  75.         return new String(new char[count]).replace('\0', ' ');
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement