JeffGrigg

CrossingStars

Mar 23rd, 2018
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.37 KB | None | 0 0
  1. /*
  2.  * Objective:  Print "cross" or "star" pattern from this post:
  3.  *      https://www.facebook.com/groups/java4developers/permalink/1742344979155475/
  4.  * JUnit tests:
  5.  *      https://pastebin.com/pVw3CUY0
  6.  * Additional tests, and tweaked for someone else's implementation:
  7.  *      https://pastebin.com/bZMmLnQs
  8.  */
  9.  
  10. public class CrossingStars {
  11.  
  12.     public static void printCrossingStars(final int n) {
  13.         for (int i = n; i >= 1; --i) {
  14.             printSideLine(n, i);
  15.         }
  16.  
  17.         printMainLine(n);
  18.  
  19.         for (int i = 1; i <= n; ++i) {
  20.             printSideLine(n, i);
  21.         }
  22.     }
  23.  
  24.     private static void printMainLine(int n) {
  25.         for (int j = 1; j <= n; ++j) {
  26.             System.out.print(nOf(j + 1, '*'));
  27.         }
  28.         if (n > 0) {
  29.             System.out.println();
  30.         }
  31.     }
  32.  
  33.     private static void printSideLine(int n, int i) {
  34.         for (int j = 1; j <= n; ++j) {
  35.             System.out.print(nOf(j, ' '));
  36.             System.out.print( (j < i) ? ' ' : '*');
  37.         }
  38.         System.out.println();
  39.     }
  40.  
  41.     private static String nOf(final int numberOf, final char chr) {
  42.         final StringBuilder stringBuilder = new StringBuilder();
  43.         for (int count = 0; count < numberOf; ++count) {
  44.             stringBuilder.appendCodePoint(chr);
  45.         }
  46.         return stringBuilder.toString();
  47.     }
  48.  
  49. }
Add Comment
Please, Sign In to add comment