Arithmetic Expressions and Assignments
Overview
In this lesson, students will write and interpret arithmetic expressions.
Learning Objectives
- 1.1.C.1 Write and interpret arithmetic expressions that use variables and constants.
- 1.1.C.2 Assign expressions to variables using assignment and compound assignment operators.
Skills
- S1.B Design algorithms for a program.
- S1.D Explain the impact design has on data storage.
- S2.A Write program code and implement algorithms.
- S2.C Analyze an algorithm and program code for correctness.
Student Outcomes
Students will be able to:
- identify when to use a constant
- write arithmetic expressions to solve problems
- interpret arithmetic expressions and determine the result
- use an assignment statement to update a variable's value
Duration: 1 class period
Resources
Background
Assignments
The assignment operator (=
) is used to initialize a variable and to update it to a new value.
The variable that is being assigned a value is on the left side of the assignment operator and the value that is being assigned is on the right.
Expressions
In analyzing data, we will need to write expressions to do computations. The following arithmetic operators are used in expressions:
+
for addition
-
for subtraction
*
for multiplication
/
for division
%
for modulus division
When using these operators, the resulting type depends on the data type of the operands.
- If both operands are of type
int
, the result is of type int
.
- If one or more of the operands are of type
double
, the result is of type double
.
This means that division functions in two different ways.
- If both operands are of type
int
, the result truncates (removes) any remainder. This is considered integer division. For example:
7 / 2
results in 3
, the decimal portion is not included.
4 / 2
results in 2
, there is no remainder in this case.
- If one or both operands are of type
double
, the result includes the remainder. For example:
7.0 / 2
results in 3.5
. Similarly, 7 / 2.0
and 7.0 / 2.0
will also result in 3.5
.
4.0 / 2
results in 2.0
. Similarly, 4 / 2.0
and 4.0 / 2.0
will also result in 2.0
.
Modulus divsion give the remainder when two numbers are divided. For example:
7 % 2
results in 1
, since 2
divides into 7
, 3
whole times with a remainder of 1
.
17 % 3
results in 2
, since 3
divides into 17
, 5
whole times with a remainder of 2
.
4 % 2
results is 0
, since 2
divides into 4
, 2
whole times with no remainder.
2 % 3
results in 2
, since 3
divides into 2
, 0
whole times and 2
is the remainder.
Order of Operations for Expressions
The order of operations works similarly how they work in mathematics. The order of operation is as follows:
- parenthesis (
( )
)
- mutliplication, division, and modulus working from left to right (
*
, /
, %
)
- addition and subtraction working from left to right (
+
and -
)
Consider the following examples:
Example 1:
3 + 5 * 7 % 4
- The first operation is multiplication
5 * 7
, which is 35
. Leaving us with 3 + 35 % 4
.
- The second operation is modulus division
35 % 4
, which is 3
. Leaving us with 3 + 3
.
- The last operation is addition
3 + 3
, which is 6
.
- So,
3 + 5 * 7 % 4
evaluates to 6
.
Example 2:
3 + 5 * (7 % 4)
- The parenthesis override the order of operations causing the first operation to be modulus division
7 % 4
, which is 3
. Leaving us with 3 + 5 * 3
.
- The next operation is multiplication
5 * 3
, which is 15
. Leavning us with 3 + 15
.
- The last operation is addition
3 + 15
, which is 18
.
- So,
3 + 5 * (7 % 4)
evaluates to 18
.
Example 3:
(3 + 5) * 7 % 4
- The parenthesis override the order of operations causing the first operation to be additiona,
3 + 5
, which is 8
. Leaving us with 8 * 7 % 4
.
- The next operation is multiplication
8 * 7
, which is 56
. Leaving us with 56 % 4
.
- The last operation is modulus
56 % 4
. Since 56
is divisible by 4
, there is no remainder and the result is 0
.
- So,
(3 + 5) * 7 % 4
evaluates to 0
.
Example 4:
(3 + 5) * (7 % 4)
- The parenthesis override the order of operations. Working left to right, the first operation that is evaluated is
3 + 5
, which is 8
. Leaving us with 8 * (7 % 4)
.
- The next operation in parenthesis is then evaluated
7 % 4
, which is 3
. Leaving us with 8 * 3
.
- The last operation is multiplication
8 * 3
, which is 24
.
- So,
(3 + 5) * (7 % 4)
evaluates to 24
.
Highlighters can be used to highlight each step in the evaluation process.
Assignment Operator
The assignment operator (=
) happens after the expression is evaluated.
Addition with String objects
The addition operator can be used to append, or join, two String
objects together or append a String
object with a primitive value. In order to append a String
with a primitive, the compiler will automatically create a String
literal out of the primitive value. This is a String Conversion.
It is important to remember that items are added together from left to right. This applies to expressions that involve String
literals as well.
Here are some examples:
String message1 = "The weight of the dog in pounds is " + 32.5; //message1 will have the value "The weight of the dog in pounds is 32.5"
String message2 = 'L' + " is my middle initial"; //message2 will have the value "L is my middle initial"
String message3 = "Her birthday is on the " + 24 +"th of the month"; //message3 will have the value "Her birthday is on the 24th of the month"
In all the cases above, we could have assigned the resulting String
literal to the variable. For example:
String message1 = "The weight of the dog in pounds is 32.5";
Where this is really powerful, is if the primitive values are being stored in a variable of their own. Consider the following:
double weight = 32.5;
String message1 = "The weight of the dog in pounds is " + weight;
The value of message1
will be "The weight of the dog in pounds is 32.5"
Order of Operations with String Literals and Primitives
String sumMessage = 3 + 5 + " is the value " + 8;
The sum of 3
and 5
is computed first, resulting in 8
.
Then this result (8
) plus "is the value "
is evaluated, resulting in "8 is the value"
.
Finally, "8 is the value "
plus 8
is evalued, resulting in "8 is the value 8"
.
Another Example:
String sumMessage2 = "What is the value of " + 3 + " plus " + 5 + "? " + 3 + 5
- The sum of
"What is the value of "
and 3
is evaluated first, resulting in "What is the value of 3"
.
- The sum of
"What is the value of 3"
and " plus "
is evaluted second, resulting in "What is the value of 3 plus "
.
- The sum of
"What is the value of 3 plus "
and 5
is evaluated third, resulting in "What is the value of 3 plus 5"
.
- The sum of
"What is the value of 3 plus 5"
and ?
is evaluated forth, resulting in "What is the value of 3 plus 5? "
.
- The sum of
"What is the value of 3 plus 5? "
and 3
is evaluated fifth, resulting in "What is the value of 3 plus 5? 3"
.
- The sum of
"What is the value of 3 plus 5? 3"
and 5
is evaluated sixth, resulting in "What is the value of 3 plus 5? 35"
.
You can use parenthesis to override the order of operation so to correct this output to be "What is the value of 3 plus 5? 8"
.
String sumMessage3 = "What is the value of " + 3 + " plus " + 5 + "? " + (3 + 5);
- The sum of
3
and 5
in parenthesis is evaluated first, resulting in 8
.
- The sum of
"What is the value of "
and 3
is evaluated second, resulting in "What is the value of 3"
.
- The sum of
"What is the value of 3"
and " plus "
is evaluted third, resulting in "What is the value of 3 plus "
.
- The sum of
"What is the value of 3 plus "
and 5
is evaluated forth, resulting in "What is the value of 3 plus 5"
.
- The sum of
"What is the value of 3 plus 5"
and ?
is evaluated fifth, resulting in "What is the value of 3 plus 5? "
.
- The sum of
"What is the value of 3 plus 5? "
and 8
is evaluated fifth, resulting in "What is the value of 3 plus 5? 8"
.
Constants
There are times when we don't want a value to change while the program is running. In these cases, a constant can be used. A constant is a value that does not change once it is assigned a value. We use the keyword final
in front of the data type when we create a constant and assign it the constant value. For example:
final double pi = 3.14;
Activity
Activity 1: Creating expressions
Create several sets of operation and number cards on index cards. Each set should include the following:
- each digit on it's own index card
0
- 9
.
- each operation on it's own index card
+
, -
, *
, /
, and %
.
- an open parenthesis
(
on one card and close parentheis )
on another.
Have students randomly create 10 expressions by pulling cards out of the set. It is adivisable to keep the number cards and operation cards separate. Students should write their expressions on a piece of paper.
Provide students with requirements for the length of the expressions in terms of the number of operations that are required. For example, maybe the first 2 have 1 operation, the next 2 have 2 operations, the next 3 have 3 operations, and the last 3 have parenthesis.
After students evaluate these expressions, they can use the Java Playground to check if their answers are correct.
Activity 2: Generate Expressions with AI
Have students ask their favorite AI to generate some practice expressions to evaluate. Here's a sample prompt you might use: "To use with people just starting out in learning Java arithmetic expressions, generate a set of 10 expressions in Java that use the arithmetic operators."