Posted by AB on Monday, May 21, 2012 at 9:01pm.
First thing that comes to mind is
(mile/500) will truncate to the next lower integer, namely zero if mile<500.
All you need to do to correct is to write
(mile/500.0) which will convert integer division to force floating division.
Also, if the program does not work, you will help everyone by stating what made you think that it's not working, such as:
execution exception (indicate message and line number), compilation errors (indicate message & line number), invalid values (indicate what you see and what you expect), input values, etc.
Thank you for your help. I apologize. You were right when you said "First thing that comes to mind is
(mile/500) will truncate to the next lower integer, namely zero if mile<500." I changed it to (mile/500.0), but the answer I get at the bottom was still 0. Also, if I put 9 in for the weight and 950 for mileage I get $3.70. I thought it should go up to $7.40 since it is not prorated. I put examples of what I get when I run them.
Enter the package weight: 2
Enter the miles shipped: 400
Total charges will be $0.00
BUILD SUCCESSFUL (total time: 3 seconds)
Enter the package weight: 9.5
Enter the miles shipped: 950
Total charges will be $3.70
BUILD SUCCESSFUL (total time: 6 seconds)
Also, when I enter
if (weight <=2.0);
{
charges = 1.10 * (miles/500);
The charges = charges = 1.10 * (miles/500); is underlined yellow and I get a symbol of a yellow light bulb with a caution symbol that states "this assigned value is never used."
I'm new to this and I don't know what that means.
This is the assignment as well:
The Fast Freight Shipping Company charges 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 charges per 500 miles are not prorated. For example, if a 2- pound package is shipped 550 miles, the charges would be $ 2.20. Write a program that asks the user to enter the weight of a package and then displays the shipping charges.
Thank you for posting the original question. It always helps (if accompanied by your efforts)!
You may want to review your logic for the prices according to 2 criteria:
1. Basic according to weight:
According to following scale:
( = excluding
] = upto and including
(0-2] $1.1
(2-6] $2.2
(6-10] $3.7
(10-∞) $3.8
Above price schedule cannot be made into a simple algebraic formula, so it is best to use a table, or as you did it, with if statements.
2. Milage:
Basic shipping charges are then adjusted according to milage. Basic charges are proportional to slices of distances of 500 miles or part thereof. So for shipping any distance up to 500 miles, final charge equals basic. From 501 to 1000, that makes two slices of 500 miles, so basic charges are to be doubled, and so on.
The strategy is to calculated basic charges, X. If milages are over 500, then multiply X by the number of chunks, N, of 500 miles or part thereof.
To calculate N, you can make use of the truncation as you originally did, but you need to add 1 to account for the cost of the first chunk:
N = (miles/500)+1
where N is the factor due to milage.
This way, the milage is accounted for at the end, and only once.
Also, to calculate the weight charges, you can use the construct:
if weight<=2
{...}
elseif weight<=6
{...}
elseif weight<=10
{...}
else // i.e. over 10
{...}
end if
Please post if you have questions.
I had to change the code completely, but now I am having another problem. I only need to enter the weight for this problem. My code will prompt to enter a weight and then it will give a total, but once I click okay on that total another box comes up stating that the total charges are = $0.00. I don't know how to get rid of that.
Here is my new code:
package shipcharges;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class ShipCharges
{
public static void main (String [] s)
{
String input;
int distance = 0;
int distanceMultiplier = (distance / 500);
int distanceRemainder = (distance % 500);
double weight;
double total = 0;
double rate;
DecimalFormat df = new DecimalFormat("#.##");
input = JOptionPane.showInputDialog ("Enter package weight: ");
weight = Double.parseDouble(input);
int miles = 0;
if (weight <= 2)
JOptionPane.showMessageDialog(null, "Shipping charges will be $" +
(miles < 500 ? 1.10 : ((miles/500)*1.10)));
else if (weight <= 6)
JOptionPane.showMessageDialog(null, "Shipping charges will be $" +
(miles < 500 ? 2.20 : ((miles/500)*2.20)));
else if (weight <= 10)
JOptionPane.showMessageDialog(null, "Shipping charges will be $" +
(miles < 500 ? 3.70 : ((miles/500)*3.70)));
else
JOptionPane.showMessageDialog(null, "Shipping charges will be $" +
(miles < 500 ? 3.80 : ((miles/500)*3.80)));
JOptionPane.showMessageDialog (null, "Your shipping charges are $" +
df.format(total));
System.exit(0);
}
}
I figured it out. I forgot to delete "JOptionPane.showMessageDialog (null, "Your shipping charges are $" +
df.format(total));
Happy to hear that you have got it working! Congrats!
Even though the code works as it is, there are traps lurking around to get you as you go on.
1. Your outputting "total" at the end is the proper way to do things, as the showMessageDialog will be used only once for the same message. The way you have it, if you make any change to the dialog, you need to change multiple places, which makes the code difficult to maintain.
2. you have defined
int distance = 0;
int distanceMultiplier = (distance / 500);
int distanceRemainder = (distance % 500);
but have not used it.
Notice that distanceMultiplier will be zero as it is, so is distanceRemainder.
If you use the same pricing as before, the distance multiplier will be
distanceMultiplier=(distance/500)+1;
DistanceRemainder will not be needed unless the pricing structure changes.
Post if you have any problem.