Expressions and Output
- CSA Unit 1.3 — Expressions and Output
- Learning Objectives
- Essential Knowledge
- Expressions in Java
- Outputs
- Popcorn Hack 1
- Real-World Example — Menu.java
- Popcorn Hack 2
- Homework Hack 1
- Homework Hack 2
- Homework Hack 3 Game
- Key Takeaways
CSA Unit 1.3 — Expressions and Output
Learning Objectives
- 1.3.A: Develop code to generate output and determine the result that would be displayed.
- 1.3.B: Develop code to utilize string literals and determine the result of using string literals.
- 1.3.C: Develop code for arithmetic expressions and determine the result of these expressions.
Essential Knowledge
- 1.3.A.1:
System.out.printandSystem.out.printlndisplay information.println→ prints + moves to a new line.print→ prints but stays on the same line.printf→ advanced formatting with placeholders.
- 1.3.B.1: A literal is a fixed value (like
5,3.14, or"Hello"). - 1.3.B.2: A string literal is text inside double quotes.
- 1.3.B.3: Escape sequences let you control formatting:
\"→ double quote\\→ backslash\n→ newline\t→ tab
- 1.3.C.1: Arithmetic expressions use
intanddoublewith operators (+,-,*,/,%). - 1.3.C.2: Division with integers truncates toward zero.
%gives the remainder.
Expressions in Java
Expressions are combinations of literals, variables, and operators that Java evaluates to produce a value.
In Unit 1.3, we focus on arithmetic expressions.
Arithmetic Operators
+→ addition-→ subtraction*→ multiplication/→ division%→ modulus (remainder)
System.out.println(7 + 3); // 10
System.out.println(7 - 3); // 4
System.out.println(7 * 3); // 21
System.out.println(7 / 3); // 2 (integer division)
System.out.println(7 % 3); // 1 (remainder)
10
4
21
2
1
Integer vs. Decimal Division
-
If both operands are int, the result is truncated (no decimals).
-
If at least one operand is a double, the result is a double.
System.out.println(7 / 3); // 2
System.out.println(7.0 / 3); // 2.333333...
System.out.println(7 / 3.0); // 2.333333...
2
2.3333333333333335
2.3333333333333335
Operator Precedence
Java uses order of operations (PEMDAS):
-
Parentheses ()
-
Multiplication *, Division /, Modulus %
-
Addition +, Subtraction -
System.out.println(2 + 3 * 4); // 14
System.out.println((2 + 3) * 4); // 20
14
20
Combining Strings and Numbers
Using + with a string triggers string concatenation.
System.out.println("Result: " + 5 + 3); // Result: 53
System.out.println("Result: " + (5 + 3)); // Result:
Result: 53
Result: 8
Outputs
Types of Output
- System.out.print() → prints text without newline.
- System.out.println() → prints text with newline.
- System.out.printf() → formatted printing (useful for decimals, alignment).
👉 Think of output as the program talking back to the user. Clear formatting is key!
// Example: println vs print
System.out.print("Hello");
System.out.print(" World");
System.out.println("!"); // moves to new line
System.out.println("Done");
Hello World!
Done
// Example: Escape Sequences
System.out.println("She said: \"Java is fun!\"");
System.out.println("Line1\nLine2");
System.out.println("C:\\Users\\Student");
System.out.println("Column1\tColumn2");
She said: "Java is fun!"
Line1
Line2
C:\Users\Student
Column1 Column2
Popcorn Hack 1
// Example: Arithmetic Expressions
int a = 7;
int b = 3;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b)); // integer division
System.out.println("a % b = " + (a % b));
double x = 7.0;
double y = 3.0;
System.out.println("x / y = " + (x / y)); // floating-point division
a + b = 10
a - b = 4
a * b = 21
a / b = 2
a % b = 1
x / y = 2.3333333333333335
Real-World Example — Menu.java
Menus use expressions and output to guide the user. This is a real-world example of combining literals, escape sequences, and arithmetic into meaningful display.
- Output gives users choices.
- Expressions let us count and customize menus.
- Input (preview of Unit 1.4) lets users interact with the program.
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
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.");
}
}
Menu.main(new String[]{});
==== Main Menu ====
1. Start Game
2. Instructions
3. Exit
Choose an option: You selected option: 1
There are 3 total options.
Popcorn Hack 2
Why Menu.java Matters
- Uses output to display instructions.
- Uses expressions to dynamically show option count.
- Uses escape sequences for clean formatting.
- Real-world: menus are everywhere (ATMs, games, apps, vending machines).
Homework Hack 1
- Predict Output: What will this print?
System.out.print("AP ");
System.out.print("CSA ");
System.out.print("Rocks!");
- Fix the Bug: The following is supposed to print
C:\Users\Student, but it fails. Correct it:
System.out.println("C:\Users\Student");
-
Menu Hack: Add a 4th option (
Settings) toMenu.javaand updateoptionCountaccordingly. -
Challenge: Use
System.out.printfto print pi with 2 decimals.
System.out.printf("Pi = %.2f\n", Math.PI);
Homework Hack 2
- Expand
Menu.javainto a calculator menu:- Print a menu with options:
Add,Subtract,Multiply,Divide. - Ask the user for two numbers.
- Use arithmetic expressions to compute and display the result.
- Example run:
```
==== Calculator Menu ====
- Add
- Subtract
- Multiply
- Divide Choose an option: 1 Enter first number: 10 Enter second number: 5 Result: 15 ```
- Print a menu with options:
Homework Hack 3 Game
Here is the game that you will also be playing, to test your understanding of the lesson. You will screenshot the game score and submit it to the 1.3 google form (this will alos include 1.4 content, submission is for both)
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.