Source of RectangleArea.java


  1: import java.util.Scanner;

  3: /**
  4:  *  This program calculates the area of a rectangle.
  5:  *
  6:  *  @author Mark Young (A00000000)
  7:  */
  8: public class RectangleArea {

 10:     public static void main(String[] args) {
 11:         // Create variables for length, width and area -- and the Scanner
 12:         int length, width;
 13:         Scanner kbd = new Scanner(System.in);

 15:         // Tell the user what we're doing
 16:         printIntroduction();

 18:         // Get the length and width of the rectangle from the user
 19:         System.out.print("Enter the length and width of the rectangle: ");
 20:         length = kbd.nextInt();
 21:         width = kbd.nextInt();
 22:         kbd.nextLine();
 23:         pause();

 25:         // Report dimensions and area to user
 26:         printReport(length, width);
 27:     }

 29:     /**
 30:      * Introduce this program to the user, then pause.
 31:      */
 32:     public static void printIntroduction() {
 33:         System.out.println("\n\n"
 34:                 + "Rectangle Area Calculator\n"
 35:                 + "-------------------------\n\n"
 36:                 + "This program calculates the area of a rectangle.\n\n"
 37:                 + "By Mark Young (A00000000)");
 38:         pause();
 39:     }

 41:     /**
 42:      * Report findings to user.  Include length, width and area of
 43:      * the rectangle in the report.
 44:      *
 45:      * @param len the length of the rectangle
 46:      * @param wid the width of the rectangle
 47:      */
 48:     public static void printReport(int len, int wid) { 
 49:         // create local variables
 50:         int area;

 52:         // Multiply length by width to get the area of the rectangle
 53:         area = len * wid;

 55:         // report results to user
 56:         System.out.println("The area of a " + len + "x" + wid
 57:             + " rectangle is " + area);

 59:         // pause to let user read report
 60:         pause();
 61:     }

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

 73: }