Computing Postage

The US postal service charges postage for letters based on the size and weight of the letter. Here are the rules that govern postage rates:

Program to compute postage

In this lab exercise you will write a program that prompts the user to enter the dimensions and weight of a letter. The program will use the rules above to compute and print the postage for the letter.

In addition to printing the postage, your program should also print the number of standard stamps needed to cover the postage for a letter. Standard postage stamps are worth $0.55 each, so one strategy for putting the correct postage on a letter is to put on enough standard postage stamps until you have met or exceeded the required postage.

Here is an example: suppose you wanted to send a 5 by 10 inch letter weighing 6 oz. This weight exceeds the 3.5 oz limit for normal sized letters, so the oversized rate applies here. 6 oz is 5 oz over 1 oz, so the postage for this letter would be $1.00 + 5 * $0.20 = $2.00. 3 standard stamps are worth 3 * $0.55 = $1.75, which is not enough. 4 standard stamps are worth 4* $0.55 = $2.20, which is enough to send this letter.

Strategies for solving the problem

The first thing you should do is set up variables to store the dimensions and weight of the letter. Since these can all take non-integer values you should use double type variables for all of these quantities. Write the code needed to prompt the user to enter these values and read them into the variables you have set up.

Another variable you should set is a variable that stores the postage for the letter. Since postage can be expressed as an integer number of cents, you should set this up as an int variable.

The first decision your program should make is whether or not the regular rate or the oversized rate applies to the letter. Write an if statement that can make this decision, and have your program print a message saying whether or not the letter is oversized. Test your program on several inputs to make sure that you can correctly distinguish regular sized mail from oversized mail. (Hint: since the decision is based on the dimensions and the weight of the letter, you will need to construct a compound test for your if statement.)

Next, fill in the logic for both the regular sized case and the oversized case that computes the number of cents of postage required for the letter. Have your program print this value, and then test your program on a number of test cases.

Finally, add the logic to determine how many standard stamps are required to meet or exceed the postage for the letter. One simple way to do this is to use integer division. For example, in the example above we needed $2.00 in postage, or 200 cents in postage. 200/55 = 3, so next we check that that 3*55 = 165 < 200. From this we can tell that we need 4 stamps instead of 3.

Helpful hints

Here are some suggestions on how to work around technical problems you will encounter in this exercise.

The first issue is rounding the weight of the item up to the nearest integer number of ounces. To do this, you should use the Math.ceil() method. This method takes a double and rounds it up to the nearest integer value. If the weight of the item is stored in a double variable named weight, you would do

int ozs = (int) Math.ceil(weight);

The second suggestion is to do all of your postage calculations in integer cents. When you print the dollar amount of the postage you can use the following code as a starting point:

System.out.print("This item costs $" + postage/100 + "." + postage%100);

Note that this won't work quite right in cases where postage%100 is less than 10. You should add an if statement to your program that checks for that case and does the right thing in that case as well.

How to turn in your work

Once you are done with the lab exercise you should compress your project folder into an archive and send me that archive as an attachment to an email message.

If you do not get done with the lab exercise by the end of the lab period, you can keep working on the exercise. You have until 5 PM of the day after the lab to finish your work and turn it in.