Source of RectangleArea.java


  1: package exceptions;

  3: import java.util.Scanner;

  5: /**
  6:  * This program calculates the area of a rectangle, using the Rectangle class.
  7:  * It catches exceptions thrown when the user enters invalid dimensions.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class RectangleArea {

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

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

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

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

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

 38:     /**
 39:      * Create a Rectangle object using dimensions entered by the user. Complain
 40:      * and return nothing if the object could not be created.
 41:      *
 42:      * @return a new Rectangle object with dimensions entered by the user, or
 43:      * null if the object could not be created
 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:         try {
 52:             return new Rectangle(height, width);
 53:         } catch (IllegalArgumentException iae) {
 54:             // illegal dimensions -- complain and return nothing
 55:             System.out.println("\nI couldn't create a " 
 56:                     + height + "x" + width + " rectangle "
 57:                     + "-- " + iae.getMessage() + ".");
 58:             return null;
 59:         }
 60:     }

 62:     /**
 63:      * Prompt for and read an double value from the user.
 64:      *
 65:      * @param prompt the message explaining to the user what to enter
 66:      * @return the value entered by the user in response to the prompt
 67:      */
 68:     private static double readDouble(String prompt) {
 69:         // create a variable to hold the result
 70:         double result;
 71:         Scanner kbd = new Scanner(System.in);

 73:         // prompt the user for input
 74:         System.out.print(prompt);

 76:         // get their answer and tidy the input stream
 77:         result = kbd.nextDouble();
 78:         kbd.nextLine();

 80:         // send their answer back to the caller
 81:         return result;
 82:     }

 84:     /**
 85:      * Report the dimensions and area of a rectangle to the user. Test for no
 86:      * Rectangle being present, because that could happen!
 87:      *
 88:      * @param rect the Rectangle to report on
 89:      */
 90:     private static void reportResults(Rectangle rect) {
 91:         if (rect != null) {
 92:             System.out.println("The area of a " 
 93:                     + rect
 94:                     + " is " + rect.getArea() + ".");
 95:         } else {
 96:             System.out.println("There's no rectangle to calculate "
 97:                     + "the area of!");
 98:         }
 99:     }

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

112: }