First class mail has no insurance, and costs \$1 plus 17 cents for each ounce up to a maximum of 13 ounces.

The first trick here is that your weight is stored in pounds and the description of the cost formula is in ounces. You do remember how to convert pounds to ounces, right? (If not, ask somebody.)

The second trick is that charges go up in 17 cent increments from a dollar. So, a .5 ounce package costs \$1.17, and a 1.5 ounce package costs \$1.34. You will need to do a little math here. You can google on Math.ceil() and Math.floor(). Math.ceil(double x) rounds x up to the next largest integer-equivalent number. So Math.ceil(1.5) returns 2.0. Math.floor(double x) rounds x down to the next largest integer-equivalent number. So Math.floor(1.5) returns 1.0.

I need help with coming up with that sort of math method. I can't figure it out.

`````` public double calculateCostOfPostage()
{

double cost= 0;
insuranceAmount=0.0;
weight=super.returnWeight();
double ounce= 0;
ounce = weight*16;
if (ounce<=13)
{

cost = 1.0 + Math.ceil(ounce*.17);
cost = cost +insuranceAmount;

}
return cost;
}
``````

That is what i have so far.

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by darkagn

>> You can google on Math.ceil() and Math.floor(). Math.ceil(double x) rounds x up to the next largest integer-equivalent number. So Math.ceil(1.5) returns 2.0. Math.floor(double x) rounds x down to the next largest integer-equivalent number. So Math.floor(1.5) returns 1.0.

These statements are a little misleading. Math.ceil() does not round the result, it returns the next highest integer. So Math.ceil(1.1) also returns 2. Similarly, Math.floor() returns the next lowest integer. So Math.floor(1.9) returns 1.

Ok, that said, your method is almost there I think. However, you shouldn't be calling Math.ceil on ounce*.17. You should be calling it on ounce and then multiplying its result by .17 unless I have misunderstood the problem at hand.

Hope this helps,
darkagn

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.