Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // original facebook post:
- // https://www.facebook.com/groups/javaharikrishna/permalink/1596271237165097/
- // JUnit test class:
- // https://pastebin.com/zmdNaJ7k
- // Related "Intersecting Triangles" classes:
- // Implementation:
- // https://pastebin.com/xjyyaBm0
- // JUnit test classes:
- // https://pastebin.com/iiUDpLZG
- // https://pastebin.com/9CCBV1C9
- public class SixSidedStar {
- public static void printSixSidedStar(final int n) {
- if (n > 0) {
- printTopLines(n);
- printHorizonalLineOfStars(n);
- printMiddleLines(n);
- printHorizonalLineOfStars(n);
- printBottomLines(n);
- } else if (n < 0) {
- throw new IllegalArgumentException("Argument value for 'n' must not be negative.");
- }
- }
- private static void printTopLines(final int n) {
- for (int line = 0; line < n; ++line) {
- printTopOrBottomLine(n, line);
- }
- }
- private static void printBottomLines(final int n) {
- for (int line = n - 1; line >= 0; --line) {
- printTopOrBottomLine(n, line);
- }
- }
- private static void printTopOrBottomLine(final int n, final int line) {
- System.out.print(spaces(n * 3 - line));
- final String triangleStars = triangleStars(line);
- System.out.print(triangleStars);
- System.out.println();
- }
- private static void printHorizonalLineOfStars(final int n) {
- final int numberOfStars = n * 3 + 1;
- System.out.println(spaces(numberOfStars * 2).replace(" ", "* ").trim());
- }
- private static void printMiddleLines(final int n) {
- final int boundry = n - 1;
- for (int offset = -boundry; offset <= boundry; ++ offset) {
- final int line = Math.abs(offset);
- final int leftIndent = n - line;
- System.out.print(spaces(leftIndent));
- final String triangleStars = triangleStars(line);
- System.out.print(triangleStars);
- System.out.print(spaces((n + leftIndent) * 2 - 1));
- System.out.println(triangleStars);
- }
- }
- private static String triangleStars(final int line) {
- if (line == 0) {
- return "*";
- } else {
- return "*" + spaces(line * 2 - 1) + "*";
- }
- }
- private static String spaces(final int count) {
- return new String(new char[count]).replace('\0', ' ');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement