\Java
Stars.java
//******************************************************************** // // Classname.java // // Author: Josh Santomieri // Author's email: // Author's WWW URL: http://www.santsys.com/ // // Course, section, and quarter: CPE 101-02 Fall 2001 // Department: Computer Science // School: Calif. Polytechnic State Univ. // Address: San Luis Obispo, CA, 93407 USA // // History: // // //******************************************************************** // // Program Description and Design Overview: // Create a program that draws Stars. The program must draw 4 different // kind of star patterns by printing sequences of asterisks and blanks // (spaces) on the screen with nested loops. The Stars.java program on // page 150 of your textbook is one of the four kinds of patterns your // program must print (click on link to download). The other three patterns // are shown here (including the original). Notice that the star patterns // may have different number of rows. Patterns are named A, B, C, and D // (left to right below). // // * ********** * * // ** ********* ** *** // *** ******** *** ***** // **** ******* **** ******* // ***** ****** ***** ********* // ****** ***** ****** *********** // ******* **** ******* ************* // ******** *** ******** // ********* ** ********* // ********** * // *********** // // Your program must prompt the user for the number of rows and the pattern type // (A, B, C, and D). Then print a star with the selected pattern and size. It // should then print 3 blank lines and prompt the user again. If the user enters // zero or a negative number of rows, the program should end. Otherwise, loop and // draw another star pattern // // // Input Requirements: // number of rows // row pattern (A, B, C, or D) // // Output Requirements: // prompt user for number of rows (if number is less than or equal to zero, stop program) // prompt user for type of pattern // pattern of stars (A, B, C, or D) // same amount of rows as entered // 3 blank lines // // Program Preconditions: // number of rows must be an integer // //******************************************************************** import java.io.*; public class Stars { //----------------------------------------------------------------- // a program that draws Stars in the shape and amount of rows that user entered //----------------------------------------------------------------- public static void main (String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int numRows = 0; String option = ""; String sMax = ""; // get the pattern type do { System.out.println("Enter a shape pattern (A, B, C, or D):"); option = reader.readLine().toUpperCase(); } while(!option.equals("A") && !option.equals("B") && !option.equals("C") && !option.equals("D")); // get the number of rows System.out.println("Enter the number of rows:"); sMax = reader.readLine(); numRows = Integer.parseInt(sMax); // if less than one row, exit. if(numRows < 1) { System.out.println("Nothing to print. Exiting."); return; } System.out.println(); // draw the selected pattern if(option.equals("A")) { for(int row = 0; row < numRows; row++) { for(int stars = 0; stars < row + 1; stars++) { System.out.print('*'); } System.out.println(); } } else if(option.equals("B")) { for(int row = 0; row < numRows; row++) { // pad the left for(int j = 0; j < row; j++) { System.out.print(" "); } // draw the stars for(int k = 0; k < numRows - row; k++) { System.out.print("*"); } System.out.println(); } } else if(option.equals("C")) { for(int row = 0; row < numRows; row++) { // pad the left for(int j = 0; j < numRows - row - 1; j++) { System.out.print(" "); } // draw the stars for(int k = numRows - row; k <= numRows; k++) { System.out.print("*"); } System.out.println(); } } else if(option.equals("D")) { for(int row = 0; row < numRows; row++) { // pad the left for(int j = 0; j < numRows - row - 1; j++) { System.out.print(" "); } // draw the stars for(int k = numRows - row; k <= numRows + (row * 1); k++) { System.out.print("*"); } // pad the right (not sure we need this, but why not...) for(int j = 0; j < numRows - row; j++) { System.out.print(" "); } System.out.println(); } } // end with 3 blank lines System.out.print("\r\n\r\n\r\n"); } //end main } // end class Stars