To start with, we are going to look at simple programs which use a single expression to produce a result, and then print it out. An expression is a piece of code which calculates a value, made up out of constants, variables, operators, functions and brackets. For example, the formula for one of the roots of a quadratic equation might look like this in Java:
(-b + Math.sqrt(b*b - 4*a*c)) / (2*a)
You have to be more precise than with printed or handwritten formulas, though. For example, in the code above:
2a because all multiplications have to be
explicit using the * symbol, so you have to write
2*ab2 because plain text is all on one
line, so you have to write something like b*b
and instead
you have to write (x + y) / (u + v) all on one line, with
bracketssqrt function lives, in the Math classIf you don't like the idea of having to put Math. in front of
all the mathematical functions every time you call them, you can put this line at the top of your program:
import static java.lang.Math.*;
Then you can use all the functions from the Math class
directly.