Popcorn Hack 1

Key Formatting Tools (from memory):

  1. \n - Newline character, moves cursor to the next line
  2. \t - Tab character, adds horizontal spacing/indentation
  3. System.out.print() - Prints without adding a newline at the end
  4. System.out.println() - Prints with a newline at the end
  5. System.out.printf() - Formatted print, allows placeholders like %d, %s, %f
  6. \\ - Escape sequence for backslash

Example line using multiple tools: System.out.println(“Name: John\nGPA: 3.85\nQuote: "Hello\World"”);

Popcorn Hack 2

  1. System.out.println() - Prints text and automatically adds a newline at the end
    • Used for the menu title, menu options, and informational messages
    • Makes each item appear on its own line for better readability
  2. System.out.print() - Prints text WITHOUT a newline at the end
    • Used for "Choose an option: " so the user’s input appears on the same line
    • Creates a cleaner prompt experience
  3. String concatenation (+) - Combines strings and variables
    • Used in "You selected option: " + choice
    • Used in "There are " + optionCount + " total options."
    • Allows dynamic output that includes user input and variable values
  4. Consistent formatting structure - The ==== borders and numbered list format
    • Creates visual hierarchy and organization
    • Makes the menu easy to scan and understand

How they make the output cleaner/better:

  • println() for menu items creates vertical spacing and clear separation between options, making the menu easy to read at a glance
  • print() for the prompt keeps the input on the same line as the question, creating a natural user experience (like: Choose an option: 1 instead of having the input on a new line)
  • String concatenation provides personalized feedback by incorporating the user’s choice into the response
  • Numbered list format (1, 2, 3) gives users a clear way to reference their choice
  • Border decoration (====) visually separates the menu from other output, drawing attention to the interactive section

Homework Hack 1

1. Predict Output:

AP CSA
Rocks!

2. Fix the Bug:

System.out.println("C:\\Users\\Student");

Explanation: The backslash \ is an escape character, so we need \\ to print a literal backslash.

3. Menu Hack:

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. Credits"); // Added 4th print line  (Homework Point here)

        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.");
    }
}
Menu.main(null);

==== Main Menu ====
1. Start Game
2. Instructions
3. Exit
4. Credits
Choose an option: You selected option: 4
There are 4 total options.

4. Challenge:

System.out.printf("Pi = %.2f\n", Math.PI);
Pi = 3.14





java.io.PrintStream@4c970aa1

Homework Hack 2 - Calculator MenuI’ve created a complete Calculator Menu program that:

Features:

  • Displays a formatted menu with 4 operations
  • Takes user input for menu choice
  • Prompts for two numbers
  • Uses arithmetic expressions (+, -, *, /) to compute results
  • Handles division by zero error
  • Uses printf to format the result with 2 decimal places
  • Includes a switch statement for clean operation selection

Example Output:

==== Calculator Menu ====
1. Add
2. Subtract
3. Multiply
4. Divide
Choose an option: 1
Enter first number: 10
Enter second number: 5
Result: 15.00
import java.util.Scanner;

public class CalculatorMenu {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Print menu
        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");
        
        // Get user choice
        System.out.print("Choose an option: ");
        int choice = scanner.nextInt();
        
        // Get two numbers
        System.out.print("Enter first number: ");
        double num1 = scanner.nextDouble();
        
        System.out.print("Enter second number: ");
        double num2 = scanner.nextDouble();
        
        // Calculate and display result
        double result = 0;
        boolean validOperation = true;
        
        switch(choice) {
            case 1:
                result = num1 + num2;
                break;
            case 2:
                result = num1 - num2;
                break;
            case 3:
                result = num1 * num2;
                break;
            case 4:
                if(num2 != 0) {
                    result = num1 / num2;
                } else {
                    System.out.println("Error: Cannot divide by zero!");
                    validOperation = false;
                }
                break;
            default:
                System.out.println("Invalid option!");
                validOperation = false;
        }
        
        if(validOperation) {
            System.out.printf("Result: %.2f\n", result);
        }
        
        scanner.close();
    }
}

CalculatorMenu.main(null);
==== Calculator Menu ====
1. Add
2. Subtract
3. Multiply
4. Divide
Choose an option: Enter first number: Enter second number: Result: 4.00