To create a larger diamond, take a smaller one and add two additional rows and columns. In the diagonal below, for clarity, I replace places with dots. In the second diamond, the newly added characters are shown in bold.
*****. ***** <- extra row
****. **** ****. .. ****
*** ... *** *** .. ... ***
** ..... ** ** ... .. .. **
* ....... * * .... .. ... *
......... -> ..... .. ....
* ....... * * .... .. ... *
** ..... ** ** ... .. .. **
*** ... *** *** .. ... ***
****. **** ****. .. ****
*****. ***** <- extra row
^^
||
extra columns
Your recursive function should print the first line, and then print a smaller diamond with two additional columns in the middle, then with the last line.
In pseudo code:
void diamond(stars, spaces) { if (n == 0) { print(' ' * spaces) } else { print('*' * stars, ' ' * spaces, '*' * stars) diamond(stars - 1, spaces + 2) print('*' * stars, ' ' * spaces, '*' * stars) } }
Since this is a training exercise, I will not give you the full Java source code - you can write it yourself. Here you can see how it works on the Internet in Python, so you can see that the algorithm works:
source share