class RecurringPatterns {
//Note that the patterns will appear
//properly aligned only if a fixed width
//font is used (e.g. Courier). With the normal
//Times Roman (variable width font) the patterns
//will not be properly aligned.
//Each pattern is expressed at a higher
//level in terms of functions which describe
// the patterns. This makes the pattern code
//easier to understand.
static void firstPattern(int n) {
for (int i=1;i<=n;i++) {
writeBlank(n-i);
writePattern("a ",i);
System.out.println();
}
for (int i=n-1;i>0;i--) {
writeBlank(n-i);
writePattern("a ",i);
System.out.println();
}
}
static void secondPattern(int n) {
for (int i=1;i<=n;i++) {
writeBlank(n-i);
writeConsecutive(1,i);
writeReverseConsecutive(i-1,1);
System.out.println();
}
for (int i=n-1;i>0;i--) {
writeBlank(n-i);
writeConsecutive(1,i);
writeReverseConsecutive(i-1,1);
System.out.println();
}
}
static void thirdPattern(int n) {
//the first line
writeConsecutiveChars(1,n-1);
writeChar(n);
writeReverseConsecutiveChars(n-1,1);
System.out.println();
//the recurring pattern
for (int i=n-1;i>=1;i--) {
writeConsecutiveChars(1,i);
writeBlank(2*(n-i)-1);
writeReverseConsecutiveChars(i,1);
System.out.println();
}
for (int i=2;i<=n-1;i++) {
writeConsecutiveChars(1,i);
writeBlank(2*(n-i)-1);
writeReverseConsecutiveChars(i,1);
System.out.println();
}
//the last line
writeConsecutiveChars(1,n-1);
writeChar(n);
writeReverseConsecutiveChars(n-1,1);
System.out.println();
}
static void writeBlank(int n) {
for (int i=1;i<=n;i++) System.out.print(" ");
}
static void writePattern(String pat,int n) {
for (int i=1;i<=n;i++) System.out.print(pat);
}
static void writeConsecutive(int from,int to) {
for (int i=from;i<=to;i++) System.out.print(i);
}
static void writeReverseConsecutive(int from,int to) {
for (int i=from;i>=to;i--) System.out.print(i);
}
//Characters are encoded as ints from 0 to 127
//the ASCII encoding. So it type casts the int
//to char.
static void writeConsecutiveChars(int from,int to) {
for (int i=from;i<=to;i++)
System.out.print((char)(i+96));
}
static void writeChar(int i) {
System.out.print((char)(i+96));
}
static void writeReverseConsecutiveChars(int from,int to) {
for (int i=from;i>=to;i--)
System.out.print((char)(i+96));
}
}