public class AsteriskFractal
{
//*******************************************************
// main
//
// Program will call a recursive method with two parameters,
the number of
// spaces to indent and the length of the middle line
//*******************************************************
public static void main(String args[])
{
printFractal(0,32);
System.out.println();
} // end main
//*******************************************************
// printFractal
//
// Inputs: indent - number of spaces the fractal should be
indented
//
lineLength - the number of '*'s to put on the middle line
// Algorithm: this is a recursive function that basically
divides the problem
// into draw the top half, draw the line, draw the bottom
half. The recursion
// stops when there is nothing to draw (lineLength == 0)
//*******************************************************
public static void printFractal(int indent, int lineLength)
{
// recursion end condition
if (lineLength==0)
{
return;
}
// print the top fractal
// it is half as long
printFractal(indent, lineLength/2);
// indent the required number of
spaces before drawing the line
for (int numSpaces = 0; numSpaces < indent;
numSpaces++)
{
System.out.print(" ");
}
// draw the middle line or star
for (int numStars = 0; numStars <
lineLength; numStars++)
{
System.out.print("*");
}
System.out.println();
// draw the bottom fractal
// it is half as long and indented
halfway
printFractal(indent+lineLength/2,
lineLength/2);
}
} // end AsteriskFractal