• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding runway length using math.pow  RSS feed

 
John Joe
Ranch Hand
Posts: 431
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your opinion and suggestions. Thank you so much.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!