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:  */

  9: public class RectangleArea {

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

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

 20:         // Get the length and width of the rectangle from the user
 21:         length = readInteger("Enter the length of the rectangle: ");
 22:         width = readInteger("Enter the width of the rectangle: ");
 23:         pause();

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

 30:     /**
 31:      * Print the introduction for this program.
 32:      */
 33:     private static void printIntroduction() {
 34:         System.out.println("This program calculates the area of a rectangle.");
 35:     }

 37:     /**
 38:      * Prompt for and read an integer value from the user.
 39:      *
 40:      * @param prompt the message explaining to the user what to enter
 41:      * @return the value entered by the user in response to the prompt
 42:      */
 43:     private static int readInteger(String prompt) {
 44:         // create a variable to hold the result
 45:         int result;
 46:         Scanner kbd = new Scanner(System.in);

 48:         // prompt the user for input
 49:         System.out.print(prompt);

 51:         // get their answer and tidy the input stream
 52:         result = kbd.nextInt();
 53:         kbd.nextLine();

 55:         // send their answer back to the caller
 56:         return result;
 57:     }

 59:     /**
 60:      * Find the area of a rectangle of the given dimensions.
 61:      *
 62:      * @param length the length of the rectangle
 63:      * @param width the width of the rectangle
 64:      * @return the area of the rectangle
 65:      */
 66:     private static int calculateArea(int length, int width) {
 67:         return length * width;
 68:     }

 70:     /**
 71:      * Report the dimensions and area of a rectangle to the user.
 72:      *
 73:      * @param length the length of the rectangle
 74:      * @param width the width of the rectangle
 75:      */
 76:     private static void printAreaReport(int length, int width) {
 77:         int area = calculateArea(length, width);
 78:         System.out.println("The area of a " + length + "x" + width
 79:             + " rectangle is " + area + ".");
 80:     }

 82:     /**
 83:      * Prompt the user and wait for them to press the enter key.
 84:      * NOTE: the input stream must not have a new-line character in it.
 85:      */
 86:     private static void pause() {
 87:         Scanner kbd = new Scanner(System.in);

 89:         System.out.print("\nPress enter...");
 90:         kbd.nextLine();
 91:         System.out.println();
 92:     }

 94: }