Diamond Printouts
The first method presented is a very mathematically elegant and object-oriented
approach. It explicitly calculates the number of spaces or stars needed given the
width and the current zero-based height that it is printing out. Symmetry is used
for the lower half of the triangle with another explicit formula.
public final class Diamond1 {
private static int width;
private Diamond1() {
setWidth(1);
}
private static void setWidth(int w) {
width = w;
}
private static int width() {
return width;
}
private static int midpoint() {
return (width()-1)/2;
}
private static int totalHeight() {
return 2*((width()+1)/2) - 1;
}
private static int equivalentHeight(int height) {
return 2*midpoint()-height;
}
private static int spaces(int height) {
if (height <= midpoint())
return midpoint()-height;
else
return spaces(equivalentHeight(height));
}
private static int stars(int height) {
if (height <= midpoint())
return 2*height+1+(width()+1)%2;
else
return stars(equivalentHeight(height));
}
private static void print(char character, int numberOfTimes) {
for (int numberPrinted = 0; numberPrinted < numberOfTimes; ++numberPrinted)
System.out.print(character);
}
private static void newline() {
System.out.println();
}
public static void printDiamond(int width) {
setWidth(width);
for (int height = 0; height < totalHeight(); ++height) {
print(' ',spaces(height));
print('*',stars(height));
newline();
}
}
}
The second method is a small and functional approach. Instead of explicitly
calculating the number of spaces and stars, it correctly initializes them for
the first line, and then iteratively adjusts the numbers for the succeeding lines.
It takes advantage of different loop control variables, and is a much more
funtioning-oriented design than the first approach.
public final class Diamond2 {
private Diamond2() {}
public static void printDiamond(int width) {
int numberOfSpaces = (width-1)/2;
int numberOfStars = 2-width%2;
while (numberOfSpaces >= 0) {
for (int space = 0; space < numberOfSpaces; ++space)
System.out.print(" ");
for (int star = 0; star < numberOfStars; ++star)
System.out.print("*");
System.out.println();
--numberOfSpaces;
numberOfStars+=2;
}
numberOfSpaces+=2;
numberOfStars-=4;
while (numberOfStars > 0) {
for (int space = 0; space < numberOfSpaces; ++space)
System.out.print(" ");
for (int star = 0; star < numberOfStars; ++star)
System.out.print("*");
System.out.println();
++numberOfSpaces;
numberOfStars-=2;
}
}
}
The last method is by far the smallest in terms of lines written, and is most likely
the "best". It used negative heights as well as positive heights and capitalizes on
absolute value, as well as using explicit formulas for the number for spaces and stars.
public final class Diamond3 {
private Diamond3() {}
public static void printDiamond(int width) {
int halfWidth = (width-1)/2;
for (int height = -halfWidth; height <= halfWidth; ++height) {
for (int space = 0; space < Math.abs(height); ++space)
System.out.print(" ");
for (int star = 0; star < width-2*Math.abs(height); ++star)
System.out.print("*");
System.out.println();
}
}
}