Arithmetic Expressions

Things To Do First

Things To Do Later

Arithmetic Operations

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).

Exercises

  1. Modulus is often used to find out if a number is even or odd. How would this work?
  2. Rewrite each of the following expressions in proper Java syntax:
    1. 3 x 4
    2. (x + 2)(x - 3)
    3. 10 ÷ 2
    4. 2x - 4

[solutions]

Integer Division

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:

  1. If either operand is a floating-point value, the result is a floating-point value.
  2. If both operands are a floating-point value, the result is a floating-point value.
  3. If both operands are an integer value, the result is an integer value.

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.

Exercise

Do programming question 1.7 (at the end of chapter 1) in your textbook.

[solutions]

Order of Precedence

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.

Note
Modulus is a division operation so therefore it has the same order of precedence as the multiplication and division operations. You can think of the "DM" in "BEDMAS" as standing for "division/multiplication/modulus".

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.

Read!
You can see a complete operator precedence chart in Appendix C of your textbook.

Exercises

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:

  1. Modulus (%) has the same level of precedence as what other operators?
  2. Which has higher precedence: the logical AND/OR operators or the mathematical operators for addition and subtraction?
  3. In the expression below, identify the order in which the operations would be done, from a (highest precedence) to c (lowest precedence):
    a * b > (c + d)
    1. _____
    2. _____
    3. _____

[solutions]

Exercises

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).

  1. In the table below there are 5 sets of algebraic expressions and a corresponding incorrect Java expression. Find the errors in the Java expressions and rewrite them correctly.
     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)
  2. Evaluate each of the following integer expressions:
    1. 20 - 5 / 2 + 3
    2. 20 - 5 / (2 + 3)
    3. 10 * (1 + 7 * 3)
    4. 15 % 3
    5. 10 + 5 % 2
    6. 10 * 5 % 2
  3. Evaluate each of the following expressions:
    1. 6.0 / 4.0
    2. 20.0 - 5.0 / 2.0 + 3.0
    3. 20.0 - 5.0 / (2.0 + 3.0)
    4. (20.0 - 5.0) / (2.0 + 3.0)
  4. Evaluate each of the following expressions:
    1. 10.0 + 15 / 2 + 4.3
    2. 10.0 + 15.0 / 2 + 4.3
    3. 3 * 6.0 / 4 + 6
    4. 20.0 - 5 / 2 + 3.0
    5. 3.0 * 4 % 6 + 6

[solutions]