Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The cost to ship a package is a flat fee of 75 cents plus 25 cents per pound.
1. Declare a constant named CENTS_PER_POUND and initialize with 25.
2. Get the shipping weight from user input storing the weight into shipWeightPounds.
3. Using FLAT_FEE_CENTS and CENTS_PER_POUND constants, assign shipCostCents with the cost of shipping a package weighing shipWeightPounds.

What I have tried:

Java
import java.util.Scanner;

public class ShippingCalculator {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int shipWeightPounds;
      int shipCostCents = 0;
      final int FLAT_FEE_CENTS = 75;
      final double CENTS_PER_POUND = 25;
      
      shipWeightPounds = scnr.nextDouble();
      shipCostCents = FLAT_FEE_CENTS + (CENTS_PER_POUND * shipWeightPounds);

      System.out.println("Weight(lb): " + shipWeightPounds);
      System.out.println("Flat fee(cents): " + FLAT_FEE_CENTS);
      System.out.println("Cents per pound: " + CENTS_PER_POUND);
      System.out.println("Shipping cost(cents): " + shipCostCents);
   }
}
Posted
Updated 21-Feb-24 3:24am
v2
Comments
OriginalGriff 21-Feb-24 1:36am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

Replace
Quote:
final double CENTS_PER_POUND = 25;

shipWeightPounds = scnr.nextDouble();
with
final int CENTS_PER_POUND = 25;

shipWeightPounds = scnr.nextInt();
 
Share this answer
 
Comments
Caroldoria Harris 21-Feb-24 2:39am    
Thank you that worked
CPallini 21-Feb-24 2:45am    
You are welcome.
Patrice T 21-Feb-24 4:36am    
+5
CPallini 21-Feb-24 7:47am    
Thank you.
_Asif_ 21-Feb-24 4:39am    
+5 for identifying the issue without any hint in Question
Quote:
What am I missing? I have tried so many ways to solve this.

Not your problem, but:
All these
Java
int shipCostCents = 0;
final int FLAT_FEE_CENTS = 75;
final double CENTS_PER_POUND = 25;

are cents.
Why are you mixing data types ?
Be consistent !
 
Share this answer
 
Comments
CPallini 21-Feb-24 7:45am    
Indeed.
5.
Patrice T 21-Feb-24 11:48am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900