4 Replies - 8878 Views - Last Post: 26 October 2010 - 04:10 PM Rate Topic: -----

#1 Guest_Pee Jay*


Reputation:

Java - Shipping Program

Posted 25 October 2010 - 07:45 PM

The assignment has the following rates:
Weight of Package Rate per 500 miles shipped
2 pounds or less $1.10
over 2 pounds but not more than 6 pounds $2.20
over 6 pounds but not more than 10 pounds $3.70
over 10 pounds $3.80

The shipping charge per 500 miles are not prorated. For example, if a 2 pound package is shipped 502 miles, the charge would be $2.20. Write a program that ask the user to enter the weight of a package and then display the shipping charges.

****My issue:*****
I have been debugging my program for the past 2 days and have made great strides getting rid of the syntax errors, now it compiles but the output is not what I am expecting. At first I had an else statement on Line 50, but it would not compile, it gave me an error: 'else without if'. But I thought the if statement (Line 32) went with it. So I tried to do a nested if statement and else if statements in those. The program will compile but it does not give me the results I expect. For example, I input "5" for the weight and "685" for the distance, and it should give me a shipping charge of $4.40, however, it returns: "Shipping cost: $0.00".

I figured that the shipping cost should be the shipping rate per 500 miles times (integer division distance /500) if there is no remainder, if there is a remainder, then it would kick up the (integer division distance / 500) by 1. Please help me see what I am doing wrong.

Here is my code:

/************************************************************************************************
Programmer:		Paul J. Williams																						*
Date:				October 24, 2010																						*
Purpose:			This program will calculate the shippping charges for a package based on the 		*
					weight of the package and the distance of the shipment entered by the user.		*
*************************************************************************************************/

import java.util.Scanner;			// Required for the Scanner class.

public class ShippingCharge
{
	public static void main (String[] args)
	{ 

int weight = 0;											// Declare and initialize variable to hold the entered weight.
double distance = 0.0;									// Declare and initialize variable to hold the entered distance.
double rate;												// This variable will hold the calculated rate.
int distanceMultiplier = (int)distance / 500;	// This will hold the increments of the shipping charge.
int distanceRemainder;									// This will decide if the shipping charge advance up one level.

Scanner input = new Scanner(System.in);			// Create a Scanner object for the input.

// Get the weight of the package.
System.out.println("What is the weight of the package (in pounds) ?");
weight = input.nextInt();

// Get the shipping distance of the package.
System.out.println("What is the shipping distance (in miles) ?");
distance = input.nextDouble();

distanceRemainder = (int)distance % 500; 
if (distanceRemainder == 0)
	{
	if (weight <= 2)
		System.out.println("Total Shipping Cost is: $ " + (distanceMultiplier * 1.10));
		}
	else if (weight > 2 && weight <= 6)
	{
		System.out.println("Total Shipping Cost is: $ " + (distanceMultiplier * 2.20));
		}
	else if (weight > 6 && weight <= 10)
	{
		System.out.println("Total Shipping Cost is: $ " + (distanceMultiplier * 3.70));
		}
	else
	{
		System.out.println("Total Shipping Cost is: $ " + (distanceMultiplier * 3.80));
		}
		
if (distanceRemainder != 0)
	{
		if (weight <= 2)
		System.out.println("Total Shipping Cost is: $ " +(distanceMultiplier + 1) * 1.10);
		}
	else if (weight > 2 && weight <= 6)
	{
		System.out.println("Total Shipping Cost is: $ " +(distanceMultiplier + 1) * 2.20);
		}
	else if (weight > 6 && weight <= 10)
	{
		System.out.println("Total Shipping Cost is: $ " +(distanceMultiplier + 1) * 3.70);
		}
	else
	{
		System.out.println("Total Shipping Cost is: $ " +(distanceMultiplier + 1) * 3.80); 
		
		System.exit(0);
}

	}
}




Is This A Good Question/Topic? 0

Replies To: Java - Shipping Program

#2 Guest_Pee Jay*


Reputation:

Re: Java - Shipping Program

Posted 25 October 2010 - 07:50 PM

View PostPee Jay, on 25 October 2010 - 06:45 PM, said:

The assignment has the following rates:
Weight of Package Rate per 500 miles shipped
2 pounds or less $1.10
over 2 pounds but not more than 6 pounds $2.20
over 6 pounds but not more than 10 pounds $3.70
over 10 pounds $3.80

The shipping charge per 500 miles are not prorated. For example, if a 2 pound package is shipped 502 miles, the charge would be $2.20. Write a program that ask the user to enter the weight of a package and then display the shipping charges.

****My issue:*****
I have been debugging my program for the past 2 days and have made great strides getting rid of the syntax errors, now it compiles but the output is not what I am expecting. At first I had an else statement on Line 50, but it would not compile, it gave me an error: 'else without if'. But I thought the if statement (Line 32) went with it. So I tried to do a nested if statement and else if statements in those. The program will compile but it does not give me the results I expect. For example, I input "5" for the weight and "685" for the distance, and it should give me a shipping charge of $4.40, however, it returns: "Shipping cost: $0.00".

