Doing mathematical calculations in Java
Simple arithmetic in Java is fairly simple. There are five basic math
operators. The first four of which you probably learned in elementary
school. The fifth you nat gave learned in a math course in middle school.
The basic math operators are:
-
+ Addition
-
- Subtraction
-
* Multiplication
-
/ Division
-
% Mod Division
|
Note: The division operator (/) is used for both decimal and integer division.
The mod division operator gives the remainder of an integer division.
Some simple examples:
double x = 4.0; // Create variable x and set to 2.0
double y = 2.0; // Create variable y and set to 3.0
double z; // Create variable z
z = x + y;
System.out.println("z = " + z); // This will print "z = 6.0"
z = x - y;
System.out.println("z = " + z); // This will print "z = 2.0"
z = x * y;
System.out.println("z = " + z); // This will print "z = 8.0"
z = x / y;
System.out.println("z = " + z); // This will print "z = 2.0"
int a = 10; // Create integer variable a and set to 10
int b = 7; // Create integer variable b and set to 7
int c; // Create integer variable c
c = a / b; // This will do integer division
System.out.println("c = " + c); // This will print "c = 1"
c = a % b; // Do mod division to get remainder after a / b
System.out.println("c = " + c); // This will print "c = 3"
A not so simple example:
Suppose after defining x and y above you executed the following
command:
z = x + y * 3.0 / 4.0 - 1.5
System.out.println("z = " + z);
Would you expect to see the output as z = 3.0? The actual output would
be z = 4.0. The problem here is a thing called precedence. Java does
not add 4.0 and 2.0 then multiply by 3.0, divide that by 4.0, and finally subtract
1.5 to get the result. Java does all multiplication and division first moving from
left to right. Then it does the addition and subtraction. Thus (4.0 + 2.0 * 3.0 / 4.0 - 1.5)
becomes (4.0 + 1.5 - 1.5) after doing the multiplication and division and then this
gives the final answer of 4.0.
If you want the order to be different then you must use parentheses. Java always
completes all calculations inside parentheses first. Thus the code...
z = ((x + y) * 3.0 / 4.0) - 1.5
System.out.println("z = " + z);
...will give the desired output of z = 3.0.
Java also has a number of built in higher math functions which can be found
in the Math library. The link to the
Java documentation will let you see a description of these functions. Click the
link above then scroll down in the list on the bottom left and click on Math.
You will see a listing of many trigonometry, exponential, logrithemic, and other
advanced math functions. All of these functions are accessed by preceeding the name
of the function with Math. For example:
double angDeg = 45.0; // Create variable angDeg and set to 45 degrees
double angRad; // Create variable angRad for angles in radians
double result; // Create variable result
angRad = Math.toDegrees(angDeg); // Convert 45.0 degrees to radians
System.out.println("45.0 degrees in radians = " + angRad );
result = Math.sin(angRad);
System.out.println("sin(" + angDeg + ") = " + result ); // This will print the sine of the angle
result = Math.cos(angRad);
System.out.println("cos(" + angDeg + ") = " + result ); // This will print the cosine of the angle
result = Math.tan(angRad);
System.out.println("tan(" + angDeg + ") = " + result ); // This will print the tangent of the angle
result = Math.sqrt(angDeg);
System.out.println("sqrt(" + angDeg+ ") = " + result ); // This will print the square root of 45.0