AP Computer Science A Unit 3 Review

AP Computer Science A Unit 3 Review with Java Code…

1. Boolean Expressions

public class BooleanExpressionsExample {
    public static void main(String[] args) {
        // Boolean expressions
        boolean isTrue = true;
        boolean isFalse = false;
        
        // Logical operators
        boolean result1 = (5 > 3);      // true
        boolean result2 = (10 <= 5);    // false
        boolean result3 = (isTrue && isFalse); // false
        boolean result4 = (isTrue || isFalse); // true
        boolean result5 = !(5 == 5);    // false
    }
}

2. if Statements and Control Flow

public class IfStatementExample {
    public static void main(String[] args) {
        int x = 10;
        
        // if statement
        if (x > 5) {
            System.out.println("x is greater than 5");
        }
        
        System.out.println("End of program");
    }
}

3. if-else Statements

public class IfElseStatementExample {
    public static void main(String[] args) {
        int x = 3;
        
        // if-else statement
        if (x % 2 == 0) {
            System.out.println("x is even");
        } else {
            System.out.println("x is odd");
        }
    }
}

4. else if Statements

public class ElseIfStatementExample {
    public static void main(String[] args) {
        int x = 10;
        
        // else if statement
        if (x < 0) {
            System.out.println("x is negative");
        } else if (x > 0) {
            System.out.println("x is positive");
        } else {
            System.out.println("x is zero");
        }
    }
}

5. Compound Boolean Expressions

public class CompoundBooleanExpressionsExample {
    public static void main(String[] args) {
        int age = 25;
        boolean isStudent = true;
        
        // Compound boolean expressions
        if (age >= 18 && isStudent) {
            System.out.println("You are an adult student");
        }
    }
}

6. Equivalent Boolean Expressions

public class EquivalentBooleanExpressionsExample {
    public static void main(String[] args) {
        int x = 10;
        
        // Equivalent boolean expressions
        boolean result1 = (x > 5);   // true
        boolean result2 = !(x <= 5); // true
        
        // Comparing results
        System.out.println(result1 == result2); // true
    }
}

7. Comparing Objects

public class ComparingObjectsExample {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = new String("Hello");
        
        // Comparing string objects
        System.out.println(str1.equals(str2)); // true
        System.out.println(str1.equals(str3)); // true
    }
}