Question
This homework assignment revisits a previous homework, except that you now implement functions and loops for menu and loops for input validation. HW8 (Graded out
This homework assignment revisits a previous homework, except that you now implement functions and loops for menu and loops for input validation.
HW8 (Graded out of 100)
The Fast Freight Shipping company charges the following rates:
Weight of package (in kilograms) | Rate per 500 miles shipped |
---|---|
2 kg or less | $1.10 |
Over 2 kg but not more than 6 kg | $2.20 |
Over 6 kg but not more than 10 kg | $3.70 |
Over 10 kg but not more than 20 kg | $4.80 |
Write a program that asks for the weight of the package and the distance to be shipped, then displays the charges. Note: To calculate the charges, the distance is rounded up to the next higher multiple of 500 miles. For example, a distance of 510 miles is rounded up to 1,000 miles, or 2 times 500 miles. If the package weights say 1kg, the charge will be $1.10 times 2, that is $2.20. If the package weights 5.5 kg, the charge will be $2.20 times 2, that is $4.40. A distance which is an integral multiple of 500 miles, say 2,000 miles, is not rounded up. If the distance is 2,000 miles and the weight is 3 kg, the charge will be $2.20 times 4, that is $8.80.
1. Additional requirements Make sure you meet all the requirements to avoid losing points
-
Make sure you follow the requirements in the Homework Notes.
-
You are required to implement the following functions and call them in the main function: -getWeight: prompts the user to enter the weight, and checks the weight inputted is valid. The function should use a while loop for input validation and prompt the user to reenter the weight. After 5 invalid reattempts, the function should print Too many invalid attempts, exiting and exit with EXIT_FAILURE. If the input is valid, the function returns the weight as a double. A weight is valid if it is > 0 and <= 20 kg. 20 kg is the maximum weight the company will ship.an input.
-getDistance: prompts the user to enter the distance, and checks the distance inputted is valid. The function should use a while loop for input validation and prompt the user to reenter the distance. After 5 invalid reattempts, the function should print Too many invalid attempts, exiting and exit with EXIT_FAILURE. The function returns the distance as an int if the distance is valid. A distance is valid only if it is >= 10 miles and <= 3,000 miles. Those are the companys minimum and maximum shipping distances.
-calculateCharges: takes as arguments the weight and the distance, and calculates the shipping charges. Returns the charges as a double.
-
You must use a function prototype for all the above functions
-
The shipping charges are displayed with a decimal point and two digits after the decimal point
-
Your main() function implements the following pseudocode
The main function should loop on printing the following menu, as long as the user does not choose 2 1. Ship Package 2. Quit If the user chooses 1, the main function should do the following: call getWeight call getDistance call calculateCharges print the charges If the user chooses 2, the main function should print Exiting and terminate.
2. Implementation hint
To round up a distance to the next higher multiple of 500 miles, you can use this formula: roundedMultiple = ((distance - 1) / 500) + 1; For example, if distance is 510 miles, it is rounded up to 2 times 500 miles, and roundedMultiple will be equal to 2. If distance is 1500 miles, it is equal to 3 times 500 miles, and not rounded up, and roundedMultiple will be equal to 3. Note roundedMultiple should be declared an int. You may assume the distance has no fractional part.
3. Grading criteria
- Source code inspection (grader) *Style (refer to "Homework Notes"): 5 points
- Program compilation and execution (zyLabs or grader) *Loop over menu, valid inputs: 15 points (test-1) *Loop for weight input validation: 17 points (test-2) *Loop for distance input validation: 17 points (test-3) *getWeight unit test: 15 points *getDistance unit test: 15 points *calculateCharges unit test: 16 points
Test-1: The code's output should exactly match:
1->Ship package, 2-> Quit: 1 Enter the weight of the package in kilograms (max 20 Kg): 5.5 Weight entered is 5.50 Enter the distance to be shipped: (min 10 Mi, max 3000 Mi): 510 Distance entered is 510 The shipping charge is $4.40 1->Ship package, 2-> Quit: 1 Enter the weight of the package in kilograms (max 20 Kg): 1 Weight entered is 1.00 Enter the distance to be shipped: (min 10 Mi, max 3000 Mi): 11 Distance entered is 11 The shipping charge is $1.10 1->Ship package, 2-> Quit: 2 Exiting
Test-2: The code's output should exactly match:
1->Ship package, 2-> Quit: 1 Enter the weight of the package in kilograms (max 20 Kg): 21 Weight entered is 21.00 Weight is invalid, must be between 0 and 20 kg, please reenter: 22 Weight is invalid, must be between 0 and 20 kg, please reenter: -2 Weight is invalid, must be between 0 and 20 kg, please reenter: -1 Weight is invalid, must be between 0 and 20 kg, please reenter: 21 Weight is invalid, must be between 0 and 20 kg, please reenter: 23 Too many invalid attempts, exiting
Test-3: The code's output should exactly match:
1->Ship package, 2-> Quit: 1 Enter the weight of the package in kilograms (max 20 Kg): 19 Weight entered is 19.00 Enter the distance to be shipped: (min 10 Mi, max 3000 Mi): 9 Distance entered is 9 Distance must be between 10 and 3000 miles, please reenter: 8 Distance must be between 10 and 3000 miles, please reenter: 3001 Distance must be between 10 and 3000 miles, please reenter: 3002 Distance must be between 10 and 3000 miles, please reenter: 7 Distance must be between 10 and 3000 miles, please reenter: 7 Too many invalid attempts, exiting
getWeight unit test: The function is expected to prompt the user and print the weight inputted by the user. The function should return the weight typed by the user (10.5)
getDistance unit test: The function is expected to prompt the user and print the distance inputted by the user. The function should return the distance typed by the user (50)
calculateCharges unit test: The function is expected to return the correct charges value for these tuples of (weight, distance): (2.2, 11), (11.5, 1950), (18.7, 100)
please follow the instruction
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started