/**
 * APCS-A Unit 1 Review Application
 * Topic: Primitive Types and Variables
 * 
 * This program reviews key concepts from Unit 1:
 * - Primitive data types (int, double, boolean, char)
 * - Variable declaration and initialization
 * - Arithmetic operations and operator precedence
 * - Type casting (implicit and explicit)
 * - Input/output operations
 * - String operations (though String is technically an object, not primitive)
 * 
 * PROBLEM AREAS TO WATCH:
 * 1. Integer division vs. floating-point division
 * 2. Operator precedence and associativity
 * 3. Type casting rules and data loss
 * 4. Overflow and underflow
 * 5. Comparison of floating-point numbers
 * 
 * @author Your Name
 * @date November 2025
 * 
 * Chatlog:
 * 1. I assume you are familiar with the APCS-A Exam (in Java) offered by the College Board in May 2026. My teacher said to expect a multiple-choice and/or Free Response test soon on Unit 1 (There are 4 units featured on the test). Can you create a Java app (APCSUnit1Review.java.) that will review me on the important topics in that unit, and also create a .md file (APCSUnit1Review.md) that I can study to be better prepared for the test? Use lots of comments and anticipate where 'problem areas' might arise and draw them to my attention.
 * 2.I am very pleased with this markdown file. What would it look like if we converted it to a text file: APCSUnit1Review.txt What are the pros and cons of this format versus a text file?
 * 3. My teacher has a website where he attempts to summarize the 'big picture' ideas about computer science including summaries to prepare for the APCS Exam in Java. Can you take what you've developed in these two files and leverage it as a webpage, apcsUnit1Review.html using HTML, CSS, Bootstrap5 and if necessary, JavaScript? I'd like to see the teacher include it in his site!
 */

import java.util.Scanner;
import java.text.DecimalFormat;

public class APCSUnit1Review {
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("=== APCS-A Unit 1: Primitive Types Review ===\n");
        
        // Section 1: Variable Declaration and Initialization
        demonstratePrimitiveTypes();
        
        // Section 2: Arithmetic Operations and Operator Precedence
        demonstrateArithmeticOperations();
        
        // Section 3: Type Casting
        demonstrateTypeCasting();
        
        // Section 4: Common Pitfalls and Problem Areas
        demonstrateCommonPitfalls();
        
        // Section 5: Interactive Practice
        interactivePractice(input);
        
