brainfrz

Untitled

Oct 10th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.46 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     Program #2: Conversion Chart
  3.  *  Author:           Terry Weiss
  4.  *  Date:             October 20, 2015
  5.  *  Course/Section:   CSC 111-003W
  6.  *  Program Description:
  7.  *     This program displays an inches-to-centimeters conversion table. The input is the smallest
  8.  *  number of inches and then the largest number of inches to be converted. The program then
  9.  *  displays the conversion for each 6-inch increment between the start and end numbers. The end
  10.  *  number must be between 6 and 36 inches larger than the start number. One inch is equivalent to
  11.  *  2.54 centimeters. The program gives the option to run the program again.
  12.  **************************************************************************************************/
  13. package weissprogram2;
  14.  
  15. import java.util.Scanner;
  16.  
  17.  
  18. /*
  19.  *  This class displays an inches-to-centimeters conversion table with 6-inch increments. One inch
  20.  *  is equivalent to 2.54 centimeters.
  21.  *
  22.  *  Input data:
  23.  *      (int) startRange – This is the first inch to be converted
  24.  *      (int) endRange – This is the highest possible inch to be converted
  25.  *      (String) userContinues – This is the user’s response to whether they want to continue
  26.  *  Output data:
  27.  *      (int) inch – This is the current inch being converted
  28.  *      (double) centimeter – This is the number of centimeters equivalent to the current inch being
  29.  *          converted, which will be formatted to two decimal points
  30.  *  Additional data:
  31.  *      (final double) CONVERSION_RATE – This is the conversion rate between inches and centimeters
  32.  *      (final int) INTERVAL – This is interval between each conversion
  33.  *      (final int) MAX_RANGE – This is maximum size of the range
  34.  *
  35.  *  Formulas:
  36.  *      centimeter = inch * CONVERSION_RATE {centimeter = inch * 2.54}
  37.  *
  38.  *  Initial Algorithm:
  39.  *  Prompt for the start number until a positive number is given
  40.  *  Prompt for the end number until a number within range is given
  41.  *  Display the table header
  42.  *  Calculate and display the conversion from inches to centimeters for each 6-inch increment
  43.  *      in the range, formatted into the table
  44.  
  45.  */
  46. public class WeissProgram2
  47. {
  48.  
  49.     /*
  50.      *  This method displays an inches-to-centimeters conversion table. The input is the smallest
  51.      *  number of inches and then the largest number of inches to be converted. The program then
  52.      *  displays the conversion for each 6-inch increment between the start and end numbers. The end
  53.      *  number must be between 6 and 36 inches larger than the start number. One inch is equivalent
  54.      *  to 2.54 centimeters. The method gives the option to continue creating new tables.
  55.      *
  56.      *  Refined Algorithm:
  57.      *  DO
  58.      *      min = 1
  59.      *      max = maximum Integer – MAX_RANGE
  60.      *      DO
  61.      *          Prompt startRange
  62.      *          IF startRange < min THEN
  63.      *              Display error message about negative number
  64.      *          ELSE IF startRange > max THEN
  65.      *              Display error message about being too big
  66.      *          END IF
  67.      *      LOOP WHILE startRange < min OR startRange > max
  68.      *
  69.      *      min = startRange + INTERVAL
  70.      *      max = startRange + MAX_RANGE
  71.      *      DO
  72.      *          Prompt endRange
  73.      *          IF endRange < min OR endRange > max THEN
  74.      *              Display error message about being out of range
  75.      *          END IF
  76.      *      LOOP WHILE endRange < min OR max
  77.      *
  78.      *
  79.      *      Display table header
  80.      *      FOR inch = startRange TO endRange [STEP inch by INTERVAL]
  81.      *          Calculate centimeter
  82.      *          Display inch and centimeter, formatted to two decimals
  83.      *      END FOR
  84.      *
  85.      *
  86.      *      Prompt userContinues
  87.      *  LOOP WHILE userContinues != “n” AND userContinues != “no”
  88.      */
  89.     public static void main( String[] args )
  90.     {
  91.         /*
  92.          *  This is the conversion rate between inches and centimeters
  93.          *  Currently 2.54cm per inch
  94.          */
  95.         final double CONVERSION_RATE = 2.54;
  96.  
  97.         /*
  98.          *  This is interval between each conversion in inches
  99.          */
  100.         final int INTERVAL = 6;
  101.  
  102.         /*
  103.          *  This is maximum size of the range
  104.          */
  105.         final int MAX_RANGE = 36;
  106.  
  107.         /*
  108.          *  This is the first inch to be converted
  109.          */
  110.         int startRange;
  111.  
  112.         /*
  113.          *  This is the highest possible inch to be converted
  114.          */
  115.         int endRange;
  116.  
  117.         /*
  118.          *  This is the smallest number in bounds for the current prompt
  119.          */
  120.         int min;
  121.  
  122.         /*
  123.          *  This is the highest number in bounds for the current prompt
  124.          */
  125.         int max;
  126.  
  127.         /*
  128.          *  This is the current inch being converted
  129.          */
  130.         int inch;
  131.  
  132.         /*
  133.          *  This is the number of centimeters equivalent to the current inch being converted, which
  134.          *  will be formatted to two decimal points
  135.          */
  136.         double centimeter;
  137.  
  138.         /*
  139.          *  This is the user’s response to whether they want to continue
  140.          */
  141.         String userContinues;
  142.  
  143.         /*
  144.          *  This object is used to capture the user's input
  145.          */
  146.         Scanner user_input = new Scanner(System.in);
  147.  
  148.  
  149.  
  150.         do //while User wants to continue
  151.         {
  152.             min = 1;
  153.             max = Integer.MAX_VALUE - MAX_RANGE;
  154.             do //while startRange is negative or too big
  155.             {
  156.                 System.out.print("Please enter the first inch: ");
  157.                 startRange = user_input.nextInt();
  158.  
  159.                 if (startRange < min)
  160.                 {
  161.                     System.out.print("That's a negative number. ");
  162.                 } //end if startRange is negative
  163.                 else if (startRange > max)
  164.                 {
  165.                     System.out.print("That's too big. ");
  166.                 } //end else if startRange is too big
  167.             } while (startRange < min || startRange > max);
  168.  
  169.             min = startRange + INTERVAL;
  170.             max = startRange + MAX_RANGE;
  171.             do //while endRange is too close to startRange or too big
  172.             {
  173.                 System.out.print("Please enter the highest inch (between "
  174.                     + min + " and " + max + "): ");
  175.                 endRange = user_input.nextInt();
  176.  
  177.                 if (endRange < min || endRange > max)
  178.                 {
  179.                     System.out.print("That's not in range. ");
  180.                 } //end if endRange is out of bounds
  181.             } while (endRange < min || endRange > max);
  182.  
  183.  
  184.             // Display table header
  185.             System.out.println(""
  186.                 + "       Conversion Chart\n"
  187.                 + "    Inches       Centimeters\n"
  188.                 + "  **********    *************");
  189.  
  190.             for (inch = startRange; inch <= endRange; inch += INTERVAL)
  191.             {
  192.                 centimeter = inch * CONVERSION_RATE;
  193.                 System.out.printf("  %10d    %13.2f\n", inch, centimeter);
  194.             } //end for each interval in table
  195.  
  196.  
  197.             System.out.print("Would you like to create another table? ");
  198.             userContinues = user_input.next();
  199.         } while (!userContinues.equalsIgnoreCase("n") && !userContinues.equalsIgnoreCase("no"));
  200.     } //end method main
  201. } //end class WeissProgram2
Advertisement
Add Comment
Please, Sign In to add comment