Program to print simple triangles in the form: * ** *** ***** public class Triangle { public static void main (String args[]) { int num_rows; char letter = '#'; System.out.println("To print a Triangle, please enter the number"); System.out.println(" of rows to print as an integer between 1 & 26. "); 1 num_rows = (int) ReadItem.getLong(); // print one row at a time System.out.println(); for (int i = 1; i <= num_rows; i++) { // for each row print the characters // the number of characters in each row is the // same as the row number 2 for(int j = 1; j <= i; j++) { System.out.print(letter); } 3 System.out.print('\n'); // print the new line character } System.out.println("\nAll done"); } } /home/hayhurst/java- java Triangle To print a Triangle, please enter the number of rows to print as an integer between 1 & 26. 8 # ## ### #### ##### ###### ####### ######## All done Centered Triangles public class CenteredTriangle { public static void main (String args[]) { int num_rows, num_blanks = 39, num_chars = 1; char letter = '#'; System.out.println("To print a Triangle, please enter the number"); System.out.println(" of rows to print as an integer between 1 & 26. "); num_rows = (int) ReadItem.getLong(); // print one row at a time System.out.println(); for (int i = 1; i <= num_rows; i++) { // for each row we need to skip enough spaces to center // the characters in the row.. In the first row there // is one character. Assuming 80 characters per row // we need to skip 39 blank spaces. In each subsequent // row, we will print 2 more characters, and skip one // fewer space. // print out the required number of blanks 2 for(int j = 1; j <= num_blanks; j++) { System.out.print(' '); } 1 // print out the required number of characters for (int k = 1; k <= num_chars; k++) { System.out.print(letter); } 3 // to go to a new line print the new line character System.out.print('\n'); // decrement the number of blanks num_blanks--; 4 // increment the number of characters to print num_chars += 2; } System.out.println("\nAll done"); } } The output is: /home/hayhurst/java- java CenteredTriangle To print a Triangle, please enter the number of rows to print as an integer between 1 & 26. 8 # ### ##### ####### ######### ########### ############# ############### All done A triangle class, to print many triangles. public class CenteredTriangleClass { private int num_rows; private char begin_letter; private boolean vary_char; public CenteredTriangleClass (int rows, char letter, boolean vary) { num_rows = rows; begin_letter = letter; vary_char = vary; } public CenteredTriangleClass () { num_rows = 10; begin_letter = '#'; vary_char = false; } public void draw_self() { int num_blanks = 39, num_chars = 1; char letter = begin_letter; // print one row at a time System.out.println(); for (int i = 1; i <= num_rows; i++) { // for each row we need to skip enough spaces to center // the characters in the row.. In the first row there // is one character. Assuming 80 characters per row // we need to skip 39 blank spaces. In each subsequent // row, we will print 2 more characters, and skip one // fewer space. // print out the required number of blanks for(int j = 1; j <= num_blanks; j++) { System.out.print(' '); } // print out the required number of characters for (int k = 1; k <= num_chars; k++) { System.out.print(letter); } // to go to a new line print the new line character System.out.print('\n'); // decrement the number of blanks num_blanks--; // increment the number of characters to print num_chars += 2; // increment to the next character if desired if (vary_char) letter = (char) (letter + 1); } System.out.println("\nAll done"); } // end draw_self } Program to test Class CenteredTriangleClass public class CenteredTriangleTest { public static void main(String args[]) { CenteredTriangleClass small = new CenteredTriangleClass( 5, 'a', false); CenteredTriangleClass varysmall = new CenteredTriangleClass( 5 , 'a', true); CenteredTriangleClass large = new CenteredTriangleClass( 10, '*', true); small.draw_self(); varysmall.draw_self(); large.draw_self(); } } /home/hayhurst/java- java CenteredTriangleTest a aaa aaaaa aaaaaaa aaaaaaaaa All done a bbb ccccc ddddddd eeeeeeeee All done * +++ ,,,,, ------......... /////////// 0000000000000 111111111111111 22222222222222222 3333333333333333333 All done