        input.close();
        System.out.println("\n=== Review Complete! Check APCSUnit1Review.md for study notes ===");
    }
    
    /**
     * Demonstrates the four primitive data types tested on APCS-A
     * PROBLEM AREA: Students often confuse when to use int vs double
     */
    public static void demonstratePrimitiveTypes() {
        System.out.println("1. PRIMITIVE DATA TYPES");
        System.out.println("========================");
        
        // int: whole numbers from -2,147,483,648 to 2,147,483,647
        int studentCount = 25;
        int temperature = -10;
        System.out.println("int examples: studentCount = " + studentCount + ", temperature = " + temperature);
        
        // double: decimal numbers (64-bit floating point)
        double gpa = 3.85;
        double pi = 3.14159;
        System.out.println("double examples: gpa = " + gpa + ", pi = " + pi);
        
        // boolean: true or false only
        boolean isRaining = true;
        boolean testPassed = false;
        System.out.println("boolean examples: isRaining = " + isRaining + ", testPassed = " + testPassed);
        
        // char: single character in single quotes
        char grade = 'A';
        char initial = 'K';
        System.out.println("char examples: grade = " + grade + ", initial = " + initial);
        
        // PROBLEM AREA: char vs String confusion
        System.out.println("\n⚠️  WATCH OUT: char uses single quotes 'A', String uses double quotes \"Hello\"");
        
        System.out.println();
    }
    
    /**
     * Demonstrates arithmetic operations and operator precedence
     * MAJOR PROBLEM AREA: Integer division and operator precedence
     */
    public static void demonstrateArithmeticOperations() {
        System.out.println("2. ARITHMETIC OPERATIONS & OPERATOR PRECEDENCE");
        System.out.println("===============================================");
        
        int a = 10, b = 3;
        double x = 10.0, y = 3.0;
        
        // Basic arithmetic operations
        System.out.println("Basic operations with int a = " + a + ", b = " + b + ":");
        System.out.println("a + b = " + (a + b));
        System.out.println("a - b = " + (a - b));
        System.out.println("a * b = " + (a * b));
        
        // CRITICAL PROBLEM AREA: Integer division
        System.out.println("a / b = " + (a / b) + " ⚠️  INTEGER DIVISION - result is " + (a/b) + ", not 3.333...");
        System.out.println("a % b = " + (a % b) + " (modulus - remainder)");
        
        System.out.println("\nWith double x = " + x + ", y = " + y + ":");
        System.out.println("x / y = " + (x / y) + " ✓ FLOATING-POINT DIVISION");
        
        // Operator precedence demonstration
        System.out.println("\nOperator Precedence Examples:");
        int result1 = 2 + 3 * 4;  // Should be 14, not 20
        int result2 = (2 + 3) * 4; // Should be 20
        System.out.println("2 + 3 * 4 = " + result1 + " (multiplication first!)");
        System.out.println("(2 + 3) * 4 = " + result2 + " (parentheses first)");
        
        // Increment/Decrement operators
        int counter = 5;
        System.out.println("\nIncrement/Decrement (counter starts at " + counter + "):");
        System.out.println("counter++ = " + (counter++) + " (post-increment, returns old value)");
        System.out.println("counter is now: " + counter);
        System.out.println("++counter = " + (++counter) + " (pre-increment, returns new value)");
        
        // PROBLEM AREA: Mixed operations
        System.out.println("\n⚠️  MIXED OPERATIONS PROBLEM AREA:");
        System.out.println("5 / 2 * 2.0 = " + (5 / 2 * 2.0) + " (left-to-right: 5/2=2, then 2*2.0=4.0)");
        System.out.println("5 / (2 * 2.0) = " + (5 / (2 * 2.0)) + " (parentheses first: 2*2.0=4.0, then 5/4.0=1.25)");
        
        System.out.println();
    }
    
    /**
     * Demonstrates type casting - both implicit and explicit
     * PROBLEM AREA: Data loss during casting and casting rules
     */
    public static void demonstrateTypeCasting() {
        System.out.println("3. TYPE CASTING");
        System.out.println("================");
        
        // Implicit casting (widening) - automatic, safe
        System.out.println("Implicit Casting (Widening - Safe):");
        int intValue = 42;
        double doubleValue = intValue;  // int to double - automatic
        System.out.println("int " + intValue + " → double " + doubleValue);
        
        char charValue = 'A';
        int asciiValue = charValue;     // char to int - automatic
        System.out.println("char '" + charValue + "' → int " + asciiValue + " (ASCII value)");
        
        // Explicit casting (narrowing) - manual, potential data loss
        System.out.println("\nExplicit Casting (Narrowing - Potential Data Loss):");
        double largeDouble = 3.14159;
        int truncatedInt = (int) largeDouble;  // Explicit cast required
        System.out.println("double " + largeDouble + " → int " + truncatedInt + " ⚠️  DECIMAL PART LOST!");
        
        // PROBLEM AREA: Data loss examples
        System.out.println("\n⚠️  DATA LOSS EXAMPLES:");
        double tooLarge = 2147483648.0;  // Larger than max int
        int overflowResult = (int) tooLarge;
        System.out.println("double " + tooLarge + " → int " + overflowResult + " (OVERFLOW!)");
        
        // Casting in expressions
        System.out.println("\nCasting in Expressions:");
        int num1 = 7, num2 = 2;
        double result1 = (double) num1 / num2;  // Cast one operand
        double result2 = (double) (num1 / num2); // Cast the result
        System.out.println("(double) " + num1 + " / " + num2 + " = " + result1);
        System.out.println("(double) (" + num1 + " / " + num2 + ") = " + result2 + " ⚠️  Different result!");
        
        System.out.println();
    }
    
    /**
     * Demonstrates common pitfalls and problem areas for Unit 1
     * These are frequently tested concepts that students struggle with
     */
    public static void demonstrateCommonPitfalls() {
        System.out.println("4. COMMON PITFALLS & PROBLEM AREAS");
        System.out.println("===================================");
        
        // Pitfall 1: Integer division
        System.out.println("Pitfall 1: Integer Division");
        System.out.println("int average = (85 + 90 + 78) / 3;");
        int average = (85 + 90 + 78) / 3;
        System.out.println("Result: " + average + " ⚠️  Expected 84.33, got " + average);
        System.out.println("Fix: double average = (85 + 90 + 78) / 3.0;");
        double correctAverage = (85 + 90 + 78) / 3.0;
        System.out.println("Correct result: " + correctAverage);
        
        // Pitfall 2: Operator precedence
        System.out.println("\nPitfall 2: Operator Precedence");
        System.out.println("int result = 2 + 3 * 4 - 1;");
        int result = 2 + 3 * 4 - 1;
        System.out.println("Many students think: (2+3) * (4-1) = 15");
        System.out.println("Actual result: " + result + " (2 + 12 - 1 = 13)");
        
        // Pitfall 3: Floating-point precision
        System.out.println("\nPitfall 3: Floating-Point Precision");
        double val1 = 0.1 + 0.2;
        System.out.println("0.1 + 0.2 = " + val1);
        System.out.println("⚠️  Not exactly 0.3 due to floating-point representation!");
        
        // Pitfall 4: Modulus with negatives
        System.out.println("\nPitfall 4: Modulus with Negative Numbers");
        System.out.println("-7 % 3 = " + (-7 % 3) + " (result has same sign as dividend)");
        System.out.println("7 % -3 = " + (7 % -3) + " (result has same sign as dividend)");
        
        // Pitfall 5: Uninitialized variables
        System.out.println("\nPitfall 5: Variable Initialization");
        System.out.println("⚠️  ALWAYS initialize variables before use!");
        System.out.println("int x; // Declared but not initialized");
        System.out.println("// System.out.println(x); // COMPILER ERROR!");
        int x = 0; // Proper initialization
        System.out.println("int x = 0; // Properly initialized to " + x);
        
        System.out.println();
    }
    
    /**
     * Interactive practice section for hands-on learning
     */
    public static void interactivePractice(Scanner input) {
        System.out.println("5. INTERACTIVE PRACTICE");
        System.out.println("========================");
        
        System.out.println("Let's practice! I'll give you some expressions to evaluate.");
        System.out.println("Try to predict the output before I show you the answer.\n");
        
        // Practice problem 1
        System.out.println("Practice 1: What is the value of: 15 / 4 * 2.0");
        System.out.print("Your prediction: ");
        String userAnswer1 = input.nextLine();
        double actual1 = 15 / 4 * 2.0;
        System.out.println("Actual answer: " + actual1);
        System.out.println("Explanation: 15/4 = 3 (integer division), then 3 * 2.0 = 6.0\n");
        
        // Practice problem 2
        System.out.println("Practice 2: What is the value of: (int) (7.9 + 2.3)");
        System.out.print("Your prediction: ");
        String userAnswer2 = input.nextLine();
        int actual2 = (int) (7.9 + 2.3);
        System.out.println("Actual answer: " + actual2);
        System.out.println("Explanation: 7.9 + 2.3 = 10.2, then (int) 10.2 = 10 (truncated)\n");
        
        // Practice problem 3
        System.out.println("Practice 3: What is the value of: 17 % 5 + 3 * 2");
        System.out.print("Your prediction: ");
        String userAnswer3 = input.nextLine();
        int actual3 = 17 % 5 + 3 * 2;
        System.out.println("Actual answer: " + actual3);
        System.out.println("Explanation: 17%5 = 2, 3*2 = 6, then 2+6 = 8\n");
        
        // Variable practice
        System.out.println("Now let's practice variable operations:");
        System.out.print("Enter an integer: ");
        int userInt = input.nextInt();
        System.out.print("Enter a decimal number: ");
        double userDouble = input.nextDouble();
        
        System.out.println("\nResults:");
        System.out.println("Integer division: " + userInt + " / 3 = " + (userInt / 3));
        System.out.println("Floating division: " + userDouble + " / 3 = " + (userDouble / 3));
        System.out.println("Cast to int: (int) " + userDouble + " = " + (int) userDouble);
        System.out.println("Modulus: " + userInt + " % 3 = " + (userInt % 3));
        
        input.nextLine(); // Consume remaining newline
    }
    
    /**
     * Utility method to format decimal output
     * Shows proper formatting techniques
     */
    public static String formatDecimal(double value, int places) {
        DecimalFormat df = new DecimalFormat("#." + "0".repeat(places));
        return df.format(value);
    }
}