2

I am creating a calculator to calculate shipping costs. The code goes something like this:

class ShippingCalc { public static void main(String[] args) { int weight = 30; if (weight < 10) { System.out.println("Shipping costs $1."); } else if (weight < 20) { System.out.println("Shipping costs $2."); } else { System.out.println("Shipping costs $3."); } } } 

This is all great but I want to create a calculator that can calculate based on already set values. For example, something that says:

if (weight < 250) { // print("Shipping cost is $1); } else if (weight < 499) { // print("Shipping cost is $2); } else if (weight < 749) { // print...etc and it keeps going 

This will be based on user input that is why I don't want to already have any constraints like above. Is it possible to make such a calculator in Java that no matter how much the weight, it calculates the shipping costs appropriately and gives out the answer.

If yes, then how do I go about it?

6
  • 3
    Yes it's possible by using simple math and writing code. Why don't you give it a try and see what you come up with. It's always better to attempt to solve things and then show your efforts before posting your assignment here. Again, it's nothing more than the most basic algebra, something I'll bet you can figure out if you put pencil to paper. Commented Jan 26, 2012 at 2:30
  • Its not my assignment. I am trying to learn Java as a hobby, hence the tag hobby. As for trying, I have tried it but all I can think of is the limited way that will take endless amount of code. I don't need people to do it for me, just give me a nudge in the right direction. Commented Jan 26, 2012 at 2:33
  • Edit, well, maybe not so easy since there is no logic to your numbers, unless it always increases by $150. You'll have to show more numbers for that ...etc bit. Show the pattern fully please. And I have made no assumptions about this being homework as my recommendations are valid for homework or home work (coming from another hobbiest). Commented Jan 26, 2012 at 2:34
  • @HovercraftFullOfEels The weight increase is 150 and it increases the price by $1. That part of the code is an example, that is maybe why its not so clear. Commented Jan 26, 2012 at 2:36
  • You only have one increase by 150 -- the first two increases are by 250, so it is confusing. Again, this is extremely simple math. Make two columns of numbers and you can solve it. Or you can have someone solve it here, but really, don't do that for this. Exercise your brain; you'll be glad you did. Commented Jan 26, 2012 at 2:38

2 Answers 2

4

First, you need a formula or table for calculating the shipping costs. For example, "the shipping is one dollar per whole ten pounds of weight".

Then, you put the weight into that formula.

System.out.println("Shipping cost is $" + (int)(weight/10)); 

If you want the formula to be more complex, you can do something like this:

if (weight < threshold1) // price is first level // or, if you like, you can even do a calculation here else if (weight < threshold2) // price is second level 

Where the users can define the value of the threshold1 and threshold2 variables.

There can be an unbounded number of these levels:

// "thresholds" is a sorted array of the weights at which prices change int[] thresholds = new int[num_thresholds]; for (int checking = 0; checking < thresholds.length; checking++) { if (weight < thresholds[checking]) // price is prices[checking] } 

Welcome to the wonderful world of computer programming!

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Sorry, can't upvote yet. Thanks for the welcome and the answer.
@Borealid, In relation to the unbounded number of levels. I don't quite understand your method. Can you expound on this please. How can it be unbounded if you have to set [num_thresholds]. What if weight has no limit?
1

If the weight to costs follows a formula you should use it to calculate the cost (a little algebra never hurt anyone).

If the weight to cost assignment is arbitrary you can create a NavigableMap using weight as the key and cost as the value.

You could then use the NavigableMap<K, V>.lowerEntry(K) to find the highest weight that is lower than the given weight.

Example

public static Integer findCost(Integer weight, NavigableMap<Integer, Integer> costMap){ Map.Entry<Integer, Integer> cost; cost = costMap.lowerEntry(weight); if(cost == null){ cost = costMap.firstEntry(); } return cost.getValue(); } 

The benifit of using a map is if you use a TreeMap as the implementation for your NavigableMap then your look ups will on average be O(log n).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.