# help

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-05-2011, 11:47 PM
beta4
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.
• 02-05-2011, 11:53 PM
Fubarable
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.
• 02-05-2011, 11:54 PM
pbrockway2
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.
• 02-06-2011, 12:32 AM
beta4
i labeled the variable "zipCode" as an int variable, so how would i go about doing this?
• 02-06-2011, 12:37 AM
sunde887
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.
• 02-06-2011, 12:37 AM
Fubarable
Quote:

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.
• 02-06-2011, 12:52 AM
beta4
the zipcode is supposed to be an input. also, im not sure how to change it.
• 02-06-2011, 12:55 AM
sunde887
in the class you designed just change the instance variable from
Code:

```int zipCode; to String zipCode;```
Input comes in as strings.
• 02-06-2011, 01:01 AM
beta4
thanks for that. now that i've declared "zipCode" as a string i got this error:

zipCode = keyboard.nextString;

it said "cannot find symbol"

whats the problem there?
• 02-06-2011, 01:03 AM
sunde887
Im guessing you are using scanner to get your input, check out the scanner api.

Scanner (Java Platform SE 6)
• 02-06-2011, 01:09 AM
beta4
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);"
• 02-06-2011, 01:12 AM
sunde887
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.
• 02-06-2011, 01:18 AM
beta4
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;

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
• 02-06-2011, 01:25 AM
sunde887
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();
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
Code:

```H e l l o```
You can use that method or charAt(int)

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.
• 02-06-2011, 01:31 AM
beta4
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)
• 02-06-2011, 01:34 AM
sunde887
I do know what you mean.

What number divides into all even numbers? What does % do?
• 02-06-2011, 01:38 AM
beta4
2 and i'm not sure what % does. i thought it had something to do with formatting decimals
• 02-06-2011, 01:41 AM
sunde887
it does if you use String.format or printf.

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

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

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.
• 02-06-2011, 01:50 AM
beta4
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?
• 02-06-2011, 01:53 AM
sunde887
You have a simple mistake, you used =, does that sound weird to you? Think how to compare numbers.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last