CS I · Fri Dec 11 · 18.5 · We Do

Before the bell

You are responsible for all of this before the end of class.

  1. The discussion form in the I Do deck. All three sections. If you are here, this should be done.
  2. Your classwork. This form on the right. Section 1 before the exit ticket pops up; Section 2 past the floor.
  3. No Bench Basics today — this is a short or catch-up day.
  4. The exit ticket. It opens 20 minutes before the bell and lives on the last slide of this deck.
Classwork

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

The Exam Week in One Snippet

This reading works through one short code segment that uses the three bench skills of this week: an answer from the Java Quick Reference, indexOf and substring, and a prediction made before any check. The segment was run inside the standard wrapper with the course's Java.

One Snippet, Three Skills

1String word = "Second Line";
2int n = word.indexOf(" ");
3System.out.println(n);
4System.out.println(word.substring(n + 1));
5System.out.println(Math.pow(n, 2));
6
Line
36.0

Line 2 uses indexOf, which returns the position where the text it is given first appears. Positions in a String are counted from 0, so the S of Second is at position 0 and the space after the d is at position 6, which means that line 2 stores 6 in n and line 3 prints 6. When the text is not found anywhere in the String, indexOf returns -1 instead of a position.

Line 4 uses substring with one number, n + 1, which is 7. A substring that is given only a starting position runs from that position to the end of the String, so line 4 prints Line. A substring given two numbers starts at the first number and stops one position before the second number.

Line 5 uses Math.pow, which raises a base to an exponent, so Math.pow(n, 2) works out 6 times 6. The Java Quick Reference lists Math.pow as returning a double, so 6 times 6 prints as 36.0 and not as 36. The Quick Reference also lists Math.abs as returning an int when it is given an int, so its answer for a whole number prints with no decimal point.

Line  Skill                      Printed
1     store a String             (nothing)
2     indexOf, count from 0      (nothing; n stores 6)
3     print an int               6
4     substring to the end       Line
5     Math.pow returns a double  36.0

Predict Before Checking

The mistake to catch in this snippet is a prediction of 36 for line 5. A student who looked up only what Math.pow does, and not what type it returns, would make that prediction. On B25, Check the Coach, a student makes a prediction first and only then reads Code Coach's answer and compares both with the real output. When a prediction and Code Coach's answer disagree, the student traces again the line where the two first differ, and then compares both with the real output. For this snippet, that line is line 5, and the Quick Reference entry for Math.pow settles it.

Sources: College Board, AP Computer Science A Course and Exam Description (2025); Bench Basics classroom data, B23, B24 and B25 (2026); the snippet run with the course JDK (2026).

One Whole Java Program

This section traces one complete Java program from start to stop, using almost every skill from the Java benches since November. The program is shown with the line numbers that the bench's code panel would display.

1public class Main
2{
3    public static int doubleIt(int n)
4    {
5        return n * 2;
6    }
7
8    public static void main(String[] args)
9    {
10        int cups = 3;
11        cups = doubleIt(cups) + 1;
12        System.out.println("Cups: " + cups);
13        if (cups % 2 == 0)
14        {
15            System.out.println("even");
16        }
17        else
18        {
19            System.out.println("odd");
20        }
21    }
22}
Cups: 7
odd

Where the Program Starts and What It Stores

The program is one class named Main, and its braces come in pairs that open and close each part. Inside the class are two methods, doubleIt on lines 3 to 6 and main on lines 8 to 21. The Java Virtual Machine starts every program by running the method named main, so this program starts at line 8 even though doubleIt is written above it. The first statement to run is line 10, which stores 3 in the variable cups. In Java, the = sign means store, so the value on the right is placed in the variable on the left. Line 11 uses = again to replace the 3 in cups.

A Method's Input and Output

