public class RectangleArea
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 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: // Report dimensions and area to user
26: reportResults(rect);
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: * Create a Rectangle object using dimensions entered by the user.
39: *
40: * @return a new Rectangle object with dimensions entered by the user
41: */
42: private static Rectangle getRectangle() {
43: double height, width;
44:
45: height = readDouble("Enter the Rectangle's height: ");
46: width = readDouble("Enter the Rectangle's width: ");
48: return new Rectangle(height, width);
49: }
51: /**
52: * Prompt for and read an double value from the user.
53: *
54: * @param prompt the message explaining to the user what to enter
55: * @return the value entered by the user in response to the prompt
56: */
57: private static double readDouble(String prompt) {
58: // create a variable to hold the result
59: double result;
60: Scanner kbd = new Scanner(System.in);
62: // prompt the user for input
63: System.out.print(prompt);
65: // get their answer and tidy the input stream
66: result = kbd.nextDouble();
67: kbd.nextLine();
69: // send their answer back to the caller
70: return result;
71: }
73: /**
74: * Report the dimensions and area of a rectangle to the user.
75: *
76: * @param rect the Rectangle to report on
77: */
78: private static void reportResults(Rectangle rect) {
79: System.out.println("The area of a "
80: + rect
81: + " is " + rect.getArea() + ".");
82: }
84: /**
85: * Prompt the user and wait for them to press the enter key.
86: * NOTE: the input stream must not have a new-line character in it.
87: */
88: private static void pause() {
89: Scanner kbd = new Scanner(System.in);
90: System.out.print("\nPress enter...");
91: kbd.nextLine();
92: System.out.println();
93: }
95: }