Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Objective: Print "cross" or "star" pattern from this post:
- * https://www.facebook.com/groups/java4developers/permalink/1742344979155475/
- * JUnit tests:
- * https://pastebin.com/pVw3CUY0
- * Additional tests, and tweaked for someone else's implementation:
- * https://pastebin.com/bZMmLnQs
- */
- public class CrossingStars {
- public static void printCrossingStars(final int n) {
- for (int i = n; i >= 1; --i) {
- printSideLine(n, i);
- }
- printMainLine(n);
- for (int i = 1; i <= n; ++i) {
- printSideLine(n, i);
- }
- }
- private static void printMainLine(int n) {
- for (int j = 1; j <= n; ++j) {
- System.out.print(nOf(j + 1, '*'));
- }
- if (n > 0) {
- System.out.println();
- }
- }
- private static void printSideLine(int n, int i) {
- for (int j = 1; j <= n; ++j) {
- System.out.print(nOf(j, ' '));
- System.out.print( (j < i) ? ' ' : '*');
- }
- System.out.println();
- }
- private static String nOf(final int numberOf, final char chr) {
- final StringBuilder stringBuilder = new StringBuilder();
- for (int count = 0; count < numberOf; ++count) {
- stringBuilder.appendCodePoint(chr);
- }
- return stringBuilder.toString();
- }
- }
Add Comment
Please, Sign In to add comment