Source of DrawRectangle.java


  2: import java.util.Scanner;

  4: /**
  5:  *  This program draws a rectangle.
  6:  *
  7:  *  @author Mark Young (A00000000)
  8:  */
  9: public class DrawRectangle {

 11:     public static void main(String[] args) {
 12:         // create the variables
 13:         Scanner kbd = new Scanner(System.in);
 14:         int height, width;

 16:         // Tell the user what we're doing
 17:         printIntroduction();
 18:         pause();

 20:         // Ask the user for the rectangles measurements
 21:         System.out.print("Enter the height and width of the rectangle: ");
 22:         height = kbd.nextInt();
 23:         width = kbd.nextInt();
 24:         kbd.nextLine();
 25:         pause();

 27:         // draw the rectangle
 28:         drawRectangle(height, width);
 29:         pause();
 30:     }

 32:     /**
 33:      * Introduce this program to the user
 34:      */
 35:     public static void printIntroduction() {
 36:         System.out.println("\n\n"
 37:                 + "Draw a Rectangle\n"
 38:                 + "----------------\n\n"
 39:                 + "This program draws a rectangle.\n\n"
 40:                 + "By Mark Young (A00000000)");
 41:     }

 43:     /**
 44:      * Draw a rectangle made of stars.
 45:      *
 46:      * @param height the number of lines for the rectangle
 47:      * @param width the number stars on each line
 48:      */
 49:     public static void drawRectangle(int height, int width) {
 50:         for (int line = 1; line <= height; ++line) {
 51:             printNStarsLine(width);
 52:         }
 53:     }

 55:     /**
 56:      * Print a line containing stars.
 57:      *
 58:      * @param numStars the number of stars to print on the line
 59:      */
 60:     public static void printNStarsLine(int numStars) {
 61:         for (int star = 1; star <= numStars; ++star) {
 62:             System.out.print("*");
 63:         }
 64:         System.out.println();
 65:     }

 67:     /**
 68:      * Prompt user then wait for them to press the enter key.
 69:      */
 70:     public static void pause() {
 71:         Scanner kbd = new Scanner(System.in);
 72:         System.out.print("\n... press enter ...");
 73:         kbd.nextLine();
 74:         System.out.println();
 75:     }

 77: }