Skip to the content.

Expressions and Outputs

Student Teaching Lesson

Popcorn Hacks 1

(+) adds two values.

(-) subtracts one value from another or makes a value negative.

(*) multiplies two values.

(/) divides one value by another to get the quotient.

(%) gives the remainder after integer division (great for even/odd checks or wrapping).

int result = (((12 + 8) * 3 - 10) / 5) % 4; 

System.out.println("Result: " + result);
Result: 2

Popcorn Hack 2

It prints lines with println and keeps the prompt on the same line with print. It adds values into messages using +, and uses \n (new line) or \t (tab) for spacing. Making the menu neat, easy to read, and easy to use.


Homework Hack 1

1) Predicted Output

AP CSA
Rocks!

2) Fix The Bug

System.out.println("C:\\Users\\Student"); //needed to add double slashes
C:\Users\Student

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. Settings");   // new option

        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."); //updated count
    }
}

Menu.main(null);
==== Main Menu ====
1. Start Game
2. Instructions
3. Exit
4. Settings
Choose an option: You selected option: 3
There are 4 total options.

4) Challenge

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





java.io.PrintStream@3db818e7

Homework Hack 2

import java.util.Scanner;

public class Menu {
    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 a = sc.nextDouble();
        System.out.print("Enter second number: ");
        double b = sc.nextDouble();

        double result;
        switch (option) {
            case 1: result = a + b; break;
            case 2: result = a - b; break;
            case 3: result = a * b; break;
            case 4:
                if (b == 0) {
                    System.out.println("Error: cannot divide by zero.");
                    sc.close();
                    return;
                }
                result = a / b; 
                break;
            default:
                System.out.println("Invalid option.");
                sc.close();
                return;
        }

        System.out.println("Result: " + result);
        sc.close();
    }
}

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