# Shipping Cost Calculator

Page 1 of 1

## 5 Replies - 9565 Views - Last Post: 16 November 2009 - 03:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=139461&amp;s=976cbc1e53fd3f5ddae61579437c7a20&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jeff125

Reputation: 0
• Posts: 9
• Joined: 27-October 09

# Shipping Cost Calculator

Posted 16 November 2009 - 12:24 PM

Hi, I have a problem with this question. This problem is dealing with if statements. I'm not sure if the calculation is right.

Here is the program sample

Weight of Order:
5
Shipping Cost: \$3.00

Weight of Order
20
Shipping Cost: \$5.50

Weight of Order
0

bye

A mail order company charges \$3.00 for handling, free shipping for orders 10 pounds or less, plus \$0.25 for each pound over 10. Write a program that repeatedly asks the user for the weight of an order, then writes out the shipping charge. The program stops when a weight of zero or less is entered.

``` import java.util.Scanner;

class ShippingCostCalculator

{

public static void main (String[] args )

{

Scanner scan=new Scanner( System.in );

double weightoforder;

System.out.print ("Weight of Order: ");
weightoforder = scan.nextDouble ();

while (!( weightoforder == 0))

{

System.out.println ("Shipping Cost: \$"+ (3 + .25*(weightoforder-10)));

System.out.println ();

System.out.print ("Enter weight of order (-1 to exit): ");

weightoforder = scan.nextDouble ();

}

System.out.println ("Bye");

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Shipping Cost Calculator

### #2 nick2price

• D.I.C Lover

Reputation: 565
• Posts: 2,826
• Joined: 23-November 07

## Re: Shipping Cost Calculator

Posted 16 November 2009 - 12:33 PM

Not looked at your code, but these

Quote

Weight of Order:
5
Shipping Cost: \$3.00

Weight of Order
20
Shipping Cost: \$5.50

Are correct. Take first one. Order is under 10 pounds, so they only have to pay the 3 dollar charge.
Now the second one. They have to pay the 3 dollar charge, plus 10 pounds worth of extras, so 10 *.25 = 2.50, 3 +2.5 = 5.50..

### #3 TriggaMike

• Using up all your 1's and 0's

Reputation: 85
• Posts: 1,103
• Joined: 26-September 08

## Re: Shipping Cost Calculator

Posted 16 November 2009 - 12:34 PM

Also I'm reposting your code with some formatting.
```import java.util.Scanner;

class ShippingCostCalculator

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

double weightoforder;

System.out.print("Weight of Order: ");
weightoforder = scan.nextDouble();

while(!(weightoforder == 0))

{

System.out.println("Shipping Cost: \$" + (3 + .25 * (weightoforder - 10)));

System.out.println();

System.out.print("Enter weight of order (-1 to exit): ");

weightoforder = scan.nextDouble();

}

System.out.println("Bye");

}
}
```

### #4 jeff125

Reputation: 0
• Posts: 9
• Joined: 27-October 09

## Re: Shipping Cost Calculator

Posted 16 November 2009 - 01:31 PM

nick2price, on 16 Nov, 2009 - 11:33 AM, said:

Not looked at your code, but these

Quote

Weight of Order:
5
Shipping Cost: \$3.00

Weight of Order
20
Shipping Cost: \$5.50

Are correct. Take first one. Order is under 10 pounds, so they only have to pay the 3 dollar charge.
Now the second one. They have to pay the 3 dollar charge, plus 10 pounds worth of extras, so 10 *.25 = 2.50, 3 +2.5 = 5.50..

My output is this

Quote

Weight of Order: 5
Shipping Cost: \$1.75

Enter weight of order (-1 to exit): 20
Shipping Cost: \$5.5

Enter weight of order (-1 to exit): 0
Bye

TriggaMike, on 16 Nov, 2009 - 11:34 AM, said:

Also I'm reposting your code with some formatting.
```import java.util.Scanner;

class ShippingCostCalculator

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

double weightoforder;

System.out.print("Weight of Order: ");
weightoforder = scan.nextDouble();

while(!(weightoforder == 0))

{

System.out.println("Shipping Cost: \$" + (3 + .25 * (weightoforder - 10)));

System.out.println();

System.out.print("Enter weight of order (-1 to exit): ");

weightoforder = scan.nextDouble();

}

System.out.println("Bye");

}
}
```

Hi, there is no error but there is a miscalculation.

Quote

Weight of Order: 5
Shipping Cost: \$1.75

Enter weight of order (-1 to exit): 20
Shipping Cost: \$5.5

Enter weight of order (-1 to exit): 0
Bye

The shipping cost should be \$3.00 instead its \$1.75.

Here is the program output should be:

Quote

Weight of Order:
5
Shipping Cost: \$3.00

Weight of Order
20
Shipping Cost: \$5.50

Weight of Order
0

bye

### #5 Atspulgs

Reputation: 89
• Posts: 507
• Joined: 29-July 09

## Re: Shipping Cost Calculator

Posted 16 November 2009 - 01:47 PM

How about checking if the weight is actually over 10 pounds before doing anything?
Cuz you should just be printing out 3.0 if it is less but instead you are multiplying .25 with -5 and then adding it to 3 now

-5 * 0.25 = -1.25
3- 1.25 = 1.75

so yeah it works correctly just not the way you need

This post has been edited by Atspulgs: 16 November 2009 - 01:48 PM

### #6 sakshamkum

Reputation: 19
• Posts: 232
• Joined: 09-June 09

## Re: Shipping Cost Calculator

Posted 16 November 2009 - 03:38 PM

you may change the print statement
```System.out.println("Shipping cost:"+(3+((weighttoforder>=10)?weighttoforder*0.25:0)));

```