5 Replies - 5505 Views - Last Post: 16 November 2009 - 03:38 PM Rate Topic: -----

#1 jeff125  Icon User is offline

  • New D.I.C Head

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

Shipping Cost Calculator

Post icon  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

The question asks

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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • 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..

Is this your output?
Was This Post Helpful? 0
  • +
  • -

#3 TriggaMike  Icon User is offline

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

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

Re: Shipping Cost Calculator

Posted 16 November 2009 - 12:34 PM

What exactly is your error?

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");

	}
}

Was This Post Helpful? 0
  • +
  • -

#4 jeff125  Icon User is offline

  • New D.I.C Head

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

Re: Shipping Cost Calculator

Posted 16 November 2009 - 01:31 PM

View Postnick2price, 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..

Is this your output?


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


View PostTriggaMike, on 16 Nov, 2009 - 11:34 AM, said:

What exactly is your error?

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

Was This Post Helpful? 0
  • +
  • -

#5 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • 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 :D

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

so yeah it works correctly just not the way you need :D

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

Was This Post Helpful? 0
  • +
  • -

#6 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • 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)));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1