Skip to content

Instantly share code, notes, and snippets.

@3355844
Created January 10, 2016 14:44
Show Gist options
  • Save 3355844/6add8be3f2b872fb1395 to your computer and use it in GitHub Desktop.
Save 3355844/6add8be3f2b872fb1395 to your computer and use it in GitHub Desktop.
package HomeWorkSecond;
/**
* Created by Andrey on 09.01.2016.
*/
public class ConditionsLoopsTask5RhombusPrinter {
public static final String S_POINT = " . ";
public static final String S_STAR = " * ";
public static void main(String[] args) {
int size = 7;
printRhombus(size);
}
public static void printRhombus(int size) {
checkSize(size);
int middle = size / 2 + 1;
int rightStar = middle;
int leftStar = middle;
for (int i = 1; i <= size; i++) {
if (i < middle) {
printUpRhombus(leftStar, rightStar, size);
rightStar++;
leftStar--;
}
if (i >= middle) {
printBottomRhombus(leftStar, rightStar, size);
rightStar--;
leftStar++;
}
System.out.println();
}
}
public static void printBottomRhombus(int leftStar, int rightStar, int size) {
for (int j = size; j >= 1; j--) {
if (rightStar == j || leftStar == j) {
System.out.print(S_STAR);
} else {
System.out.print(S_POINT);
}
}
}
public static void printUpRhombus(int leftStar, int rightStar, int size) {
for (int j = 1; j <= size; j++) {
if (rightStar == j || leftStar == j) {
System.out.print(S_STAR);
} else {
System.out.print(S_POINT);
}
}
}
public static void checkSize(int size) {
int n = size % 2;
if (size < 0 && n == 0) throw new IllegalArgumentException();
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment