Source of BadRectangleArea.java


  1: import java.util.Scanner;

  3: /**
  4:  * This program is BAD!  BAD BAD BAD!  Don't do what this program does.  It 
  5:  * reads a value into a parameter!  It does it TWICE!  It is TWO-TIMES-BAD.
  6:  *
  7:  *  @author Mark Young (A00000000)
  8:  */
  9: public class BadRectangleArea {

 11:     public static void main(String[] args) {
 12:         // Create variables for length, width and area -- and the Scanner
 13:         int length, width;
 14:         Scanner kbd = new Scanner(System.in);

 16:         // Tell the user what we're doing
 17:         printIntroduction();

 19:         // Get the length and width of the rectangle from the user
 20:         System.out.print("Enter the length and width of the rectangle: ");
 21:         length = kbd.nextInt();
 22:         width = kbd.nextInt();
 23:         kbd.nextLine();
 24:         pause();

 26:         // Report dimensions and area to user
 27:         printReport(length, width);
 28:     }

 30:     /**
 31:      * Introduce this program to the user, then pause.
 32:      */
 33:     public static void printIntroduction() {
 34:         System.out.println("\n\n"
 35:                 + "Rectangle Area Calculator\n"
 36:                 + "-------------------------\n\n"
 37:                 + "This program calculates the area of a rectangle "
 38:                 + "-- POORLY.\n\n"
 39:                 + "By Mark Young (A00000000)");
 40:         pause();
 41:     }

 43:     /**
 44:      * Report findings to user.  Include length, width and area of
 45:      * the rectangle in the report.
 46:      *
 47:      * @param length the length of the rectangle
 48:      * @param width the width of the rectangle
 49:      */
 50:     public static void printReport(int length, int width) { 
 51:         // create local variables
 52:         Scanner kbd = new Scanner(System.in);
 53:         int area;

 55:         // THIS IS THE BAD THING COMING UP
 56:         // Get the length and width of the rectangle from the user
 57:         System.out.print("Enter the length and width of the rectangle: ");
 58:         length = kbd.nextInt();
 59:         width = kbd.nextInt();
 60:         kbd.nextLine();
 61:         pause();
 62:         // NOW length and width might be different from first user input
 63:         // AT BEST the user had to type their numbers in TWICE.

 65:         // Multiply length by width to get the area of the rectangle
 66:         area = length * width;

 68:         // report results to user
 69:         System.out.println("The area of a " + length + "x" + width
 70:             + " rectangle is " + area);

 72:         // pause to let user read report
 73:         pause();
 74:     }

 76:     /**
 77:      * Prompt user then wait for them to press the enter key.
 78:      */
 79:     public static void pause() {
 80:         Scanner kbd = new Scanner(System.in);
 81:         System.out.print("\n... press enter ...");
 82:         kbd.nextLine();
 83:         System.out.println();
 84:     }

 86: }