Conditional statements

In this section you will practice a bit with the idea of conditional logic and writing code that uses conditionals. We will create a short interactive application for calculating postage.

The U.S. Postal Service has some complex rules for deciding how much it costs to mail a letter. For this project we'll use a small part of these rules. (There have been a couple of rate increases since this lab was written, so the exact numbers are not current, but the calculation strategy is still the same.)

For an ordinary letter up to 3.5 ounces, the cost is .47 plus .21 for each additional ounce (or part of an ounce). Try some examples:

To translate this into code, it would be nice to have a slick way to take a float value and round "up" to the next whole number. Fortunately there is a function in the Math class, called ceil() (short for “ceiling”) that does exactly what we want. Try executing these statements:
package lab4;

    public class CeilingTest
    {
      public static void main(String[] args)
      {
        System.out.println(Math.ceil(1.3));
        System.out.println(Math.ceil(2.0));
        System.out.println(Math.ceil(2.1));
        System.out.println(Math.ceil(-2.1));
      }
    }
Now, to make things more interesting, let's add the rule for letters over 3.5 ounces: the cost is .94 plus .21 for each additional ounce (or part of an ounce).

For example,

Notice you have to check the actual weight to see if it is bigger than 3.5, but then round up when you calculate the extra ounces.