1.4 Expressions and Assignment Statements
Arithmetic expressions allow your to perform mathematical operations within Java. Such expressions can be used for basic math and even more complex algorithms.
Arithmetic Operators
Operator | Description |
---|---|
+ | Addition operator (Ex. 1 + 1 = 2) |
- | Subtraction operator (Ex. 2 - 1 = 1) |
* | Multiplication operator (Ex. 2 * 2 = 4) |
/ | Division operator (Ex. 6 / 2 = 3) |
% | Modulus operator (Ex. 10 % 3 = 1) |
The addition operator allows you to add values together. Here is an example of the addition operator in Java:
The subtraction operator allows you to subtract values from one another. Here is an example of the subtraction operator in Java:
The multiplication operator allows you to multiply values. Here is an example of the multiplication operator in Java:
The division operator allows you to divide values. Here is an example of the division operator in Java:
There are multiple types of division that can be performed in Java. These forms include: integer division, double division, and mixed division.
Integer Division
If you divide two integers you will be returned an integer value.
So in this case, while 2 / 5 equals 2.5, in Java 2 / 5 = 2
. This always holds true, unless the type is specified as a double. Whenever you divide two integers, the return value is always truncated.
Double Division
Dividing doubles is different from dividing integers.
When you divide two doubles you will be returned a double. In this case,2.0 / 5.0 = 2.5
will be your result.
Mixed Division
Mixed division is when you divide a double by an integer or an integer by a double.
When you use mixed division in your program you will be returned a double. This is true, unless the type is specifically defined as an integer.
Consider the following piece of code:
No matter which data type you use, dividing by zero will cause your programs to throw an ArithmeticException. Exceptions are unwanted or unexpected events which occur during the execution of a program. In this case, dividing by zero causes the variable to store the value of undefined, which cannot be used in arithmetic expression. The program throws the ArithmeticException to warn programmers that there is an issue that needs to be resolved.
The modulus operator allows you to divide two numbers and get the remainder. Here is an example of the modulus operator in Java:
It is important to remember that the order of operations still applies. In instances where you have multiple operators with the same precedence the order will be left to right.
Operator | Precedence |
---|---|
( ) Parentheses |
1st |
* / Multiplication and Division |
2nd |
+ - Addition and Subtraction |
3rd |
-
Incorrect
Correct
No Answer was selected
Invalid Answer
What is the result of this expression?
In this program you will initialize 4 integers that represent each part of two fractions, namely the numerator and denominator of the first fraction and the numerator and denominator of the second fraction.
Your program should subtract the two fractions and print out the result.
For example, a sample program run might look like this:
Note: you do not need to reduce these fractions. Be sure to test your code for positive and negative integers by changing the values of your initialized variables.
Remember, if you have two fractions you subtract them with: