# Arithemtic Operations

• Introduction

• Although we have used some arithmetic operations, we have not introduced them formally (because the arithmetic operations follow similar rules as Algebra, the book did not pay much attention to them).

• We focus mainly on understanding:

• How to use objects/classes
• How to define/write objects/classes

• We will now learn about the arithmetic operations in depth because we will need to use them to write methods...

• Arithmetic Operators

• These are the Arithmetic Operators in Java:

Symbol Operation
- Subtract
* Multiply
/ Divide
% Remainder (Modulo)

• The order of evaluation (precedence) is:

• + and - have same precedence.
• * , / , and % have same precedence.
• * , / , and % have higher precedence than + and -
• You can use brackets () to override.

• Operations of equal precedence are evaluated from left to right

• Examples:

 ``` Expression Evaluates to --------------- ------------------ 2 + 3 * 4 = 2 + (3 * 4) * before + = 2 + 12 = 14 ```

 ``` Expression Evaluates to --------------- ------------------ 2 - 3 - 4 * 5 = 2 - 3 - (4 * 5) * before + = (2 - 3) - 20 left to right = -1 - 20 = -21 ```

 ``` Expression Evaluates to --------------- ------------------ 2 - (3 - 4) * 5 = 2 - (-1) * 5 () overrides = 2 - (-1 * 5) * before + = 2 - -5 = 7 ```

• Automatic conversions in arithmetic expression

• As you know (but it may be hard to accept for beginning programmers), integers and floating point numbers in a computer are very different.

• Java allows you to mix integers and floating point numbers in an arithmetic expression.

• Because integers and floating point numbers are different, the computer must first convert the integer to a floating point number before they can be operated upon.

• Example:

 ``` Expression Evaluates to --------------- ------------------ 2 + 2.0 = 2.0 + 2.0 (convert int to double) = 4.0 ```

• Attention: the Integer Division Operation

• The arithmetic operations in Java will behave exactly the same as what you have learned in Algebra...

except in the following case...

• When the divide operation (/) operates on 2 integer operands, the result is always an integer quotient.

The remainder of the division is discarded !!!

• Examples:

 ``` Expression Evaluates to --------------- ------------------ 7 / 4 1 (7 / 4 = 1 with remainder 3 the remainder is discarded) ```

• However:

 ``` Expression Evaluates to --------------- ------------------ 7.0 / 4.0 1.75 (Floating point division works like a calculator !!!) ```

• Question:

What is the result of 7.0 / 4 ?

 ``` Expression Evaluates to --------------- ------------------ 7.0 / 4 = 7.0 / 4.0 (convert integer to floating point !) = 1.75 ```

• The Remainder Operation

• The one operation that you may not have seen before is the remainder operator (%)

• The remainder operator (%) computes the remainder of an integer division (%)

In other words, both operands in the remainder operator (%) operation must be integers.

• Examples:

 ``` Expression Evaluates to --------------- ------------------ 7 % 4 3 (7 / 4 = 1 with remainder 3 The answer is the remainder) ```

• Self Check

• What is the value of 1729 / 100?

17

• What is the value of 1729 % 100?

29

• Given 3 integer numbers s1, s2 and s3.

• Does the following statement computes the average of s1, s2 and s3 ?

```	double average = s1 + s2 + s3 / 3;
```

No !!! Wrong precedence...

The expression computes:

```        double average = s1 + s2 + (s3 / 3);
```

• Does the following statement computes the average of s1, s2 and s3 ?

```	double average = (s1 + s2 + s3) / 3;
```

No !!! Because "/" in the expression is an Integer division !

Case in point: (1 + 1 + 3) / 3 = 5 / 3 = 1

```        double average = (s1 + s2 + s3) / 3.0 ;