12 Replies - 614 Views - Last Post: 08 October 2012 - 06:18 PM Rate Topic: -----

#1 johnlandauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-October 12

Shipping program not computing

Posted 08 October 2012 - 05:25 PM

I am having a problem with an assignment and need some help....

I get everything to compile, but it doesn't add up at the end. We have to put in miles and pounds. The weight works, but it always says $0.00 for the shipping price.

I'm searching right now to see how to copy my code to post it here. This is my first class in programming and I don't know how to copy the entire code and paste it....It you can tell me, great!

Thanks!

John

Is This A Good Question/Topic? 0
  • +

Replies To: Shipping program not computing

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Shipping program not computing

Posted 08 October 2012 - 05:27 PM

Please post your code using code tags: :code:.
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Shipping program not computing

Posted 08 October 2012 - 05:28 PM

Just highlight your code, and paste it into here. When you paste it in here, highlight it again in the reply area and click on the code button.
Was This Post Helpful? 0
  • +
  • -

#4 johnlandauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-October 12

Re: Shipping program not computing

Posted 08 October 2012 - 05:35 PM

import java.util.Scanner;

public class jlandauer_Shipping
{
    public static void main ( String args[] )
    {
        System.out.println("Welcome to the You Package it We Savage It Shipping Company.");
        System.out.println();

        Scanner input = new Scanner(System.in);

        int miles;
        int pounds;
        double rateCharged = 0;
        int Segmiles = 0;
        double Charge = (Segmiles*rateCharged);

        miles = 1;

        do
        {
            System.out.print ("How far will you be shipping your package in miles? ");
            miles = input.nextInt();
        }
        while ( miles < 1 );

        System.out.println();

        pounds = 1;

        do
        {
            System.out.print ("How heavy is your package in pounds (1-70)? ");
            pounds = input.nextInt();
        }
        while ( pounds <= 0 || pounds > 70 );
        {
            if ( pounds >= 46 )
            {
                rateCharged = 8.89;
            }
            else if ( pounds >= 26 )
            {
                rateCharged = 7.63;
            }
            else if ( pounds >= 11 )
            {
                rateCharged = 5.81;
            }
            else if ( pounds >= 6 )
            {
                rateCharged = 4.42;
            }
            else
            {
                rateCharged = 2.67;
            }
        }
        if ( miles % 25 == 0 )
        {
            Segmiles = (miles/25);
            System.out.printf("\nThe shipping rate is %f per 25 miles.  \nYour total shipping cost for %d miles is $%.2f\n", rateCharged, miles, Charge);
        }
        else
            Segmiles = ((miles/25) + 1);
            System.out.printf("\nThe shipping rate is %f per 25 miles.  \nYour total shipping cost for %d miles is $%.2f\n", rateCharged, miles, Charge);
        {
        }

    }
}



Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Shipping program not computing

Posted 08 October 2012 - 05:36 PM

See this line double Charge = (Segmiles*rateCharged);. You want to handle this calculation at the end, after all the variables have values that aren't 0.
Was This Post Helpful? 0
  • +
  • -

#6 johnlandauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-October 12

Re: Shipping program not computing

Posted 08 October 2012 - 05:39 PM

This is what I get when I compile the program. Everything enters fine, but the shipping amount is always $0.00.


Welcome to the You Package it We Savage It Shipping Company.

How far will you be shipping your package in miles? 5

How heavy is your package in pounds (1-70)? 5

The shipping rate is 2.670000 per 25 miles.
Your total shipping cost for 5 miles is $0.00

View Postmacosxnerd101, on 08 October 2012 - 05:36 PM, said:

See this line double Charge = (Segmiles*rateCharged);. You want to handle this calculation at the end, after all the variables have values that aren't 0.



So just move it to after my last else statement? I'll see what it does. I guess I don't see why it needs to be moved.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Shipping program not computing

Posted 08 October 2012 - 05:41 PM

When you initialize Charge, Segmiles = 0. After Segmiles has a non-zero value, then you want to calculate the Charge.
Was This Post Helpful? 0
  • +
  • -

#8 johnlandauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-October 12

Re: Shipping program not computing

Posted 08 October 2012 - 05:49 PM

View Postmacosxnerd101, on 08 October 2012 - 05:41 PM, said:

When you initialize Charge, Segmiles = 0. After Segmiles has a non-zero value, then you want to calculate the Charge.



OK...here's what I did.

double Charge = 0; //instead of segmiles*rateCharged


I still get the same result after I do that. Is there something wrong in lines 62 or 66? That's where I think the problem is, I just don't know where.
Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Shipping program not computing

Posted 08 October 2012 - 05:53 PM

Your program shouldnt even compile. You have some rogue braces on lines 67 and 68. I presume these should be for the else statement, so place your code inside of these braces.

This post has been edited by nick2price: 08 October 2012 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#10 johnlandauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-October 12

Re: Shipping program not computing

Posted 08 October 2012 - 05:53 PM

OK...figured it out...just a little trial and error!

Also, why are there 4 zeroes in my output for the shipping rate? Should be 2.67, it's 2.670000
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Shipping program not computing

Posted 08 October 2012 - 05:55 PM

You can use a System.out.printf() statement to round to two decimal places:
System.out.printf("Your shipping cost is %0.2f\n", Charge);


Was This Post Helpful? 0
  • +
  • -

#12 johnlandauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-October 12

Re: Shipping program not computing

Posted 08 October 2012 - 06:11 PM

View Postmacosxnerd101, on 08 October 2012 - 05:55 PM, said:

You can use a System.out.printf() statement to round to two decimal places:
System.out.printf("Your shipping cost is %0.2f\n", Charge);




Thanks! I just missed the .2 in the first part of my statements. Cleared it right up!
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Shipping program not computing

Posted 08 October 2012 - 06:18 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1