1.03 Expressions and Output
Print with print, println, and printf, use escape sequences, and predict results using operator precedence and integer division.
1. Reference Guide
| Term | Definition / Example | College Board Requirement |
|---|---|---|
| Output | Information the program shows the user: System.out.println("Hi"); |
Unit 1, Topic 1.3, 1.3.A.1, pp. 33–34 |
| String literal | Text written inside double quotes: "Hello" |
Unit 1, Topic 1.3, pp. 33–34 |
| Escape sequence | A backslash plus a character, for characters a string cannot hold directly: "Line 1\nLine 2" |
Unit 1, Topic 1.3, pp. 33–34. Only \", \\, and \n are tested. |
| Arithmetic expression | Numbers and operators that work out to one value: 2 + 3 * 4 |
Unit 1, Topic 1.3, 1.3.C.5, pp. 33–34 |
| Integer division | int / int keeps only the whole-number part: 7 / 2 is 3 |
Unit 1, Topic 1.3, 1.3.C.3, pp. 33–34 |
| Remainder | % gives what is left over after dividing: 7 % 2 is 1 |
Unit 1, Topic 1.3, pp. 33–34 |
| Concatenation | + joins a String with another value: "Age: " + 16 |
— |
+adds numbers but joins Strings, and Java works left to right- Dividing an
intby0compiles, then throws anArithmeticException
Key Takeaways
- Output = communication from program to user.
printvsprintlnvsprintfcontrol formatting.- Escape sequences handle special characters and formatting.
- Arithmetic expressions make output dynamic.
Menu.java(Example B below) demonstrates real-world application of expressions + output.
Output Methods
| Method | What it does | Example | Prints |
|---|---|---|---|
print |
Prints and stays on the same line | System.out.print("Hi "); |
Hi |
println |
Prints, then moves to a new line | System.out.println("Hi"); |
Hi, then a new line |
printf |
Prints using a format pattern (not on the AP exam) | System.out.printf("%.2f", 3.14159); |
3.14 |
Escape Sequences
| Sequence | Meaning | Example | Prints |
|---|---|---|---|
\" |
Double quote | "She said \"hi\"" |
She said "hi" |
\\ |
Backslash | "C:\\Users" |
C:\Users |
\n |
New line | "A\nB" |
A and B on two lines |
\t |
Tab (not on the AP exam) | "A\tB" |
A and B with a tab between |
Operator Precedence
| Order | Operators | Example | Result |
|---|---|---|---|
| 1st | ( ) |
(2 + 3) * 4 |
20 |
| 2nd | *, /, %, left to right |
2 + 3 * 4 |
14 |
| 3rd | +, -, left to right |
10 - 4 - 3 |
3 |
Predicting Output
- Both values are
int→ the result is anint, decimals dropped - At least one
double→ the result is adouble - A String comes first with
+→ everything after it is joined as text, unless it is in parentheses print→ the next output stays on the same line;println→ the next output starts a new line
College Board Requirements
AP CSA Unit 1, Topic 1.3 Expressions and Output. Quoted from the course and exam description (College Board, 2025, pp. 33-34):
- 1.3.A.1 “System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.”
- 1.3.C.3 “When dividing numeric values that are both int values, the result is only the integer portion of the quotient.”
- 1.3.C.5 “Multiplication, division, and remainder have precedence over addition and subtraction. Operators with the same precedence are evaluated from left to right.”
The Java language specification says the same thing in one line: “Integer division rounds toward 0” (Gosling et al., 2023, section 15.17.2).
Note for the exam: the AP course uses only three escape sequences, \", \\, and \n. printf and \t are useful in real code but are not tested.
Reference: College Board, AP Computer Science A Course and Exam Description (2025), Unit 1, Topic 1.3, pp. 33–34.
2. Real-Life Application
The Stock Index That Lost Half Its Value by Rounding Down
In 1982, the Vancouver Stock Exchange started a new index at 1000. Its computer recalculated the index thousands of times a day and kept only three decimal places, chopping off the rest instead of rounding. Nothing crashed. Nothing looked wrong on any single update. Everyone guess: after about 22 months, what did the index read, and what should it have read?
| Real Case | What Happened | Java Idea in This Lesson |
|---|---|---|
| Vancouver Stock Exchange (1982 to 1983) | Each update threw away a tiny bit. By late 1983 the index read about 525, when the correct value was about 1099. | Integer division also throws away the decimal part: 7 / 2 is 3, every time |
| Store receipts | $4.5 looks broken; $4.50 looks right |
printf("%.2f", 4.5) prints exactly two decimal places |
| Windows file paths | C:\Users\Student |
A backslash inside a String must be written \\ |
| Text messages with quotes | She said "hi" |
A quote inside a String must be written \" |
The takeaway: predict the exact output before you run, especially when division or rounding is involved. Small errors repeated thousands of times add up.
3. Tech Talk
Learning Objective: Generate output, use string literals and escape sequences, and work out the result of arithmetic expressions.
Success Criteria: You can predict what print and println show, fix strings that need escape sequences, and evaluate expressions that use integer division, %, precedence, and string joining.
print stays on the same line. println moves to the next line.
Two int values always give an int, so division throws away the decimal part. One double changes that.
System.out.println(7 / 3); // 2
System.out.println(7.0 / 3); // 2.3333333333333335
System.out.println(7 % 3); // 1 (remainder)
System.out.println(2 + 3 * 4); // 14 (* before +)
+ joins strings, and Java works left to right:
System.out.println("Result: " + 5 + 3); // Result: 53
System.out.println("Result: " + (5 + 3)); // Result: 8
Dividing an int by 0 does not fail at compile time. It throws an ArithmeticException while running.
The code runner has no keyboard, so the menu examples read their choice from a String instead of waiting for you to type. Reading real input is lesson 1.4.
Code Examples
A. print vs println. The PrintDemo class shows which output stays on the line and which moves down
The two get mixed up a lot:
System.out.print("Hello"); // print: the next output stays on this line
System.out.println("Hello"); // println: the next output starts on a new line
Terminology:
Output is text the program sends to the console. print and println send the same text; the only difference is whether the cursor moves to a new line afterward.
Code Runner Challenge
Predict how many lines this prints, then run it
View IPYNB Source
// CODE_RUNNER: Predict how many lines this prints, then run it
public class PrintDemo {
public static void main(String[] args) {
// Example: println vs print
System.out.print("Hello");
System.out.print(" World");
System.out.println("!"); // moves to new line
System.out.println("Done");
}
}
PrintDemo.main(null);
B. Menu.java. The MenuDemo class combines literals, expressions, and output in one menu
A menu uses literals, expressions, and output together. The runner has no keyboard, so this copy reads its choice from a String. Change "2\n" to another option and run it again.
Common joining mistake:
"There are " + 3 + 1 + " options" // String first, so + joins: There are 31 options
"There are " + (3 + 1) + " options" // parentheses add first: There are 4 options
Terminology:
Concatenation: once a String is on the left, + joins instead of adding. Parentheses make the math happen first.
Code Runner Challenge
Change the simulated choice, then run it
View IPYNB Source
// CODE_RUNNER: Change the simulated choice, then run it
import java.util.Scanner;
public class MenuDemo {
public static void main(String[] args) {
Scanner sc = new Scanner("2\n"); // simulated keyboard input
System.out.println("==== Main Menu ====");
System.out.println("1. Start Game");
System.out.println("2. Instructions");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
int choice = sc.nextInt();
System.out.println("You selected option: " + choice);
int optionCount = 3;
System.out.println("There are " + optionCount + " total options.");
}
}
MenuDemo.main(null);
Hacks & Practice Tasks
Lesson: CSA 1.3 Expressions and Output
MCQ 1.3: <paste the copied result line, such as 3/4 | answers: B,C,A,D>
Popcorn: six formatting lines with a comment on each, runs and prints (yes/no); predicted lines = <p>; actual lines = <a>
Popcorn: Menu.java tools named in a comment = <short summary>
Homework Hack 1: prediction = <what the three prints show>, fixed path = <your corrected string>, both checks print true (yes/no)
Homework Hack 2: optionCount check prints true (yes/no), printf output = <what it prints>
Homework Hack 3: calculator check prints true (yes/no), results for options 2, 3, 4 = <r2>, <r3>, <r4>
Submission Safety Rules (Read First)
- One class per cell, ending with
ClassName.main(null);. - Label the homework hacks 1 through 3.
- Write your Hack 1 prediction before you run it.
- Run each Java cell and leave the output showing.
- Include your MCQ score.
- Use
##headings or smaller.
Popcorn Hack (In-Class)
2-minute challenge: from memory, write one line for each formatting job, then run the cell.
- Print two words that stay on the same line
- End a line so the next output starts below it
- Put a line break inside one string
- Put quotation marks inside a string
- Print a backslash
- Print a decimal with exactly 2 places
- In a comment, name the formatting tools
Menu.javauses and how they make its output cleaner - Answer the question in the Popcorn checker under the runner before you run. When every check passes, copy its line into your submission notes
Add a comment on each line saying what the tool does. Replace the sample lines with your own.
Code Runner Challenge
Replace the sample lines with your own, then run it
View IPYNB Source
// CODE_RUNNER: Replace the sample lines with your own, then run it
// Practice #1 - Try it yourself first!
public class FormattingTools {
public static void main(String[] args) {
// Try writing your own lines here first!
// Sample answer:
System.out.print("Hello "); // print: stays on the same line
System.out.println("World"); // println: moves to a new line after
System.out.println("Line one\nLine two"); // \n: new line inside a string
System.out.println("She said \"Java is fun\""); // \": quote inside a string
System.out.println("Folder: C:\\Games"); // \\: backslash inside a string
System.out.printf("Price: $%.2f\n", 4.5); // printf: exactly 2 decimal places
// Menu.java: println puts each option on its own line, print keeps the answer on the
// same line as "Choose an option: ", and + joins the numbers into the sentences.
}
}
FormattingTools.main(null);
Before you press Run, predict this output.
4. MCQ Check
4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your submission notes.
5. Homework Hack
Task: Do all three hacks. Hack 1 starts with a compiler error on purpose: run it, read the error, then fix it. Each starter compiles before you edit it, so you can run the initial version and compare its incorrect output with the expected output. Change only what the TODO asks for, and keep going until every check prints true.
Optional: Game. Play the 1.3 and 1.4 Game, screenshot your score, and send it to the 1.3 form (it covers 1.3 and 1.4). It is extra practice and is not graded.
Solution Skeleton:
Hack 1: Predict and Fix Output (5 minutes)
Output and escape sequences.
Part 1: Predict. What do the three print lines show? Write your prediction in prediction, then run it.
Part 2: Fix the bug. This should print C:\Users\Student, but it will not compile. Fix it.
Code Runner Challenge
Run it to see the compiler error, fix the path, write your prediction, then run until both checks print true.
View IPYNB Source
// CODE_RUNNER: Run it to see the compiler error, fix the path, write your prediction, then run until both checks print true.
public class OutputHack {
public static void main(String[] args) {
// Part 1: Predict. Write what the three print lines show BEFORE you run.
String prediction = "TODO: your prediction";
System.out.print("AP ");
System.out.print("CSA ");
System.out.print("Rocks!");
System.out.println();
// Part 2: Fix the bug. This should print C:\Users\Student, but it will not compile.
String path = "C:\Users\Student";
System.out.println(path);
System.out.println(prediction.equals("AP CSA Rocks!"));
System.out.println(path.length() == 16);
}
}
OutputHack.main(null);
Hack 2: Menu Hack (5 minutes)
Expressions and output.
Part 3: Menu Hack. Add a 4th option, Settings, to Menu.java and update optionCount.
Part 4: Challenge. Use printf to print pi with 2 decimals: System.out.printf("Pi = %.2f\n", Math.PI);
Code Runner Challenge
Add Settings, update optionCount, and add the printf line so the check prints true.
View IPYNB Source
// CODE_RUNNER: Add Settings, update optionCount, and add the printf line so the check prints true.
import java.util.Scanner;
public class MenuHack {
public static void main(String[] args) {
Scanner sc = new Scanner("2\n"); // simulated keyboard input
System.out.println("==== Main Menu ====");
System.out.println("1. Start Game");
System.out.println("2. Instructions");
System.out.println("3. Exit");
// Part 3: TODO: print a 4th option, Settings.
System.out.print("Choose an option: ");
int choice = sc.nextInt();
System.out.println("You selected option: " + choice);
int optionCount = 3; // Part 3: TODO: update the count
System.out.println("There are " + optionCount + " total options.");
// Part 4: TODO: print pi with 2 decimals using printf.
System.out.println(optionCount == 4);
}
}
MenuHack.main(null);
Hack 3: Calculator Menu (10 minutes)
Combined. Part 5: Calculator menu. Grow Menu.java into a calculator with Add, Subtract, Multiply, and Divide. Skeleton below. When the check prints true, change the simulated choice to 2, 3, and 4 and confirm each result.
Code Runner Challenge
Fill in the blanks so the check prints true, then try the other options.
View IPYNB Source
// CODE_RUNNER: Fill in the blanks so the check prints true, then try the other options.
import java.util.Scanner;
public class CalculatorMenu {
public static void main(String[] args) {
// The runner has no keyboard, so the choice and numbers come from a String.
Scanner sc = new Scanner("1\n10\n5\n");
System.out.println("==== Calculator Menu ====");
System.out.println("1. Add");
// 1. Print the other three options
System.out.print("Choose an option: ");
int choice = sc.nextInt();
System.out.print("Enter first number: ");
int first = sc.nextInt();
System.out.print("Enter second number: ");
int second = sc.nextInt();
// 2. Work out the result for the chosen option
int result = 0;
System.out.println("Result: " + result);
System.out.println(result == 15); // 10 + 5 for option 1
}
}
CalculatorMenu.main(null);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | Six formatting lines, each with a comment saying what the tool does, the Menu.java tools named, and the cell runs. The Popcorn checker line is in the notes. |
| MCQ | 0.2 | 4 correct. 0.15 for 2 or 3, 0.1 if every question was answered. |
| Homework Hack 1 | 0.15 | Predicts AP CSA Rocks! on one line and escapes the backslashes so the path prints; both checks print true. |
| Homework Hack 2 | 0.2 | Settings added and optionCount is 4 so the check prints true, and printf prints pi with two decimals. |
| Homework Hack 3 | 0.25 | Four options, two numbers read, the correct result printed for each option, and the check prints true. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - Popcorn has all six formatting lines and names the
Menu.javatools. - Popcorn checker line in the notes.
- MCQ score in the notes.
\\in the fixed Hack 1 path.- Every homework check prints
true.
6. LxD Cycle Process
Empathize: Math class says 7 / 3 is about 2.33, but Java prints 2, and “Result: “ + 5 + 3 prints Result: 53. In a seven-country study, many students could not predict what short pieces of code would print (Lister et al., 2004).
Define:
- POV: CSA students need to predict exactly what a line of Java prints, because integer division, precedence, and string joining do not match the math they already know.
- Learning Goal: Students will print output with print and println, use escape sequences in strings, and work out the value of arithmetic expressions.
Ideate:
- HMW Question: How might we get students to commit to a prediction before they press run?
- HMW Question: How might we show that the same printing skills are how they will debug the class project?
- Activity: Predict the output of short programs, run them to check, fix a broken string, then grow Menu.java into a calculator menu.
Prototype:
- A reference guide, runnable print and Menu.java examples, a formatting-tools practice runner, an MCQ knowledge check, three scaffolded homework hacks with self-checking output, an optional game, and a grading rubric.
- Students write a prediction before each run and revise it after seeing the real output or the compiler error.
- Excellence means predicting the exact output, including integer division, precedence, and string joining, and improving the first attempt rather than only producing working code.
Test:
- Ask peers in peer review to complete the practice without additional explanation.
- Observe whether peers commit to a prediction before they press run, and whether they predict integer division and string joining correctly.
- Compare your lesson with another that is posted.
- Use the findings to revise any instruction or rubric criterion that did not guide students clearly.
- On submission, collect evidence from runner output, MCQ results, game screenshots, AI grading and student explanations.
- After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.
7. References/Citations
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025]. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java® language specification (Java SE 21 ed.). Oracle. https://docs.oracle.com/javase/specs/jls/se21/html/index.html
Lister, R., Adams, E. S., Fitzgerald, S., Fone, W., Hamer, J., Lindholm, M., McCartney, R., Moström, J. E., Sanders, K., Seppälä, O., Simon, B., & Thomas, L. (2004). A multi-national study of reading and tracing skills in novice programmers. In Working group reports from ITiCSE on Innovation and Technology in Computer Science Education (pp. 119–150). Association for Computing Machinery. https://doi.org/10.1145/1044550.1041673
Submit Assignment
Need to update a submission later? Open the submissions dashboard.