Starting from:

$25

COMP1210-Project 2 Variables and Expressions Solved

You will write two programs this week.  One will solve for the result of a specified formula after reading input values for x, y, and z, and the other will determine the number of barrels, gallons, quarts, and ounces for an input value of a raw amount in ounces.  

 

        •    Formula.java
 

Requirements: A program is needed that inputs values of type double for x, y, and z and solves for the result of the indicated formula when xyz is not equal to zero.  If xyz is equal to zero, then the result is zero.  

 

Design: The result should be calculated as follows (except for the special case):

 

(3𝑥 + 10.5)       (2𝑦 + 7.5)          (𝑧 + 5.5)             

                                                                                            𝑟𝑒𝑠𝑢𝑙𝑡 =                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     for                                                                               xyz   

𝑥𝑦𝑧

       

Special case:  

 

                                                             𝑟𝑒𝑠𝑢𝑙𝑡 = 0                                                                                                                                                                                                                                                         for                                                                  xyz =                      0    

 

Three examples of program output for the indicated input values are shown below.  Note that lines 2 through 4 for the input values begin with tab which is equivalent to three spaces in jGRASP (i.e., your program should use the escape sequence for a tab).

 

    Example #1
Line #
Program output











result = (3x + 10.5) (2y + 7.5) (z + 5.5) / xyz 

   Enter x: 1 

   Enter y: 1    Enter z: 1 result = 833.625 

 
             

 

 

                Example #2
Line #
Program output











result = (3x + 10.5) (2y + 7.5) (z + 5.5) / xyz 

   Enter x: 5.8 

   Enter y: 0.0    Enter z: 1.0 result = 0.0 

 
 

                 Example #3 
Line #
Program output











result = (3x + 10.5) (2y + 7.5) (z + 5.5) / xyz 

   Enter x: -1 

   Enter y: -2    Enter z: -3 result = -10.9375 

 
 

Code: Your numeric variables should be of type double.  Use an if-else statement to determine if the divisor in the formula is zero. Note that in Example #2, the value of y is zero so the divisor, xyz, is zero, which is the special case where the result is assigned zero.  

 

Test: You are responsible for testing your program, and it is important to not rely only on the examples above. Remember that the input values are doubles, so be sure to test both positive and negative values (with and without a decimal point) for x, y, and z.  You should use a calculator or jGRASP interactions to check your answers. 

 

 

        •    OilUnits.java 
 

Requirements: An oil company would like a program that allows the user to enter an amount of oil in ounces, which must not exceed 1 billion, and then displays the number of barrels, gallons, quarts, and ounces.  The number of each of these should be maximized from largest to smallest as indicated in Examples 2 and 3 below.  Your program should use the following conversion values in the computation:  

1 barrel = 42 gallons, 1 gallon = 128 ounces; 1 quart = 32 ounces.

 

Design: The oil company would like for the program’s I/O to be as shown in the three example runs below where (1) 1234567890 is entered, (2) 54321 is entered, and (3) 12345678 is entered.

 

Example 1 
Line #
Program output



Enter amount of oil in ounces: 1234567890 Amount must not exceed 1,000,000,000. 

 
 

Example 2 
Line #
Program output














 
Enter amount of oil in ounces: 54321 

Oil amount in units:  

   Barrels: 10 

   Gallons: 4 

   Quarts: 1 

   Ounces: 17 

54321 oz = (10 bl * 5376 oz) + (4 gal * 128 oz) + (1 qt * 32 oz) + (17 oz) 

 
 

                     Example 3                                                               
Line#
Program output














 
Enter amount of oil in ounces: 12345678 

Oil amount in units:  

   Barrels: 2296 

   Gallons: 18 

   Quarts: 2 

   Ounces: 14 

12345678 oz = (2296 bl * 5376 oz) + (18 gal * 128 oz) + (2 qt * 32 oz) + (14 oz) 

 
 

Your program must follow the above format with respect to the output.  Note that lines 3 through 6 for Examples 2 and 3 begin with tab (i.e., your program should use the escape sequence for a tab).   

 

Code: The input and output variables should be declared as type int.  A simple if-else statement can be used to check that the amount does not exceed one billion, where the true block prints the error message and the false block prints the normal output (or vice-versa).   Also, the return statement (return;) can be used in an if statement to return from main to immediately end the program after the error message.  In order to receive full credit for this assignment, you must calculate the number of barrels, gallons, quarts, and ounces and store them in separate variables. It is recommended as a practice that you do not modify input values once they are stored.

 

Test: You will be responsible for testing your program, and it is important to not rely only on the example above. For example, test with the following amounts: 16, 32, 128, and 5376. The last amount is the number ounces in a barrel (42 * 128).

More products