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

New Topic/Question
Reply
MultiQuote









|