/** ups2.java (calculates the girth) * * CS7 - practicing with java functions * * This program asks the user for package details and produces a * classification according to the ups rules. To find out more * about these guidelines, visit: * * http://www.ups.com/content/us/en/resources/prepare/weight_size.html * * Issues: * - DOUBLES SHOULD PROBABLY BE USED FOR EXTRA PRECISION */ // Javadoc comments are also used. See p.131 in JBD. import tio.*; class ups2 { /** main() handles the top level control. It calls various * methods to determine the proper classification for the * user's packages. */ public static void main (String[] args) { char answer='y'; // user's choice. int weight, height, width, length, girth; while (answer=='y' || answer=='Y') { // get the package specifications System.out.println("Please enter the package specifications."); weight = getPosInt(" weight (lbs): "); height = getPosInt(" height (in): "); width = getPosInt(" width (in): "); length = getPosInt(" length (in): "); System.out.println(); // calculate & display girth girth = calcGirth(height, width); System.out.println("The girth is " + girth); System.out.println(); System.out.print("Do you have another package to classify? (y/n) "); Console.in.readLine(); // flush out the CR answer = (char) Console.in.readChar(); } } // end main() /** getPosInt() reads an integer from the user and rereads while the * number is not positive. * * @param prompt The string to present to the user prior to input. * @return The positive integer. */ public static int getPosInt(String prompt) { System.out.print(prompt); int n = Console.in.readInt(); while (n <= 0) { System.out.print("Please enter a postive value: "); n = Console.in.readInt(); } return n; } // end getPosInt() /** calcGirth() returns the girth of a package, which is the distance * around a package, perpendicular to the length (the longest edge). * * @param ht The height of the package in inches. * @param wd The width of the package in inches. * @return The girth of the package. */ public static int calcGirth(int ht, int wd) { return (ht*2 + wd*2); } // end calcGirth() } // end ups class /* SAMPLE RUN: $ java ups2 Please enter the package specifications. weight (lbs): 20 height (in): 22 width (in): 19 length (in): 28 The girth is 82 Do you have another package to classify? (y/n) y Please enter the package specifications. weight (lbs): 18 height (in): 2 width (in): 4 length (in): 10 The girth is 12 Do you have another package to classify? (y/n) n $ */