CSA · ACTIVITY 1.1.3

Expressions & Assignment Statements

Java can do math — but it follows its own strict rules about types, division, and order of operations. Today you'll learn to predict exactly what Java computes before you ever hit Run. Work through all six sections; each pill earns its checkmark only when the work in it is done.

1 · Comments in Code

Comments are notes for humans. The compiler ignores them completely — they never execute. Java has three types:

TypeLooks likeUsed for
Multiline/* ... */Block headers, instructions that span lines
Single-line// ...Explaining one line; can sit at the end of a statement
Javadoc/** ... */Java documentation — you'll meet it later this course
Mission 1: The compiler is about to read this program. Click every piece the compiler ignores. Careful — one line is only partly a comment.

      

Quick check

To earn the checkmark: find all 3 ignored pieces + answer the quick check.

2 · Arithmetic Expressions

An expression is made of values, variables, and operators, and it evaluates to a single value — and that value has a type. The operators:

SymbolNameExample
*Multiplication4 * 312
/Division12 / 43
%Modulo (remainder)7 % 31
+Addition4 + 37
-Subtraction4 - 31

The type rule: int with int gives int. If even ONE value is a double, the result is a double — even 5.0 * 5.

Mission 2: Type-check all 8 expressions. Watch for the sneaky ones — a single . changes everything.
To earn the checkmark: correctly type-check all 8 expressions.

3 · Division & Modulo Lab

Two facts that break people's programs: int division throws away the decimal part (18 / 5 is 3, not 3.6), and dividing an int by zero crashes the program with an ArithmeticException.

The Division Lab

Enter two values and an operator. The lab shows what your calculator says next to what Java says. A value with a decimal point (like 18.0) is a double; without one it's an int.

// results appear here
Mission 3a: Crash the lab on purpose. Make Java throw an ArithmeticException. (Then try 18.0 / 0 and see something even stranger.)

Modulo predictions

Mission 3b: a % b is the remainder after dividing a by b. Predict all four — type each answer, then check.
To earn the checkmark: crash the lab on purpose + all 4 modulo predictions correct.

4 · Compound Expressions

When several operators share one expression, Java uses operator precedence — PEMDAS with one addition: *, /, and % are all the same level, evaluated left to right. Then + and -, also left to right.

Mission 4a — The Stepper: Be the compiler. In the expression below, click the operator Java evaluates NEXT, step by step, until one value remains.

Parentheses

Parentheses override precedence, exactly like in math.

Mission 4b — Minimize It: Take the expression 5 + 12 * 18 - 2 / 2 % 2 (worth 220 as written) and add exactly one pair of parentheses to make it as small as possible. Can you hit 0? Type your version and test it — this is a real evaluator, it computes whatever you give it.
// your expression's value appears here
To earn the checkmark: complete the stepper + reach 0 with one pair of parentheses.

5 · Calculate Your Weight on Jupiter

Your mass is the same everywhere in the universe — but your weight depends on the planet's gravity. The conversion is one expression:

weightOnPlanet = weightOnEarth * planetGravity / earthGravity;

Earth's gravity is 9.81 m/s². Jupiter's is a monstrous 24.79.

Mission 5: Enter a weight on Earth (yours, or the sample 130). Predict the Jupiter weight with a calculator first, type your prediction, then let the simulator check you. After that, explore at least two more planets.
Weight on Earth (lbs): My Jupiter prediction:
To earn the checkmark: prediction within 1 lb + explore 2 more planets.

6 · Exit Ticket

Coming next class — GalaxyWeight: you'll write a program that prints your weight on three planets and the average of those weights. Keep today's type rule in mind: what type must that average be?

Eight questions. You get one retry per question. After that, the question locks — bring it to Mr. Babb instead of guessing.

Conclusion — in your own words (2+ sentences each)

Q1. Why do you think the order of operations still applies in Java?

Q2. Why is it important to know the type of a variable?

To earn the checkmark: resolve all 8 questions + both conclusion answers.

📕 Computer Science Notebook — copy these into your notes

  • Comments (/* */, //, /** */) are for humans; the compiler ignores them.
  • An expression evaluates to a single value, and that value has a type: int op int → int; anything with a double → double.
  • int division drops the decimal: 18 / 5 is 3. Dividing an int by 0 throws an ArithmeticException.
  • a % b is the remainder of a divided by b: 7 % 3 is 1.
  • Precedence: *, /, % first (left to right), then +, - (left to right). Parentheses override everything.

Mr. Babb · AP Computer Science A · Paul Public Charter School · This page does not save work — finish in one sitting.