1.03 Expressions and Output
Write arithmetic expressions following operator precedence and print results to the console.
1. 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 & Test: Lesson authors: add what happened in your trial run and what you changed.
2. Lesson Plan
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.
Tech Talk (5 minutes)
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.
Code Runner Challenge
Predict how many lines this prints, then run it
View IPYNB Source
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:
Code Runner Challenge
Change the simulated choice, then run it
View IPYNB Source
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.
From the College Board
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.
3. Reference Guide
Key Takeaways
- Output = communication from program to user.
printvsprintlnvsprintfcontrol formatting.- Escape sequences handle special characters and formatting.
- Arithmetic expressions make output dynamic.
Menu.javademonstrates real-world application of expressions + output.
4. Code Examples
A. Simple: print vs println
Code Runner Challenge
Write your prediction first, then run it to check
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. Complex: Menu.java
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.
Code Runner Challenge
Run it to see the compiler error, then fix the string and run it again
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);
5. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one raw cell at the top with the frontmatter:
Code Runner Challenge
Fill in the blanks, then run it
View IPYNB Source
---
layout: post
codemirror: true
title: Expressions and Output HW
categories: [Java]
lesson_language: Java
lesson_topic: Expressions-and-Output HW
lesson_part: interactive
lesson_type: lesson
permalink: /csa/unit_01/1_3-hw
author: yourGithubID
---
- Add code cells for the Popcorn Hack and the Homework Hack. Make sure every cell runs with visible output.
- Submit the link to your published page at the bottom of this page, and paste this in the description box:
Lesson: CSA 1.3 Expressions and Output
MCQ 1.3: <paste the copied result line, such as 5/6 | answers: B,C,A,D>
Popcorn Q1 (formatting tools): <short summary>
Popcorn Q2 (tools in Menu.java): <short summary>
Part 1 prediction: <what the three prints show>
Part 2 fixed line: <your corrected string>
Part 3: Settings added, optionCount = <value>
Part 4 printf output: <what it prints>
Part 5 calculator menu runs and prints the right result (yes/no)
Part 6: game screenshot sent to the 1.3 form (yes/no)
Submission Safety Rules (Read First)
- The answer boxes only save in your browser, so copy both answers into your notebook.
- Label the homework parts 1 through 6.
- Run each Java cell and leave the output showing.
- Include your MCQ score.
- Use
##headings or smaller.
Popcorn Hack (In-Class)
2-minute challenge: answer from memory first, save, then use Copy both answers to paste them into your notebook. Question 2 is about Menu.java above.
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.
Homework Hack
Do all six parts.
Part 1: Predict. What does this print? Write your prediction, then run it.
// CODE_RUNNER: Write your prediction first, then run it to check
public class PredictOutput {
public static void main(String[] args) {
System.out.print("AP ");
System.out.print("CSA ");
System.out.print("Rocks!");
}
}
PredictOutput.main(null);
Part 2: Fix the bug. This should print C:\Users\Student, but it will not compile. Fix it.
// CODE_RUNNER: Run it to see the compiler error, then fix the string and run it again
public class BackslashBug {
public static void main(String[] args) {
System.out.println("C:\Users\Student");
}
}
BackslashBug.main(null);
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);
Part 5: Calculator menu. Grow Menu.java into a calculator with Add, Subtract, Multiply, and Divide. Skeleton below.
Part 6: Game. Play the game below, screenshot your score, and send it to the 1.3 form (it covers 1.3 and 1.4).
// CODE_RUNNER: Fill in the blanks, then run it
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);
}
}
CalculatorMenu.main(null);
6. Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | Both answers name the tools, say what each does, and include a line of code. |
| MCQ | 0.2 | 5 or 6 correct. 0.15 for 3 or 4, 0.1 if every question was answered. |
| Homework Part 1 | 0.05 | Predicts AP CSA Rocks! on one line. |
| Homework Part 2 | 0.05 | Escapes the backslashes so the path prints. |
| Homework Part 3 | 0.05 | Settings added and optionCount is 4. |
| Homework Part 4 | 0.05 | printf prints pi with two decimals. |
| Homework Part 5 | 0.3 | Four options, two numbers read, correct result printed. |
| Homework Part 6 | 0.1 | Game score screenshot. |
| Total | 1.0 |
Quick Validation Checklist
- Both Popcorn answers copied into the notebook.
- MCQ score in the notes.
\\in the fixed Part 2 string.SettingsandoptionCount = 4in Part 3.- Part 5 output shows the menu and a
Result:line. - Game screenshot.
7. Lesson Revisions & Feedback Evidence
Feedback Received: Lesson authors: what your peers said in the practice run.
Revision Made: Lesson authors: what you changed because of it.
References
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.