Chapter 01 · Reps

Reading Code Like Scripture — Reps

← Back to Chapter 1

Chapter 1 — Reps

Conditioning, not grading. Reading reps this week — you will not write logic.

Ground rules:

  1. Type every line you do type yourself. No copy-paste. (Most reps are read-only.)
  2. Pen-and-paper or a scratch text file is required. Your reading notes are the rep.
  3. Predict before you run. Whenever a rep gives you a program, predict the output before you compile it.
  4. AI stays OFF for every Phase 1 rep. No exceptions. The reading muscle does not grow if a machine reads for you.

You’ll work in OnlineGDB with Java 17 (see Coding 1’s online-coding workflow appendix if you forgot the workflow) or your local JDK. Either path; don’t switch mid-rep.


Reps 1–4: Predict Before You Run

Rep 1 — Predict the Output

Without compiling, predict the exact output of this program (including spacing and order). Write your prediction down. Then run it.

public class Rep1 {
    public static void main(String[] args) {
        String name = "Maya";
        String name2 = "Maya";
        String name3 = new String("Maya");

        System.out.println(name == name2);
        System.out.println(name == name3);
        System.out.println(name.equals(name3));
        System.out.println(name3.length());
    }
}

If your prediction was wrong on any of the three booleans, re-read Coding 1 §13.6 — §7.6 in the accelerated edition. The == vs .equals() instinct must be automatic by the end of this week.


Rep 2 — List the Assumptions

Read this program. Do not run it. Write down, on paper:

  • Three assumptions the code makes about its inputs.
  • Two ways a user could make it crash or misbehave.
import java.util.Scanner;

public class GradeReport {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("How many scores? ");
        int n = in.nextInt();
        int total = 0;
        for (int i = 0; i < n; i++) {
            total += in.nextInt();
        }
        System.out.println("Average: " + (total / n));
        in.close();
    }
}

Hints (don’t read until after you’ve tried): integer overflow, division by zero, non-numeric input, negative n, mixing nextInt with nextLine.


Rep 3 — Look It Up

Open docs.oracle.com/en/java/javase/17/docs/api/. Navigate to java.lang.String. Find a method on String you have never used before. Examples to consider: strip(), repeat(int), chars(), codePointAt(int), formatted(Object...).

In two sentences in your notes:

  1. What does the method do?
  2. Under what circumstance would you reach for it instead of an alternative you already know?

You are not required to write code that uses it. You are required to be able to explain it.


Rep 4 — Trace the Data

Read the code below. With pen and paper, trace what happens when the user types Maya, then Marcus, then Maya again.

import java.util.ArrayList;
import java.util.Scanner;

public class CheckIn {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> seen = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            String name = in.nextLine();
            if (!seen.contains(name)) {
                seen.add(name);
                System.out.println("Welcome, " + name);
            } else {
                System.out.println("Already checked in: " + name);
            }
        }
        in.close();
    }
}

Write the exact output, line by line. Then run it. Compare. If your trace was wrong, find where you misread before you do anything else.


Reps 5–7: Read the Signatures First

Rep 5 — Signatures Only

Below are four method signatures from an unspecified class. Without seeing the bodies, write down:

  • What each method probably does.
  • What state the class probably owns to support these methods.
public boolean isOpen()
public void close()
public int readByte()
public byte[] readNBytes(int n)

Then look up java.io.InputStream in the API docs. Compare your guesses to the real Javadoc summaries.


Rep 6 — Convention as Evidence

Here is the import list and class signature of a file. Without seeing anything else, what does this class do?

package com.example.catechism;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

public class CatechismLoader {
    // ...
}

Write a one-sentence guess. Then list three method signatures you would expect to find in this class.


Rep 7 — The Lying Name

Read this method. Its name is getCount. Decide whether the name is honest.

public int getCount() {
    int n = list.size();
    list.clear();
    return n;
}

Why or why not? What would you rename it to? (No code change required — just a name and a one-line justification.)


Reps 8–9: Spot the Bugs

Rep 8 — Bug Hunt (Easy)

Read this program. Find every bug you can in five minutes. Don’t run it.

public class Average {
    public static void main(String[] args) {
        int[] nums = {10, 20, 30, 40};
        int total = 0;
        for (int i = 1; i <= nums.length; i++) {
            total += nums[i];
        }
        double avg = total / nums.length;
        System.out.println("Average: " + avg);
    }
}

At least three things are wrong. List them all. (Hint: bounds, integer division, off-by-one.)


Rep 9 — Bug Hunt (Subtle)

Same drill, harder code. Don’t run it. Find every bug.

public class Roster {
    private String[] names;
    private int size;

    public Roster(int capacity) {
        names = new String[capacity];
    }

    public void add(String name) {
        names[size] = name;
        size++;
    }

    public boolean contains(String name) {
        for (int i = 0; i < names.length; i++) {
            if (names[i] == name) return true;
        }
        return false;
    }
}

There are at least three issues. Some are logic bugs. Some are robustness bugs. List each and classify.


Rep 10 — The Comprehension Brief

This is the rep that becomes Project 1’s deliverable. Practice it now.

Pick any small (~100-line) Java program — from this textbook, from a public open-source project, from a friend, from anywhere — that you did not write. Spend 30 minutes producing a one-page brief that contains:

  1. What the program does (one paragraph).
  2. Each class and its role (one bullet per class).
  3. The data flow through main (a numbered list).
  4. Three concrete questions a reviewer would ask.
  5. Two bugs or fragilities you found.

If you cannot find a program, use the Hello World programs from Coding 1 Chapter 13’s reps. Even a small program produces a brief — and the move at scale is the same.

Compare your brief to a friend’s brief on the same program. The differences are the gap each of you needs to close.


Rep 11 — Read Without Looking

Take a small Java program you wrote in Coding 1 (Project 13 is a good choice — Project 6, the Java Migration, if you took the accelerated edition). Read it as if it were unfamiliar — produce a brief like Rep 10’s, but for your own code.

How much of it surprised you? How much of it could you defend in a code review? How much of it would you write differently today?

This is not graded. This is calibration.


Done? One Last Thing.

Open the standard library Javadocs for java.util.ArrayList. Read the class-level description. Then read the Javadoc for every public method on the class. (Yes, every one — it’s not as many as you think, maybe twenty-five.)

Write down: which three methods did you not know existed? When would you use each one?

This is the rep that makes you a senior reader of Java. The standard library is not a black box. The standard library is a book — and you are now the kind of student who reads books.


Up next: Project 1 — Project 1: The Code Comprehension Brief.