Table 2.3 in Chapter 2.9 of your textbook shows the five most basic arithmetic operations and the symbols used in the Java language. Make note of the use of the * for multiplication and the / (forward slash, not the back slash) for division. Also, you might not be familiar with the modulus (%) operator. This operator performs a division operation but yields the remainder instead of the quotient. For example:
45 % 6 = 3 (6 goes into 45 seven times; 7 * 6 is 42. This leaves a remainder of 3)
In a modulus expression, the left-hand operand is called the dividend and the right-hand operand is called the divisor. To find the result of the expression, you determine what's left over (the remainder) after dividing the divisor into the dividend. More examples:
17 % 4 = 1 (4 goes into 17 4 times, leaving a remainder of 1)
45 % 5 = 0 (5 goes into 45 exactly 9 times, leaving no remainder)
9 % 5 = 4 (5 goes into 9 once, leaving a remainder of 4)
5 % 9 = 5 (9 goes into 5 zero times, leaving a remainder of 5)
Modulus works with negatives. If the dividend (the # to the left of the % symbol) is negative, the result is negative. Otherwise, the result is positive:
-17 % 4 = -1
-9 % -5 = -4
11 % -3 = 2
When writing arithmetic expressions in Java, you have to recall not only the special symbols used, but also that you can't use shorthand notation like you do on paper. For example, the expression xy + 2 would have to be written as x * y + 2. Also, exponents in Java are not written with the ^ or ** symbols as they are in other programming languages. We'll learn about this in a later session (see Chapter 5.10 if you're too impatient to wait).
The simplest arithmetic expressions in Java are made up of two operands and one operator. As a programmer, it is important that you not only know the data type of the operands, but also the data type of the expression's result. The rules for this are:
These rules don't need to be memorized, as they will seem common sense once you get the hang of programming arithmetic expressions.
A tricky point of concern is when working integer values. For example, the expression 5 / 2 will result in an integer value, given rule #3. However, we can calculate 5 / 2 in our heads as 2.5, which is a floating point value. Because 5 and 2 are integers, the result must be an integer, so the decimal portion of the result will be ignored: 5 / 2 will result in the value 2. You will need to consider that calculating with integers is very likely to require a float or a double for the result.
Do programming question 1.7 (at the end of chapter 1) in your textbook.
In high school math you probably learned the acronym "BEDMAS". This acronym stands for "Brackets, Exponents, Division/Multiplication, Addition/Subtraction" and defines the order in which you evaluate operations in a complex arithmetic expression. For example, in the expression 6 + 9 / 3 the rules of "BEDMAS" state that you must first do the division before the addition, even though the addition comes first in the expression. With this particular example you can see how important it is to follow the rules, as the expression evaluates incorrectly if you perform the operations in the wrong order! If you evaluate the addition first (6 + 9), the expression evaluates to 5. However, if you evaluate the expression correctly by doing the division first, the result is 9.
Java follows the same rules of precedence, but there are some additional operations in the traditional list of operators that you're used to. We'll discuss some of these as we move along through the course.
Examine the order of precedence chart in Appendix C of your textbook. If you don't yet have access to the text, you can use this Java Operator Precedence Table instead.
Answer the following questions:
a * b > (c + d)
For some of the questions below, you can check your answers by inserting the expressions into a println() statement. For example:
System.out.println(10 - 5 / 2 + 3);
...would give you the result of question 2.a).
Algebraic Expression | Incorrect Java Expression | |
---|---|---|
1. | (2)(3) + (4)(5) | (2)(3) + (4)(5) |
2. | 6 + 18 2 | 6 + 18 / 2 |
3. | 4.5 12.2 - 3.1 | 4.5 / 12.2 - 3.1 |
4. | 4.6(3.0 + 14.9) | 4.6(3.0 + 14.9) |
5. | (12.1 + 18.9)(15.3 - 3.8) | (12.1 + 18.9)(15.3 - 3.8) |