public class GarbageRectangleArea
1: import java.util.Scanner;
3: /**
4: * This program is supposed to show the area of a rectangle. It has a bug.
5: * It passes the WRONG arguments to the printReport method. Actually, they
6: * are the right arguments; they're just in the wrong order.
7: *
8: * But: Garbage in; garbage out! The method prints what it was told to print.
9: *
10: * @author Mark Young (A00000000)
11: */
12: public class GarbageRectangleArea {
14: public static void main(String[] args) {
15: // Create variables for length, width and area -- and the Scanner
16: int length, width, area;
17: Scanner kbd = new Scanner(System.in);
19: // Tell the user what we're doing
20: printIntroduction();
22: // Get the length and width of the rectangle from the user
23: System.out.print("Enter the length and width of the rectangle: ");
24: length = kbd.nextInt();
25: width = kbd.nextInt();
26: kbd.nextLine();
27: pause();
29: // calculate area
30: area = length * width;
32: // Report dimensions and area to user
33: printReport(area, length, width);
34: // It's main's fault that the mistake happens!
35: // It passed the arguments in the wrong order.
36: }
38: /**
39: * Introduce this program to the user, then pause.
40: */
41: public static void printIntroduction() {
42: System.out.println("\n\n"
43: + "Rectangle Area Calculator\n"
44: + "-------------------------\n\n"
45: + "This program calculates the area of a rectangle "
46: + "-- POORLY.\n\n"
47: + "By Mark Young (A00000000)");
48: pause();
49: }
51: /**
52: * Report findings to user. Include length, width and area of the
53: * rectangle in the report. This method relies on main to pass in the
54: * correct length, width and area. It's NOT THIS METHOD'S FAULT if main
55: * passes in the wrong numbers, or passes them in the wrong order!
56: *
57: * @param len the length of the rectangle
58: * @param wid the width of the rectangle
59: * @param ar the area of the rectangle
60: */
61: public static void printReport(int len, int wid, int ar) {
62: // report results to user
63: System.out.println("The area of a " + len + "x" + wid
64: + " rectangle is " + ar);
66: // pause to let user read report
67: pause();
68: }
70: /**
71: * Prompt user then wait for them to press the enter key.
72: */
73: public static void pause() {
74: Scanner kbd = new Scanner(System.in);
75: System.out.print("\n... press enter ...");
76: kbd.nextLine();
77: System.out.println();
78: }
80: }