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 week02/RectangleArea
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class RectangleArea {

 12:     public static final Scanner KBD = new Scanner(System.in);

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

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

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

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

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

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

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

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

 63:         // prompt the user for input
 64:         System.out.print(prompt);

 66:         // get their answer and tidy the input stream
 67:         result = KBD.nextDouble();
 68:         KBD.nextLine();

 70:         // send their answer back to the caller
 71:         return result;
 72:     }

 74:     /**
 75:      * Report the dimensions and area of a rectangle to the user.
 76:      *
 77:      * @param rect the Rectangle to report on
 78:      */
 79:     private static void reportResults(Rectangle rect) {
 80:         System.out.println("The area of a " 
 81:                 + rect
 82:                 + " is " + rect.getArea() + ".");
 83:     }

 85:     /**
 86:      * Prompt the user and wait for them to press the enter key.
 87:      * NOTE: the input stream must not have a new-line character in it.
 88:      */
 89:     private static void pause() {
 90:         System.out.print("\nPress enter...");
 91:         KBD.nextLine();
 92:         System.out.println();
 93:     }

 95: }