# Thread: help

1. Member
Join Date
Feb 2011
Posts
17
Rep Power
0

## 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 10:53 PM.

2. 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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. Member
Join Date
Feb 2011
Posts
17
Rep Power
0
i labeled the variable "zipCode" as an int variable, so how would i go about doing this?

5. 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. Originally Posted by beta4
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. Member
Join Date
Feb 2011
Posts
17
Rep Power
0
the zipcode is supposed to be an input. also, im not sure how to change it.

8. in the class you designed just change the instance variable from
Java Code:
```int zipCode;

to

String zipCode;```
Input comes in as strings.

9. Member
Join Date
Feb 2011
Posts
17
Rep Power
0
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. Im guessing you are using scanner to get your input, check out the scanner api.

Scanner (Java Platform SE 6)

11. Member
Join Date
Feb 2011
Posts
17
Rep Power
0
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. 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. Member
Join Date
Feb 2011
Posts
17
Rep Power
0
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. 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. Member
Join Date
Feb 2011
Posts
17
Rep Power
0
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. I do know what you mean.

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

17. Member
Join Date
Feb 2011
Posts
17
Rep Power
0
2 and i'm not sure what % does. i thought it had something to do with formatting decimals

18. 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 12:46 AM.

19. Member
Join Date
Feb 2011
Posts
17
Rep Power
0
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. You have a simple mistake, you used =, does that sound weird to you? Think how to compare numbers.

Page 1 of 2 12 Last

#### Posting Permissions

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