Staging: a draft, not the live site
✕ Close

Week 13 · 13.5 · Fri, Oct 30 · We Do + Try Its

Before Java Together: Four Skills on One Bench

This section puts the week's four skills side by side, the way today's bench uses them: storing values in boxes, dividing with ÷ and rem, asking questions that are true or false, and tracing all of them in one table.

Boxes and the Arrow

A box holds one value at a time. The arrow ← stores the value on its right into the box on its left, and the old value in that box is gone. A line that names a box on the right only reads it, so that box keeps its value.

Example 1. Trace these lines.

1a ← 5
2b ← a
3a ← 9
Show the solution
line   a   b
1      5
2      5   5
3      9   5

Line 2 copies 5 into b. Line 3 replaces a with 9, but b still holds the 5 it was given. A copy does not change when the box it came from changes.

Example 2. A bag starts with 36 pieces of candy, and 5 are eaten. Trace the box.

1pieces ← 36
2pieces ← pieces - 5
Show the solution
line   pieces
1      36
2      31

The right side is worked out first, with the old value 36. Then the answer, 31, replaces it.

Try It on your own

Work each one in your notebook first, then check it.

Try It 1. After line 3, what does y hold?

1x ← 8
2y ← x
3x ← 2

Try It 2. A bag holds 40 pieces of candy. After line 2, what does bag hold?

1bag ← 40
2bag ← bag - 12

Whole Parts and Leftovers

Whole-number division gives two answers. The sign ÷ keeps the whole part, and rem keeps what is left over. The signs *, ÷ and rem are worked from left to right before + and -.

Example 3. 47 pieces of candy are shared fairly by 5 friends.

Show the solution
147 ÷ 5     each friend gets      9
247 rem 5   pieces left over      2

Each friend gets 9, because 5 * 9 is 45, and 47 - 45 leaves 2.

Example 4. Work out 2 + 13 rem 4 * 3.

Show the solution
12 + 13 rem 4 * 3
2= 2 + 1 * 3       13 rem 4 first, from the left
3= 2 + 3           then the *
4= 5               + comes last

Example 5. Work out 4 ÷ 13.

Show the solution
14 ÷ 13     the whole part of 4 ÷ 13 is      0

Dividing a smaller number by a larger one gives a whole part of 0. Writing a division upside down, as 4 ÷ 13 instead of 13 ÷ 4, changes 3 into 0.

Try It on your own

Work each one in your notebook first, then check it.

Try It 3. 38 pieces of candy are shared fairly by 6 friends. What are 38 ÷ 6 and 38 rem 6?

Try It 4. Work it out. What is 1 + 17 rem 5 * 2?

Try It 5. Work out 3 ÷ 12 and 12 ÷ 3. Explain in one sentence why the two answers are different.

Check your answer

3 ÷ 12 is 0 and 12 ÷ 3 is 4. Dividing a smaller number by a larger one gives a whole part of 0, so writing the division upside down changes the answer.

Questions with Two Answers

A comparison answers true or false. The signs are <, >, ≤, ≥, = and ≠. The operator and is true only when both sides are true, or is true when at least one side is true, and not flips the answer.

Example 6. Is 47 rem 5 = 2 true or false?

Show the solution
147 rem 5 = 2
2= 2 = 2          work the remainder first
3= true

Example 7. A house number h is even when h rem 2 = 0. Check 23.

Show the solution
123 rem 2 = 0     23 rem 2 is 1, so      false

Example 8. Work out 9 > 5 and 9 rem 2 = 0, and then 9 > 5 or 9 rem 2 = 0.

19 > 5 and 9 rem 2 = 0     true and false     false
29 > 5 or 9 rem 2 = 0      true or false      true
Try It on your own

Work each one in your notebook first, then check it.

Try It 6. Is 29 rem 2 = 0 true or false?

Try It 7. Work it out. Is 12 ≥ 10 and 12 rem 5 = 3 true or false?

Tracing Them Together

A trace table records every box after every line runs, and shown records what a show line puts on the screen. When a program cannot be run, a trace table is how it is checked by hand.

Example 9. 30 pieces of candy are shared by 7 trick-or-treaters. Is anything left in the bowl?

1candy ← 30
2kids ← 7
3each ← candy ÷ kids
4left ← candy rem kids
5show left > 0
Show the solution
line   candy   kids   each   left   shown
1      30
2      30      7
3      30      7      4
4      30      7      4      2
5      30      7      4      2      true

Line 3 stores 4, because 7 * 4 is 28. Line 4 stores 2, the 2 pieces left over. Line 5 shows true, because 2 is greater than 0.

Example 10. A student traced the lines below and wrote that c holds 0. Find the line where the trace goes wrong.

1a ← 13
2b ← 4
3c ← a ÷ b
Show the solution

The rows for lines 1 and 2 are right. On line 3, 13 ÷ 4 is 3, so c should hold 3. The student's 0 is 4 ÷ 13, the division written upside down. A trace table shows the exact line where a mistake first happens, so that line is the one to fix.

The bench catches three common mistakes, and each one changes the answer. The first is changing a copy when the box it came from changes. The second is rounding a division up instead of keeping only the whole part. The third is writing a division upside down.

Sources: Oracle, The Java Tutorials: Assignment, Arithmetic, and Unary Operators and Equality, Relational, and Conditional Operators; course repository, Bench Basics B10 job data (2026).

Try It on your own

Work each one in your notebook first, then check it.

Try It 8. After line 4, what do the boxes hold?

1candy ← 26
2kids ← 4
3each ← candy ÷ kids
4left ← candy rem kids

Try It 9. Trace these lines the way Example 9 does. What does line 5 show?

1masks ← 23
2hats ← masks ÷ 4
3masks ← masks rem 4
4done ← masks = 0
5show hats
Check your answer

Line 2 stores 5 in hats, because 4 * 5 is 20. Line 3 replaces masks with 23 rem 4, which is 3. Line 4 stores false in done, because 3 is not 0. Line 5 shows 5.