Line 11 calls doubleIt and passes it the argument cups. A method's input is the value that goes into the method when it is called, so the input to doubleIt is 3, and the parameter n holds 3 while the method runs. The 7 that the program prints later is not the input to doubleIt, because the input is the argument passed in, which is 3. Next, line 5 multiplies n by 2 and returns 6, and since a method's output is the value that it sends back, the output of doubleIt is 6. Returning a value is not the same as printing it, because the output of doubleIt goes back to line 11, where the call was, and nothing appears on the screen at that moment. Back in main, Java adds 1 to the returned 6 and stores 7 in cups. Figure 1 shows this path as a curved arrow from line 11 into doubleIt and back down.

Printing and Deciding

Line 12 joins the text "Cups: " with the number stored in cups, and it prints Cups: 7. This printed line is the program's first line of output, and because println ends its line, the next printed text starts on a new line.

Line 13 is an if statement, and it decides which of two blocks runs. A condition is a test whose value is either true or false. The condition on line 13 asks whether the remainder of cups divided by 2 is equal to 0. Java works it out in the steps below.

the condition on line 13, one step at a time
cups % 2 == 0
7 % 2 == 0
1 == 0
false

The remainder on the left side of the comparison is not a condition by itself, because it is the number 1, not true or false. The whole comparison is the condition, because its value is false. Since the condition is false, Java skips the if block on lines 14 to 16 and runs the else block, so line 19 prints odd, and the program stops at the closing brace of main on line 21.

The Whole Trace and a Check with Code Coach

The trace table below follows the program in the order that its lines run.

Line   n    cups   Output
10          3
11          3
5      3    3
11          7
12          7      Cups: 7
13          7
19          7      odd

The variable n has a value only on the row for line 5, because n belongs to doubleIt and exists only while that method runs. Line 11 appears twice, because the call sends control into doubleIt and the returned value comes back to the same line before it is stored.

On the bench, the Ask Code Coach panel opens only after a student checks a prediction, such as the value of cups after line 11. On October 20, 2026, Code Coach was sent this whole program with its line numbers and asked what line 11 does, in the same form the bench uses. The request was run twice, and the first explanation is below, word for word.

Line 11 calls the `doubleIt` method with `cups` (which is 3) as the argument. The method returns 6 (3 * 2), then adds 1, making it 7. It then stores this value back into the `cups` variable. So, `cups` now stores 7.

The second explanation, word for word, follows.

Line 11 calls the `doubleIt` method with the value of `cups` (which is 3), doubles it to 6, adds 1 to make 7, and stores the result back in `cups`. It then prints: `Cups: 7`.

The first explanation matches the trace table. It names the 3 that goes in, the 6 that comes back and the 7 that is stored, so it meets requirement R2, which says that every explanation names what the code prints or stores. The second explanation also names the stored 7, but its last sentence goes past line 11, because the trace table shows that line 12 prints Cups: 7. That sentence breaks requirement R1, which says that Code Coach explains only the Java the student asked about. One run stayed on line 11 and one went past it. A model generates every explanation, so the student checks each one against the trace table and the real output before trusting it.

Summary

A Java program starts at main, stores values with =, and runs its lines in order unless a method call or an if statement changes that order. A method's input is the argument passed in, its output is the value it returns, and a returned value reaches the screen only when a println prints it. A condition is true or false, and it decides which block runs.

Sources: College Board, AP Computer Science A Course and Exam Description (2025); Oracle, The Java Language Specification, Java SE 21 Edition, §12.1 (2023); Code Coach captured explanations, cups-full-line11 (2026); Code Coach requirements and bench code, course repository (2026); CS I Reading 2 texts, Nov 2, Nov 3, Nov 18 and Dec 2 (2026); the program run with the course JDK (2026).

Classwork · Section 1 — Reading 2

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

MC 1. In the passage's program, where does the program start, and which statement runs first?

  • At line 1, because Java runs a file from the top, so the class header runs first
  • At line 3, because doubleIt is the first method written in the class
  • At main on line 8, so the first statement to run is line 10
  • At line 12, because it is the first line that prints something

