Lesson 1.3 Expressions and Output
CSA Unit 1.3 — Expressions and Output
Popcorn Hack 1
Task:
Recall as many key formatting tools as you can without referring to the notes above. List them and explain what each does.
Then, write a single line of code using as many of the tools as possible.
Answer:
\n → newline (moves text to the next line)
\t → tab (adds spacing)
\\ → prints a backslash
\" → prints a double quote
System.out.print() → prints text without moving to the next line
System.out.println() → prints text and moves to a new line
System.out.printf() → prints formatted text (e.g., decimals, alignment)
System.out.println("She said:\t\"Java\\CSA rocks!\"\nLet's code!");
She said: "Java\CSA rocks!"
Let's code!
Popcorn Hack 2
Task:
What formatting tools are used in Menu.java?
How do they make the output cleaner/better?
Answer:
- System.out.println() → separates each menu option on a new line.
- System.out.print() → keeps the user input prompt (“Choose an option:”) on the same line.
- String concatenation (+) → combines variables with text dynamically.
- Escape sequences (====) → make the menu organized and visually clear.
Together, they create a clean, structured, and easy-to-read menu for users.
Homework Hack 1
System.out.print("AP ");
System.out.println("CSA");
System.out.println("Rocks!");
AP CSA
Rocks!
//Fixed bug code
System.out.println("C:\\Users\\Student");
C:\Users\Student
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.println("4. Settings");
System.out.print("Choose an option: ");
int choice = sc.nextInt();
System.out.println("You selected option: " + choice);
int optionCount = 4;
System.out.println("There are " + optionCount + " total options.");
}
}
Challenge
System.out.printf("Pi = %.2f\n", Math.PI);
Pi = 3.14
java.io.PrintStream@2bf33799
Homework Hack 2
import java.util.Scanner;
public class CalculatorMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("==== Calculator Menu ====");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.print("Choose an option: ");
int option = sc.nextInt();
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
double result = 0;
if (option == 1) result = num1 + num2;
else if (option == 2) result = num1 - num2;
else if (option == 3) result = num1 * num2;
else if (option == 4) result = num1 / num2;
System.out.println("Result: " + result);
}
}
Homework Hack 2: Assignment vs Comparison Explanation
Task:
Write an explanation distinguishing between:
- Assignment operator (=)
- Comparison operator (==)
Include code examples showing proper usage of each, and explain a common bug that occurs when these are confused.
Answer:
1. Assignment Operator (=)
The assignment operator is used to store a value in a variable.
It evaluates the expression on the right side, then assigns that value to the variable on the left side.
Example: ```java int x = 5; // assigns the value 5 to x x = x + 2; // takes current value (5), adds 2, then stores 7 in x