Popcorn Hacks 1 & 2
Was answered on the lesson during the class peiod.
Homework Hacks
Homework Hack 1
-
The output is that AP CSA is on one line and the next line says Rocks!
-
See below
System.out.println("C:\\Users\\Student");
C:\Users\Student
- See below.
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.");
}
}
- See below.
System.out.printf("Pi = %.2f\n", Math.PI);
Pi = 3.14
java.io.PrintStream@3d817c54
Homework Hack 2
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Print calculator 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");
// Ask user to choose an option
System.out.print("Choose an option: ");
int choice = sc.nextInt();
// Ask for two numbers
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
double result = 0; // variable to store the result
boolean validChoice = true;
// Compute result based on user choice
switch(choice) {
case 1: // Add
result = num1 + num2;
break;
case 2: // Subtract
result = num1 - num2;
break;
case 3: // Multiply
result = num1 * num2;
break;
case 4: // Divide
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Cannot divide by zero.");
validChoice = false;
}
break;
default:
System.out.println("Invalid choice.");
validChoice = false;
}
// Print result if valid
if (validChoice) {
System.out.println("Result: " + result);
}
}
}
==== Calculator Menu ====
- Add
- Subtract
- Multiply
- Divide Choose an option: 1 Enter first number: 10 Enter second number: 5 Result: 15.0
Homework Hack 3
- Photo submitted directly in form as asked.