MC 2. When line 11 calls `doubleIt(cups)`, what is the method's input?

  • 7, because 7 is the value of cups that the program prints
  • 6, because 6 is the number that doubleIt works with
  • n, because n is the name inside the method's parentheses
  • 3, the value stored in cups when the call happens

MC 3. What value does `cups` hold right after line 11 runs?

  • 6, because doubleIt returns 6
  • 7
  • 3, because a method call does not change a variable in main
  • 4, because line 11 adds 1 to cups

MC 4. When the program reaches the `if` statement on line 13, which block runs?

  • The if block, which prints even
  • The else block, which prints odd
  • Both blocks, so the program prints even and then odd
  • Neither block, because the condition is false

MC 5. The Java Quick Reference lists Math.abs. What does this code segment print?

1int drop = -7;
2System.out.println(Math.abs(drop));
  • -7
  • 7.0
  • 7
  • 0

MC 6. In Reading 2, line 5 prints 36.0 instead of 36. Why?

  • Math.pow returns a double, so its answer prints with a decimal point
  • n holds 6.0, because indexOf returns a decimal number
  • println adds .0 to every number it prints
  • Squaring a number always gives a decimal answer in Java

MC 7. What does this code segment print?

1String food = "Gumbo";
2System.out.println(food.substring(1, 4));
  • umbo
  • Gum
  • umb
  • Gumb

MC 8. What does this code segment print?

1String word = "bayou";
2System.out.println(word.indexOf("e"));
  • -1, because "e" does not appear in "bayou"
  • 0, because nothing was found at the first position
  • 5, because that is the length of "bayou"
  • Nothing, because the program stops with an error

MC 9. What does this code segment print?

1int temp = 80;
2if (temp > 90)
3{
4    System.out.println("hot");
5}
6else if (temp > 75)
7{
8    System.out.println("warm");
9}
10else
11{
12    System.out.println("cool");
13}
  • hot
  • warm
  • cool
  • warm, then cool on the next line

MC 10. A student predicts that `cups` holds 6 after line 11 of the passage's program, and Code Coach's explanation says 7. Which line should the student trace again first?

  • Line 11, because that is where the prediction and the explanation first differ, and the + 1 happens there
  • Line 1, because a trace should always start over from the top of the file
  • Line 19, because the last printed line decides who was right
  • None, because a model's explanation is always right about Java

CR 1. Write the full trace table for the passage's program, in the order its lines run. Use four columns: Line, n, cups and Output. Leave a cell blank when the variable has no value on that row.

Write your answer in your lab notebook, labeled CR 1.

CR 2. In two sentences, explain the difference between returning a value and printing it. Use doubleIt from the passage as your example.

Write your answer in your lab notebook, labeled CR 2.

Figure 1. The passage's program with the line numbers the bench shows, the call from line 11 into doubleIt and back, the condition on line 13 worked out, and the full trace table.
Classwork · Section 2 — Past the floor

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

CR 3. Predict what this code segment prints, line by line. Then explain how you got the first line of output.

1String street = "Magazine";
2System.out.println(street.substring(0, 3) + street.length());
3System.out.println("5" + 3);
4System.out.println(5 + 3);

Write your answer in your lab notebook, labeled CR 3.

CR 4. Line 11 of the passage's program changes to `cups = doubleIt(cups);` as shown below. Give the new output, and explain which branch of the if and else runs now.

1public class Main
2{
3    public static int doubleIt(int n)
4    {
5        return n * 2;
6    }
7
8    public static void main(String[] args)
9    {
10        int cups = 3;
11        cups = doubleIt(cups);
12        System.out.println("Cups: " + cups);
13        if (cups % 2 == 0)
14        {
15            System.out.println("even");
16        }
17        else
18        {
19            System.out.println("odd");
20        }
21    }
22}

Write your answer in your lab notebook, labeled CR 4.

Bench Basics

No bench today

There is no bench on this day. Finish your classwork and move to the exit ticket.

No bench today — the bench pop-up is off for this day.

Classwork

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

Exit Ticket

This opens during your class period.