Expressions and Output
- CSA Unit 1.3 — Expressions and Output
- Learning Objectives
- Essential Knowledge
- Expressions in Java
- Popcorn Hack 1
- Real-World Example — Menu.java
- Popcorn Hack 2
- Homework Hack 1
- Homework Hack 2
- 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)
### 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.
```java
System.out.println(7 / 3); // 2
System.out.println(7.0 / 3); // 2.333333...
System.out.println(7 / 3.0); // 2.333333...
### Operator Precedence
Java uses order of operations (PEMDAS):
- Parentheses ()
- Multiplication *, Division /, Modulus %
- Addition +, Subtraction -
```java
System.out.println(2 + 3 * 4); // 14
System.out.println((2 + 3) * 4); // 20
### Combining Strings and Numbers
Using + with a string triggers string concatenation.
```java
System.out.println("Result: " + 5 + 3); // Result: 53
System.out.println("Result: " + (5 + 3)); // 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!
```python
// Example: println vs print
System.out.print("Hello");
System.out.print(" World");
System.out.println("!"); // moves to new line
System.out.println("Done");
Output:
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");
Output:
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
Output:
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.");
}
}
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.println("CSA"); System.out.println("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:
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.