computer science
CIST 1400, Introduction to Computer Programming Programming Assignment
Page 1 of 4
Program 9 :: 40 points :: Due See Blackboard @ 11:59:59 PM
The You Package It We Savage It Shipping Company charges the following shipping costs to ship packages: Weight of Package (in pounds) Rate Per 25 miles (or partial 25 miles) shipped From 1 lb up to and including 8 lbs $ 1.10 From 9 lbs up to and including 17 lbs $ 3.75 From 18 lbs up to and including 20 lbs $ 5.01 From 21 lbs up to and including 36 lbs $ 6.89 From 37 lbs up to and including 80 lbs $ 8.14 Write a program that asks for the distance a package is to be shipped (an integer value) and then the weight of a package in pounds (integer value). With this information, calculate and display the shipping charges for that package. You may assume that all distances and weights will be integer values. You may not assume, however, that the values are within the appropriate ranges. Because of this, your program will need to continuously prompt the user to enter a distance until they enter a value greater than 0 (1 or above). Any value less than 1 should cause your program to re-prompt for a distance. Once a valid distance has been obtained, your program will need to continuously prompt the user for a valid weight until they enter a value between 1 and 80 inclusive. Any value outside of that range should cause your program to re-prompt for a weight. The company cannot ship packages less than 1 pound or over 80 pounds. Output your results with two significant digits after the decimal place. To calculate the shipping cost, figure the shipping rate based on the weight, then use the number of miles to calculate the actual shipping cost. For example, for a package that weighs 22 pounds that is being shipped 55 miles, you should charge the individual $20.67 because: you should charge $6.89 (based on the weight) for the first 25 miles, $6.89 for the second 25 miles and another $6.89 for the remaining 5 miles, which is a fraction of 25 miles. Do not pro-rate the shipping charges based on partial mileage (like 5/25 miles). Goals
Create int and double variables and use them appropriately in the program
Possibly use the && operator along with selection structure(s) to determine the
shipping rate. Use arithmetic to calculate how many times the rate needs to be charged based on
the distance given. Use looping structures to perform data validation on user input.
Class and File Naming
Name your class NetID_Shipping and source file NetID_Shipping.java, using
your UNO NetID. For example, for Loki account jsmith, he or she would create a
class jsmith_Shipping.
Submitting Your Program
See the document in Blackboard Course Materials for how to submit your assignment. Grading Notes
Remember that your program requires a complete, digitally signed Honor Pledge to be graded.
Name your class according to the “Class and File Naming” section above. Your program must follow the output requirements as outlined. Do not use any material from beyond Lecture 9. Format the output of your result to have two decimal places. You must prompt for the values in the order distance and then weight. You must validate the distance (at least 1 mile) before prompting for the weight. You must validate the weight (between 1 and 80 inclusive) before calculating the final
shipping cost. At this point it is expected that you should be able to distinguish when to use just an
if structure and when to use if/else or if/else if when appropriate. Be sure
to use the appropriate structure for the situation. Be sure to use the proper type of loop code for the situation, i.e. while or do/while
for indefinite, a for loop for definite
You must have some sort of an introductory statement, though you may determine the name of your company and exactly what it says (remember – it must be clean and appropriate!)
Testing your Program
Since there are so many ranges and boundaries in this program (ranges for weight and ranges for distance), you should test a number of different inputs in your program. For instance, pick a distance of 15 miles and then test each of the following weights with it: 1, 3, 4, 5, 11, 12, 13, 21, 22, 23, 44, 45, 46, 64, 65. Then pick a particular weight such as 25 and then a few different distances: 1, 10, 24, 25, 26, 45, 50, 51, 75, 902. You should manually calculate the desired results or run the sample program below for each of those combinations and then see if your program generates the correct results.
Suggested Approach to Solution Writing more complex programs like this is easier if you break it down into steps and code and test each step along the way. Here is how you might approach writing the solution to this program:
1. Write a basic program that prompts the user for an integer value for the distance to ship the package. Compile, run and test your program to make sure it reads the value correctly.
2. Modify the program to use a loop to validate the input from the user to make sure that the user enters a value greater than or equal to 1. After the loop, have an output statement that prints the distance the user entered. This output statement is a debugging tool that will be removed when you are done with your program. If you haven't covered sentinel-controlled loops yet in class, you can skip this step, but remember to come back to it and finish it before handing in your program.
3. Testing for the distance should include attempting to enter a bunch of values less than 1. You should continuously be prompted by the program to enter a distance until you enter a value greater than or equal to 1.
4. Once you are certain that you are entering and validating the distance correctly, do the
same basic coding thing for the weight. Add code to your program that will then prompt the user for an integer value for the weight of the package to be shipped.
5. Modify the program to use a loop to validate the input from the user to make sure that the user enters a value greater than or equal to 1 and less than or equal to the maximum weight. After the loop, have an output statement that prints the weight the user entered. This output statement is a debugging tool that will be removed when you are done with your program. If you haven't covered sentinel-controlled loops yet in class, you can skip this step, but remember to come back to it and finish it before handing in your program.
6. Compile and test this portion of your code. Testing should include attempting to enter a bunch of values less than 1 and greater than the maximum weight. You should continuously be prompted by the program to enter a weight until you enter a value between 1 and the maximum weight, inclusive. Then the program should end.
7. Once you have these two inputs working correctly, determine the shipping rate based on the weight using some sort of selection statement. After determining the shipping rate, have an output statement that prints the shipping rate. This output statement is a debugging tool that will be removed when you are done with your program. You may wish to store the shipping rate into a separate variable.
8. Compile and test your current program. Testing should include running the program a number of times, entering various values for the weight (remember to test all boundaries). Given the shipping rate chart given at the beginning of this program description, you should know if your program accurately calculates the shipping rate based on the weight of the package.
9. Finally, add code to your program that will determine how many units of 25 miles the package is being shipped (remember that parts of 25 count as a full 25, so 75 is 3 units of 25 while 76 is 4 units of 25). Output the “parts of 25” so you know if your program is
calculating how many units of 25 there are correctly. This output is a debugging step and should be removed before submitting the program. Multiply this number by the rate you determined in step 7 and output the result.
10. Test your program with a few sets of values against the sample program available on Loki to see if you get the same results. Remember to take out the debugging statements from your code before submitting it.
A Sample Program Run (user input is underlined) Welcome to the You Package It We Savage It Shipping Company. How far will you be shipping the package in miles? 0 That is not a valid distance, try again. How far will you be shipping the package in miles? -1 That is not a valid distance, try again. How far will you be shipping the package in miles? -91 That is not a valid distance, try again. How far will you be shipping the package in miles? 0 That is not a valid distance, try again. How far will you be shipping the package in miles? 678 How heavy is your package in pounds (1 - 80)? 0 That is not a valid weight, try again. How heavy is your package in pounds (1 - 80)? -1 That is not a valid weight, try again. How heavy is your package in pounds (1 - 80)? 81 That is not a valid weight, try again. How heavy is your package in pounds (1 - 80)? 100 That is not a valid weight, try again. How heavy is your package in pounds (1 - 80)? -1 That is not a valid weight, try again. How heavy is your package in pounds (1 - 80)? 39 The shipping rate for 39 pounds is $8.14 per 25 miles. Your total shipping cost for 678 miles is $227.92