Expressions Creators HW
For the creators group. 1.4.
import java.util.Scanner;
public class HacksDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// ==========================
// 🍿 Popcorn Hack 1
// ==========================
System.out.println("=== Popcorn Hack 1 ===");
System.out.print("System.out.print keeps text on same line. ");
System.out.println("System.out.println moves to a new line.");
System.out.printf("System.out.printf formats numbers: Pi = %.2f\n", Math.PI);
System.out.println("Escape sequences: \"Quotes\", backslash \\\\, new line\nNew line works!");
System.out.println("String concatenation: " + "Hello " + "World!");
System.out.println("Arithmetic expressions: 7 % 3 = " + (7 % 3));
// Example line using many tools
System.out.printf("Hello\tWorld!\nPi = %.2f and 5/2 = %d\n\n", Math.PI, (5/2));
// ==========================
// 🍿 Popcorn Hack 2
// ==========================
System.out.println("=== Popcorn Hack 2 ===");
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();
int optionCount = 3;
System.out.println("You selected option: " + choice);
System.out.println("There are " + optionCount + " total options.\n");
// ==========================
// 🏠 Homework Hack 1
// ==========================
System.out.println("=== Homework Hack 1 ===");
// Predict Output
System.out.print("AP ");
System.out.println("CSA");
System.out.println("Rocks!");
// Fix the bug (Windows path with escaped backslashes)
System.out.println("C:\\Users\\Student");
// Add 4th option (Settings) to Menu
System.out.println("==== Updated Menu ====");
System.out.println("1. Start Game");
System.out.println("2. Instructions");
System.out.println("3. Exit");
System.out.println("4. Settings");
optionCount = 4;
System.out.println("There are " + optionCount + " total options.\n");
// Print Pi with 2 decimals
System.out.printf("Pi = %.2f\n\n", Math.PI);
// ==========================
// 🏠 Homework Hack 2
// ==========================
System.out.println("=== Homework Hack 2 ===");
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 calcChoice = 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 (calcChoice == 1) result = num1 + num2;
else if (calcChoice == 2) result = num1 - num2;
else if (calcChoice == 3) result = num1 * num2;
else if (calcChoice == 4) result = num1 / num2;
// Print result with 2 decimals
System.out.printf("Result = %.2f\n", result);
sc.close();
}
}
HacksDemo.main(null);
=== Popcorn Hack 1 ===
System.out.print keeps text on same line. System.out.println moves to a new line.
System.out.printf formats numbers: Pi = 3.14
Escape sequences: "Quotes", backslash \\, new line
New line works!
String concatenation: Hello World!
Arithmetic expressions: 7 % 3 = 1
Hello World!
Pi = 3.14 and 5/2 = 2
=== Popcorn Hack 2 ===
==== Main Menu ====
1. Start Game
2. Instructions
3. Exit
Choose an option: You selected option: 1
There are 3 total options.
=== Homework Hack 1 ===
AP CSA
Rocks!
C:\Users\Student
==== Updated Menu ====
1. Start Game
2. Instructions
3. Exit
4. Settings
There are 4 total options.
Pi = 3.14
=== Homework Hack 2 ===
==== Calculator Menu ====
1. Add
2. Subtract
3. Multiply
4. Divide
Choose an option: Enter first number: Enter second number: Result = 0.67