As usual here's the output:
% javac FahrToCelsius.java % java FahrToCelsius 0 -17.7778 20 -6.66667 40 4.44444 60 15.5556 80 26.6667 100 37.7778 120 48.8889 140 60 160 71.1111 180 82.2222 200 93.3333 220 104.444 240 115.556 260 126.667 280 137.778 300 148.889
This program is a little more involved than the previous examples. Mostly it's stuff you've seen before though so a line by line analysis isn't necessary. The line to be concerned with is
celsius = (5.0 / 9.0) * (fahr-32.0);
This is a virtual translation of the formula C = (5/9)(F - 32) with
the single change that a * was added because Java does not
implicitly multiply items in parentheses. The parentheses are used
just as they are in regular algebra, to adjust the precedence of
terms in a formula. In fact the precedence of operations that use
the basic arithmetic operators (+
, -
, *
, /
) is exactly the same as
you learned in high school algebra.
Remember, you can always use parentheses to change the order of evaluation. Everything inside the parentheses will be calculated before anything outside of the parentheses is calculated. If you're in doubt it never hurts to put in extra parentheses to clear up the order in which terms will be evaluated.