/** ups3.java (adds OS1 classification) * * 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: * - THIS PROGRAM IS NOT COMPLETE, IT ONLY CHECKS FOR OS1. * - DOUBLES SHOULD PROBABLY BE USED FOR EXTRA PRECISION */ // Javadoc comments are also used. See p.131 in JBD. import tio.*; class ups3 { /** 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); // output classification System.out.print("The UPS classification is: "); if (isOversize1(length, girth, weight)) System.out.println(" OS1"); else System.out.println(" (unknown)"); 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() /** isOversize1() determines if a package should be classifed as OS1. * * @param ln The length of the package in inches. * @param g The girth of the package in inches. * @param wt The weight of the package in lbs. * @return True if the package is OS1, false otherwise. */ public static boolean isOversize1(int ln, int g, int wt) { return ((ln + g > 84) // combined l & g exceed 84 inches && (ln + g <= 108) // combined l & g less than or equal to 108 && (wt < 30)); // weight is less than 30 pounds } // end isOversize1() } // end ups class /* SAMPLE RUN: $ java ups Please enter the package specifications. weight (lbs): 22 height (in): 13 width (in): 18 length (in): 25 The girth is 62 The UPS classification is: OS1 Do you have another package to classify? (y/n) y Please enter the package specifications. weight (lbs): 29 height (in): 15 width (in): 23 length (in): 30 The girth is 76 The UPS classification is: OS1 Do you have another package to classify? (y/n) y Please enter the package specifications. weight (lbs): 20 height (in): 20 width (in): 25 length (in): 40 The girth is 90 The UPS classification is: (unknown) Do you have another package to classify? (y/n) n $ */