public class RectangleArea
1: import java.util.Scanner;
3: /**
4: * This program calculates the area of a rectangle, using the Rectangle class.
5: * It catches exceptions thrown when the user enters invalid dimensions.
6: *
7: * @author Mark Young (A00000000)
8: */
9: public class RectangleArea {
11: public static void main(String[] args) {
12: // Create variables for the Rectangle and its area
13: Rectangle rect;
14: double area;
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: rect = getRectangle();
22: pause();
24: // Report dimensions and area to user
25: reportResults(rect);
26: pause();
27: }
29: /**
30: * Print the introduction for this program.
31: */
32: private static void printIntroduction() {
33: System.out.println("This program calculates the area of a rectangle.");
34: }
36: /**
37: * Create a Rectangle object using dimensions entered by the user. Complain
38: * and return nothing if the object could not be created.
39: *
40: * @return a new Rectangle object with dimensions entered by the user, or
41: * null if the object could not be created
42: */
43: private static Rectangle getRectangle() {
44: double height, width;
45:
46: height = readDouble("Enter the Rectangle's height: ");
47: width = readDouble("Enter the Rectangle's width: ");
49: try {
50: return new Rectangle(height, width);
51: } catch (IllegalArgumentException iae) {
52: // illegal dimensions -- complain and return nothing
53: System.out.println("\nI couldn't create a "
54: + height + "x" + width + " rectangle "
55: + "-- " + iae.getMessage() + ".");
56: return null;
57: }
58: }
60: /**
61: * Prompt for and read an double value from the user.
62: *
63: * @param prompt the message explaining to the user what to enter
64: * @return the value entered by the user in response to the prompt
65: */
66: private static double readDouble(String prompt) {
67: // create a variable to hold the result
68: double result;
69: Scanner kbd = new Scanner(System.in);
71: // prompt the user for input
72: System.out.print(prompt);
74: // get their answer and tidy the input stream
75: result = kbd.nextDouble();
76: kbd.nextLine();
78: // send their answer back to the caller
79: return result;
80: }
82: /**
83: * Report the dimensions and area of a rectangle to the user. Test for no
84: * Rectangle being present, because that could happen!
85: *
86: * @param rect the Rectangle to report on
87: */
88: private static void reportResults(Rectangle rect) {
89: if (rect != null) {
90: System.out.println("The area of a "
91: + rect
92: + " is " + rect.getArea() + ".");
93: } else {
94: System.out.println("There's no rectangle to calculate "
95: + "the area of!");
96: }
97: }
99: /**
100: * Prompt the user and wait for them to press the enter key.
101: * NOTE: the input stream must not have a new-line character in it.
102: */
103: private static void pause() {
104: Scanner kbd = new Scanner(System.in);
105: System.out.print("\nPress enter...");
106: kbd.nextLine();
107: System.out.println();
108: }
110: }