I figured that the shipping cost should be the shipping rate per 500 miles times (integer division distance /500) if there is no remainder, if there is a remainder, then it would kick up the (integer division distance / 500) by 1. Please help me see what I am doing wrong. A friend suggested i try a while loop, but we do not go over that until the next chapter, or a try /catch. We have not discussed that either yet.

Here is my code:

/************************************************************************************************
Programmer:		Paul J. Williams																						*
Date:				October 24, 2010																						*
Purpose:			This program will calculate the shippping charges for a package based on the 		*
					weight of the package and the distance of the shipment entered by the user.		*
*************************************************************************************************/

import java.util.Scanner;			// Required for the Scanner class.

public class ShippingCharge
{
	public static void main (String[] args)
	{ 

int weight = 0;											// Declare and initialize variable to hold the entered weight.
double distance = 0.0;									// Declare and initialize variable to hold the entered distance.
double rate;												// This variable will hold the calculated rate.
int distanceMultiplier = (int)distance / 500;	// This will hold the increments of the shipping charge.
int distanceRemainder;									// This will decide if the shipping charge advance up one level.

Scanner input = new Scanner(System.in);			// Create a Scanner object for the input.

// Get the weight of the package.
System.out.println("What is the weight of the package (in pounds) ?");
weight = input.nextInt();

// Get the shipping distance of the package.
System.out.println("What is the shipping distance (in miles) ?");
distance = input.nextDouble();

distanceRemainder = (int)distance % 500; 
if (distanceRemainder == 0)
	{
	if (weight <= 2)
		System.out.println("Total Shipping Cost is: $ " + (distanceMultiplier * 1.10));
		}
	else if (weight > 2 && weight <= 6)
	{
		System.out.println("Total Shipping Cost is: $ " + (distanceMultiplier * 2.20));
		}
	else if (weight > 6 && weight <= 10)
	{
		System.out.println("Total Shipping Cost is: $ " + (distanceMultiplier * 3.70));
		}
	else
	{
		System.out.println("Total Shipping Cost is: $ " + (distanceMultiplier * 3.80));
		}
		
if (distanceRemainder != 0)
	{
		if (weight <= 2)
		System.out.println("Total Shipping Cost is: $ " +(distanceMultiplier + 1) * 1.10);
		}
	else if (weight > 2 && weight <= 6)
	{
		System.out.println("Total Shipping Cost is: $ " +(distanceMultiplier + 1) * 2.20);
		}
	else if (weight > 6 && weight <= 10)
	{
		System.out.println("Total Shipping Cost is: $ " +(distanceMultiplier + 1) * 3.70);
		}
	else
	{
		System.out.println("Total Shipping Cost is: $ " +(distanceMultiplier + 1) * 3.80); 
		
		System.exit(0);
}

	}
}



Was This Post Helpful? 0

#3 TokoYami200  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 44
  • Joined: 18-October 10

Re: Java - Shipping Program

Posted 25 October 2010 - 08:17 PM

Your variable distanceMultiplier is zero because 0/500 = 0.

Also, you're converting between int and float a lot so you're probably losing precision left and right.

Finally, your println statements are only printing distanceMultiplier * whatever-rate.

distanceMultiplier * 1.10 for ex. this is just printing the variable distanceMultiplier (which we learned above is zero by default) times 1.10 which is equal to zero.

I'm sleepy so I'm not exactly sure what your proggy is supposed to do (:P), but I think you need to move distanceMultiplier = (int)distance / 500 to AFTER you get the input (i.e. line 30) as well as fix the print statements.

Oh and small note, I put the code in bold because putting it in code blocks would make the lines look weird.

This post has been edited by TokoYami200: 25 October 2010 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Pee Jay*


Reputation:

Re: Java - Shipping Program

Posted 26 October 2010 - 05:48 AM

I moved the distanceMultiplier variable, but I don't see what I need to do with the println statements. I realized that I declared a variable "rate" and never used it. It was my intentions to use it in the println stmt. I have changed the println stmt to be: System.out.println("Total Shipping Cost is: $ " + (rate = distanceMultiplier * 1.10));

for each of the println statements, (using the appropiate rates) since rate is suppose to hold the shipping cost. But it is still not working for me. Not looking for anyone to give me an answer, but a little guidance would help me a lot. :P
Was This Post Helpful? 0

#5 wreckmyhead  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 21-October 10

Re: Java - Shipping Program

Posted 26 October 2010 - 04:10 PM

First of you don't need to test for 2 values of distanceRemainder
If the distance is !=0 just add 1 as so.


if (distanceRemainder != 0)
{
distanceMultiplier++;
} 




This removes a whole set of if statements and makes your code easier on the eye.
Also you dont need all those print statements. You should use the if statements to establish the rate and then just print the rate..eg:

if (weight <= 2) 
 {
  rate = (distanceMultiplier * 1.10);  
 } 
 else if (weight > 2 && weight <= 6)  
 {  
  rate = (distanceMultiplier * 2.20);  
 }  //etc etc

System.out.println("Total Shipping Cost is: $ " + (rate));  




Hopefully, you should now find it easier to check for errors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1