# math problem.

Daniel Rich
Greenhorn
Posts: 16
Hey peeps. I have a very noob question, but first let me explain what I'm actually doing. I decided to take a small break from my book "Head first into Java" (An amazing book that says came from here! Javaranch). Anyways. i've been fiddling with object classes and so on. My project started very simple and slowly grew as one idea evolved into a better one. it all started by creating a 'person' object then creating an instance of the person class. The morning was spent thinking creating a blue print of a person from, age, sex, height, weight etc. I thought, hey what would be really cool is if there was a method that would give me a body type (build). small, stocky etc based on what infomation was given for height and weight. So made a new method that would work out their IBM (Index body Mass) and based on their height and weight.

So there are two questions. The first is although the code above gives me the correct answer surely there is an easier way of doing it rather than having three variables? And the second question (Sorry, I know I said 'question' and not questions at the start, but while i've got your attention). I'd really like only three decimal places (three numbers after the decimal point) rather than 19.527777777777775 I'm currently getting. (would rather just have 19.527) or even 19.530 (rounded up).

Daniel.

Jeff Verdegan
Bartender
Posts: 6109
6
Daniel Rich wrote: surely there is an easier way of doing it rather than having three variables?

Sure, if you don't need the intermediate results, you can just write it all in a single line, just like you would if doing it with pencil and paper, with a couple of caveats:

1. Just like when doing arithmetic by hand, you have to pay attention to operator precedence, and use parentheses if you want to group operations differently that would be done by the normal precedence rules.

2. Probably not an issue here, but for more complex operations, even when it can be done in one line, that doesn't mean you should do it that way. Sometimes it's easier to follow what's going on if it's broken up into smaller steps.

And the second question (Sorry, I know I said 'question' and not questions at the start, but while i've got your attention). I'd really like only three decimal places (three numbers after the decimal point) rather than 19.527777777777775 I'm currently getting. (would rather just have 19.527) or even 19.530 (rounded up).

You'll want to use DecimalFormat, or String.format(), or System.out.printf(). Here are some links to get you started:

java number formatting
java.text.DecimalFormat
System.out.printf()
Format String Syntax

Daniel Rich
Greenhorn
Posts: 16
Jeff Verdegan wrote:
Daniel Rich wrote: surely there is an easier way of doing it rather than having three variables?

1. Just like when doing arithmetic by hand, you have to pay attention to operator precedence, and use parentheses if you want to group operations differently that would be done by the normal precedence rules.

I couldn't figure out how do it in one line. I tried, but failed. maybe you should show me an example i tried looking it up by nothing :S

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
• 1
This is a basic algebra problem. you can always start at the end, and use the substitution principle...

ibm = three.

per the substitution principle, I can replace anything with something else to which it is equal. If you look at your line 7, you see that "three" is equal to "two * 703". so, I will make that change, and use parentheses so I don't screw up anything like operator precedence.

ibm = ( two * 703);

Ok...now...what can I replace "two" with? Look at your line 6, and make that substitution. use another set of parens. See if you can fill in this:

ibm = ( (<put something here>) * 703);

Then see if you can make another substitution.

Daniel Rich
Greenhorn
Posts: 16
fred rosenberger wrote:This is a basic algebra problem. you can always start at the end, and use the substitution principle...

ibm = three.

per the substitution principle, I can replace anything with something else to which it is equal. If you look at your line 7, you see that "three" is equal to "two * 703". so, I will make that change, and use parentheses so I don't screw up anything like operator precedence.

ibm = ( two * 703);

Ok...now...what can I replace "two" with? Look at your line 6, and make that substitution. use another set of parens. See if you can fill in this:

ibm = ( (<put something here>) * 703);

Then see if you can make another substitution.

Hey thanks for the help I narrowed it down to

This works really well. However, I don't understand the syntax of the brackets. I can't seen to really find anything that explains this very well.

Jeff Verdegan
Bartender
Posts: 6109
6
Daniel Rich wrote:
This works really well. However, I don't understand the syntax of the brackets. I can't seen to really find anything that explains this very well.

You mean the parentheses? It's the same as it is with algebraic expressions written on paper.

If you get a pencil and paper and write 3 + 4 X 5 the answer is 23, because 3 + 4 X 5 is the same as 3 + (4 X 5), which is 3 + 20. If you wanted it to be 7 X 5 you'd have to write it as (3 + 4) X 5. The parentheses supersede normal operator precedence rules and force the enclosed operation to bind more tightly than neighboring ones.

http://en.wikipedia.org/wiki/Order_of_operations

Daniel Rich
Greenhorn
Posts: 16
Thank you jeff, I'll break open an old math book and give myself a little refresser!

Stuie Clarky
Ranch Hand
Posts: 98
• 1
In your original example, having the three steps essentially side-steps the precedence issue as it separates out each step of the calculation. Doing it like that, with more explicitly named variables, might make it clearer to see what is going on if you come back to it in the future. The clarity point was mentioned above, but it really does make things easier to come back to or if you are working with other developers on a project.

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Daniel Rich wrote:

Hey thanks for the help I narrowed it down to

This works really well.

That's great, but you didn't finish it. I suggested you then try and make another substitution. there is something else in that line you can replace with something else...

Daniel Rich
Greenhorn
Posts: 16
fred rosenberger wrote:
Daniel Rich wrote:

Hey thanks for the help I narrowed it down to

This works really well.

That's great, but you didn't finish it. I suggested you then try and make another substitution. there is something else in that line you can replace with something else...

Woot!!! I figured it out! only took me most of the day, but i got it! thank you!

Also, thank you Jeff I figured out also how to DecimalFormat thanks so much for all the replies!