programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Finding runway length using math.pow

John Joe
Ranch Hand
Posts: 431
3
Question: Write a program that prompts the user to enter v in meters/second(m/s) and the accelearation a in (m/s^2) and display the length by using formula
length = v^2/2a

Expected Output
Enter speed and acceleration : 60 3.5
The minimum runway length for this airplane is 514.286

This is my code

But I get this output

Enter speed and acceleration: 60 3.5
The minimum runway length for this airplane is 6300.0

Tim Cooke
Marshal
Posts: 4041
239
Double check whether the division is happening before or after the multiplication. Are assignment operators evaluated left to right, or right to left?

As a side note: It seems rather overkill to import the Math library here when a simple (speed * speed) would suffice.

John Joe
Ranch Hand
Posts: 431
3
Tim Cooke wrote:Double check whether the division is happening before or after the multiplication. Are assignment operators evaluated left to right, or right to left?
As a side note: It seems rather overkill to import the Math library here when a simple (speed * speed) would suffice.

Thanks ！ I have figured it out

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
"v" is a TERRIBLE name for a variable.  what does it represent?  what does it mean?

if the formula is v^2/2a, why aren't you coding it do to that?  you can use parentesis to force a different order of operations that what Java wants to default to...

Campbell Ritchie
Marshal
Posts: 56536
172
You do need brackets. But not where you think. Forget that you are trying to calculate x². x² ÷ 2a shou‍ld read x² ÷ (2a) which you can rewrite as x² ÷ (2 × a) or x² ÷ 2 ÷ a. But we still haven't finished. Avoid the square and don't use Math#pow. Write
x * x / (2 * a)
or
x * x / 2 / a
Don't use floats. Use doubles instead.

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Just my opinion, but I would find

(x*x) / (2*a)

easier to understand than

x * x / 2 / a

John Joe
Ranch Hand
Posts: 431
3
Thanks guys for your opinion and suggestions. Thank you so much.

Campbell Ritchie
Marshal
Posts: 56536
172
• 1
That's a pleasure. As Fred points out, some versions are easier to understand than others.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.