CP- Mathematical Operations and Expressions Assignment
Mathematical Operations and Expressions Lesson
A mathematical expression is a combination of operators and operands to perform calculations.
These are the basic Mathematical Operators for Java and Processing:
We are familiar with addition, subtraction, and multiplication. You get the same result as you would in your math class when using these operations. They are used to modify variables.
int r = 45;
r = r + 10; r = r * 10; r = r - 10;
There are two types of division in programming and each will produce different results.
Division of Integers
When you divide two integers the result will be an integer. You truncate, or cut off, any decimal answer. The result can be stored in a variable declared as an int or as a double. Since a double reserves more space than an int, even the largest or smallest integer can be stored in a double without an error.
Examples:
- 7/2 = 3
- 10/4 = 2
- 24/5 = 4
Division of Doubles
When you divide with a double, the result will be a double. The result must be stored in a variable declared as a double. You can never store a double in an int variable - not enough space!
Modulus Operator
The modulus operator is used to compute the remainder of division. In the example 7 / 4, when we divide 7 by 4, the number 4 will go into 7 one (1) time evenly. When you substract 7 - 4 the remainder is 3. Therefore 7 % 4 = 3.
Examples:
- 7 % 2 = 1
- 14 % 4 = 2
- 24 % 5 = 4
When creating and evaluating mathematical expressions you must consider the order that each operation will execute.
Order of Operations Activity
Complete the Order of Operations learning object below.
Printing with Strings
In Processing and Java anything in double quotes is printed directly to the screen. Anything in double quotes is called a String literal. A String stores words and phrases. You can add Strings together using the + symbol. This process is called concatenation.
- In example 1 the String "Georgia" and the String "Virtual" are added together. There is no space, so it prints Georgia Virtual altogether.
- In example 2 a blank " "space was added between the words so it prints Georgia Virtual with a space between the words.
- In example 3, if you start off with a String and add a number to the String, the number will be created as a String. This is called arithmetic promotion.
"Total" + 2 will promote 2 to a String and add the two together giving you
"Total 2". This is then added to 4 and 4 will get promoted. In the end you get "Total 24". Notice to create a space between the words a space was added before and after the word " Total " .
- In example 4, when you start with a number, it remains a number until you reach a String, then everything after it is promoted to a String so it prints "6 Total 24".
- Order of operation performs anything in parenthesis first. So it adds 2 + 4 then reads the String "Total" so it prints Total 6.
Order of Operation in Programming Video
Watch the Order of Operation in Programming video below.
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION