Skip Top Navigation Bar

Arithmetic Expressions and Assignments

Overview

In this lesson, students will write and interpret arithmetic expressions.

Learning Objectives

Skills

Student Outcomes

Students will be able to:

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:

When using these operators, the resulting type depends on the data type of the operands.

This means that division functions in two different ways.

Modulus divsion give the remainder when two numbers are divided. For example:

Order of Operations for Expressions

The order of operations works similarly how they work in mathematics. The order of operation is as follows:

Consider the following examples:

Example 1:

3 + 5 * 7 % 4

Example 2:

3 + 5 * (7 % 4)

Example 3:

(3 + 5) * 7 % 4

Example 4:

(3 + 5) * (7 % 4)

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

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

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:

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