Overview of This Lesson

Programming and arithmetic use the same thinking skills, and arithmetic is an important part of programming. To be a good programmer, you need to be able to understand arithmetic (not just how to type mathmatical expressions into a calculator) because YOU are the one that's instruction the computer how your app should perform its necessary calculations. In this lesson you'll review "order of operations" and learn how to write efficient arithemtic expressions and statements in Java.

Pre-Requisites

Before doing this tutorial, it is expected that you've completed the Expressions & Statements tutorial.

Arithmetic Operators

In programming there are five most basic binary arithmetic operations: addistion, subtraction, multiplication, division, and modulus. The table below shows the symbols for each operation and an example. Make note of the use of the * for multiplication and the / (forward slash, not the back slash) for division.

Basic Java Binary Operators
Operation Symbol Example
Addition + int sum = a + b;
Subtraction - int difference = a - b;
Multiplication * int product = a * b;
Division / int quotient = a / b;
Modulus % int remainder = a % b;

If you're not familiar with the modulus (%) operator, it 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 tutorial

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

1. Modulus is often used to find out if a number is even or odd. How would this work?
Answer: Any number N % 2 will be equal to 0 if the number is even, and equal to 1 if the number is odd.

2. Rewrite each of the following expressions in proper Java syntax:
a. 3 x 4
Answer: 3 * 4
b. (x + 2)(x - 3)
Answer: (x + 2) * (x - 3)
c. 10 ÷ 2
Answer: 10 / 2
d. 2x - 4
Answer: 2 * x - 4

Integer Division

The simplest binary 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

Interestingly, the value of π (PI) can be approximately determined with a mathematical formula:

Pi = 4 times left parenthesis 1 minus one-third plus one-fifth minus one-seventh plus one-ninth minus one-eleventh ellipses right parenthesis
Formula: Approximate value of π (PI)

Write a program that calculates and displays the approximate value of π (PI) up to 1/11, and also displays the approximate value of π (PI) up to 1/13.

formula for approximate value of pi up to one-eleventh formula for approximate value of pi up to one-thirteenth
Formulas for Approximation of π (PI)
public class ApproximatePi {

    public static void main(String[] args) {
        
        // this will yield an incorrect answer, because everything is done with
        // integer division
        System.out.println(4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11));
        
        // the statement above works like this:
        //  1/3, 1/5, 1/7, etc.. those all evaluate to 0, due to integer division
        // therefore, you have 4 * (1 - 0 + 0 - 0 .. etc) = 4 * 1 = 4
        
        // this will be more accurate because by using 1.0, you're
        // forcing Java to do proper floating-point division
        System.out.println(4 * 1.0 - 1.0/3 + 1.0/5 - 1.0/7 + 1.0/9 
            - 1.0/11);
        System.out.println(4 * 1.0 - 1.0/3 + 1.0/5 - 1.0/7 + 1.0/9 
            - 1.0/11 + 1.0/13);
    }
}

Order of Precedence

In high school math you probably learned the acronym "BEDMAS", "PEDMAS", or something similar. I was taught "BEDMAS": This acronym stands for "Brackets, Exponents, Division/Multiplication, Addition/Subtraction" (for "PEDMAS", the P stands for Parentheses, so they mean the same thing). This acronym, regardless of which one you learned, 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.

Exercises

Using this Java Operator Precedence Table as a guide, 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. _____
  1. Modulus (%) has the same level of precedence as multiplication and division.
  2. Addition and subtraction have a higher level of precedence than the logical AND/OR operators.
  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. brackets: (c + d)
    2. multiplication: a * b
    3. greater than symbol: determine truth value of expression "result of a * b greater than result of (c + d)"

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.
    Incorrect Algebraic Expressions in Java
      Algebraic
    Expression
    Incorrect Java
    Expression
    1. (2)(3) + (4)(5) (2)(3) + (4)(5)
    2. fraction with the expression 6 plus 8 as numerator and the value 2 as denominator 6 + 18 / 2
    3. fraction with 4.5 as numerator and the expression 12.2 minus 3.1 as denominator 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

1. Rewrite the expressions:

Incorrect Algebraic Expressions with Solutions
  Algebraic
Expression
Incorrect Java
Expression
Correct Java
Expression
1. (2)(3) + (4)(5) (2)(3) + (4)(5) 2 * 3 + 4 * 5
2. fraction with the expression 6 plus 8 as numerator and the value 2 as denominator 6 + 18 / 2 (6 + 18) / 2
3. fraction with 4.5 as numerator and the expression 12.2 minus 3.1 as denominator 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) 4.6 * (3.0 + 14.9)
5. (12.1 + 18.9)(15.3 - 3.8) (12.1 + 18.9)(15.3 - 3.8) (12.1 + 18.9) * (15.3 - 3.8)

2. Evaluate each of the following integer expressions:
a. 20 - 5 / 2 + 3 = 20 - 2 + 3 = 21
b. 20 - 5 / (2 + 3) = 20 - 5 / 5 = 20 - 1 = 19
c. 10 * (1 + 7 * 3) = 10 * (1 + 21) = 10 * 22 = 220
d. 15 % 3 = 0
e. 10 + 5 % 2 = 10 + 1 = 11
f. 10 * 5 % 2 = 50 % 2 = 0

3. Evaluate each of the following expressions:
a. 6.0 / 4.0 = 1.5
b. 20.0 - 5.0 / 2.0 + 3.0 = 20.0 - 2.5 + 3.0 = 20.5
c. 20.0 - 5.0 / (2.0 + 3.0) = 20.0 - 5.0 / 5.0 = 20.0 - 1.0 = 19.0
d. (20.0 - 5.0) / (2.0 + 3.0) = (15.0) / (2.0 + 3.0) = 15.0 / 5.0 = 3.0

4. Evaluate each of the following expressions:
a. 10.0 + 15 / 2 + 4.3 = 10.0 + 7 + 4.3 = 17.0 + 4.3 = 21.3
b. 10.0 + 15.0 / 2 + 4.3 = 10.0 + 7.5 + 4.3 = 17.5 + 4.3 = 21.8
c. 3 * 6.0 / 4 + 6 = 18.0 / 4 + 6 = 4.5 + 6 = 10.5
d. 20.0 - 5 / 2 + 3.0 = 20.0 - 2 + 3.0 = 18.0 + 3.0 = 21.0
e. 3.0 * 4 % 6 + 6 = 12.0 % 6 + 6 = 0.0 + 6 = 6.0