Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: help

  1. #1
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default help

    im currently doing an assignment for a programming class and im not sure how to do this part.

    "If the first digit of the zip code is a "4" then there is an additional surcharge of 5% on the shipping cost. If the first digit of the zip code is a "6" then there is an additional shipping cost surcharge of 9% based on the weight of the package. For all other zip codes there is an additional shipping cost surcharge of 14%. Additionally, if the zip code is even, then there is an additional charge of 2% of the shipping cost."

    basically, i'm a delivery company. i ask for a zipcode, and depending on the the first number of that zip code, i give a "surcharge", or "tax".

    i have no idea what the code is on how to find whether the first digit is even or odd. thats what what i mainly ned to know

    help would be appreciated.
    Last edited by beta4; 02-05-2011 at 11:53 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Looks like you'll have an series of if blocks in the code where you calculate the shipping cost. You'll need to get the first character from the zip code String for this, and this can be obtained via String's charAt(int) method, where you'll pass in a 0 to the method to get the first character.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Grab the first digit (as a char ie an integer value) of the zip code with one of the String methods.

    Then use that value in a switch statement and/or if statement to implement all the various options.

  4. #4
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    i labeled the variable "zipCode" as an int variable, so how would i go about doing this?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can convert the int to a string, then use charAt to get the item you want, then go with the way others have suggested.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by beta4 View Post
    i labeled the variable "zipCode" as an int variable, so how would i go about doing this?
    Change it to a String as zip shouldn't be an int as it doesn't truly hold numeric information -- you're never going to add 3 to a zip code or multiply zip code by zip code.

  7. #7
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    the zipcode is supposed to be an input. also, im not sure how to change it.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    in the class you designed just change the instance variable from
    Java Code:
    int zipCode;
    
    to
    
    String zipCode;
    Input comes in as strings.

  9. #9
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    thanks for that. now that i've declared "zipCode" as a string i got this error:

    System.out.print("Enter your zip code: ");
    zipCode = keyboard.nextString;

    it said "cannot find symbol"

    whats the problem there?

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im guessing you are using scanner to get your input, check out the scanner api.

    Scanner (Java Platform SE 6)

  11. #11
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    um, my assignments due by midnight, so... could i get a shortened explanation?

    i dont know if this will help, but this is what i typed:

    "import java.util.Scanner;

    Scanner keyboard = new Scanner(System.in);"

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Post all your code if it's not too long please. Ill help you out for another hour or so though. Just search for all the nextXxx() methods in the api, see if nextString() exists.

  13. #13
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    package assignment_3_1;
    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {

    double length;
    double width;
    double height;
    double weight;
    long surCharge;
    double shippingCost;
    String zipCode;

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the length of the package: ");
    length = keyboard.nextDouble();

    System.out.print("Enter the width of the package: ");
    width = keyboard.nextDouble();

    System.out.print("Enter the height of the package: ");
    height = keyboard.nextDouble();

    weight = length * width * height;

    if (weight > 5 && weight <= 15)
    shippingCost = 14;
    else if (weight > 15 && weight <= 34)
    shippingCost = 17;
    else if (weight > 34 && weight <= 45)
    shippingCost = 21;
    else if (weight > 45 && weight <= 60)
    shippingCost = 33;
    else if (weight > 60)
    shippingCost = 105;
    else
    shippingCost = 12;

    System.out.print("Enter your zip code: ");
    zipCode = keyboard.next();
    }

    }

    i just fixed the underlined problem problem. now i need to find out how to grab the first number in the users input of zipCode

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    is it alright to pile it all in main, or do you need to make a package class?

    you can either use the String method substring();
    Java Code:
    String x = "Hello";
    for(int i = 0; i < x.length() - 1; i++){
      System.out.println(x.substring(i, i + 1));
    }
    this code will output
    Java Code:
    H
    e
    l
    l
    o
    You can use that method or charAt(int)

    Java Code:
    String x = "Hello";
    for(int i = 0; i < x.length(); i++){
      System.out.println(charAt(i));
    }
    produces the same output

    Choose which you prefer and remember, strings are 0 based.

    If you need to make a class like package I can help you out with that as well, just let me know.

  15. #15
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    here's a new line i just typed in:

    int index1 = zipCode.charAt(0);

    now i need to know how to determine whether the zipCode starts with an even or odd number so i can determine the surcharge (if you dont know what im talking about, read the first post)

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I do know what you mean.

    What number divides into all even numbers? What does % do?

  17. #17
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    2 and i'm not sure what % does. i thought it had something to do with formatting decimals

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    it does if you use String.format or printf.

    Instead of giving you a link, Ill supply a short explanation.

    When used mathematically it returns the remainder of two numbers after division

    Java Code:
    int x = 5 % 2;
    int y = 10 % 3;
    int z = 20 % 2;
    in this x would be 1, since 5 / 2 is 2 with a remainder of 1
    y is 1, z is 0

    Also, I dono if elegant is the word, but it may be a bit more elegant to create a class like

    Java Code:
    class Package{
      double length;
      double width;
      double height;
      double weight;
      long surCharge;
      double shippingCost;
      String zipCode;
    
      //constructors
      Package(doubly length, double width, double height, double weight, long surCharge, double shippingCost, String zipCode){
        //perform initialization
      }
    
      //methods for determine shipping costs, get and set instance variables
    Then in main you can prompt for measurements of a package and create a package item with the constructor and simply call the methods on the new item. You could easily add an extra parameter to specify how many items to ship, looping through getting the dimensions and creating objects as you go. Just a thought if you want to get a little practice on designing classes.
    Last edited by sunde887; 02-06-2011 at 01:46 AM.

  19. #19
    beta4 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default

    thanks man.

    heres what i put:

    int x = zipCode.charAt(0);

    if (x == 4)
    surCharge = (.05 * shippingCost);
    else if (x == 6)
    surCharge = (.09 * shippingCost);
    else if (x % 2 = 0)
    surCharge = (.02 * shippingCost);
    else
    surCharge = (.014 * shippingCost);

    how can i fix that part?

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You have a simple mistake, you used =, does that sound weird to you? Think how to compare numbers.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •