Source of RectangleArea.java


  2: import java.util.Scanner;

  4: /**
  5:  * This program calculates the area of a rectangle, using the Rectangle class.
  6:  * Based on an earlier program also called RectangleArea.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class RectangleArea {

 12:     public static void main(String[] args) {
 13:         // Create variables for the Rectangle and its area
 14:         Rectangle rect;
 15:         double area;

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

 21:         // Get the length and width of the rectangle from the user
 22:         rect = getRectangle();
 23:         pause();

 25:         // Get the area of the Rectangle
 26:         area = calculateArea(rect);

 28:         // Report dimensions and area to user
 29:         reportResults(rect, area);
 30:         pause();
 31:     }

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

 40:     /**
 41:      * Create a Rectangle object using dimensions entered by the user.
 42:      *
 43:      * @return a new Rectangle object with dimensions entered by the user
 44:      */
 45:     private static Rectangle getRectangle() {
 46:         double height, width;
 47:         
 48:         height = readDouble("Enter the Rectangle's height: ");
 49:         width = readDouble("Enter the Rectangle's width: ");

 51:         return new Rectangle(height, width);
 52:     }

 54:     /**
 55:      * Prompt for and read an double value from the user.
 56:      *
 57:      * @param prompt the message explaining to the user what to enter
 58:      * @return the value entered by the user in response to the prompt
 59:      */
 60:     private static double readDouble(String prompt) {
 61:         // create a variable to hold the result
 62:         double result;
 63:         Scanner kbd = new Scanner(System.in);

 65:         // prompt the user for input
 66:         System.out.print(prompt);

 68:         // get their answer and tidy the input stream
 69:         result = kbd.nextDouble();
 70:         kbd.nextLine();

 72:         // send their answer back to the caller
 73:         return result;
 74:     }

 76:     /**
 77:      * Find the area of a rectangle of the given dimensions.
 78:      *
 79:      * @param rect a Rectangle object
 80:      * @return the area of the rectangle
 81:      */
 82:     private static double calculateArea(Rectangle rect) {
 83:         return rect.getHeight() * rect.getWidth();
 84:     }

 86:     /**
 87:      * Report the dimensions and area of a rectangle to the user.
 88:      *
 89:      * @param rect the Rectangle to report on
 90:      * @param area the area of the rectangle
 91:      */
 92:     private static void reportResults(Rectangle rect, double area) {
 93:         System.out.println("The area of a " 
 94:                 + rect.getHeight() + "x" + rect.getWidth()
 95:                 + " rectangle is " + area + ".");
 96:     }

 98:     /**
 99:      * Prompt the user and wait for them to press the enter key.
100:      * NOTE: the input stream must not have a new-line character in it.
101:      */
102:     private static void pause() {
103:         Scanner kbd = new Scanner(System.in);
104:         System.out.print("\nPress enter...");
105:         kbd.nextLine();
106:         System.out.println();
107:     }

109: }