3.1 Boolean Expressions

  • Represent logical and tell whether something is true or false
  • =, !, <, >, >= are different operators

3.2 If Statements and Control Flow

  • Perform computation depending on whether a Boolean conditions is true or false
  • If Statements: If the statement is true, it will return true; otherwise false

3.3 If-Else Statements

  • Run a block of code if there is more than one alternative

3.4 Else-If Statements

  • More conditions
  • If the first condition is proven false, the other conditions will be evaluated

3.5 Compound Boolean Statements

  • Nested if statements: if-statements within if-statement -- allows more flexibility to run the code; if outer if-statement is false, internal if-statement won't be evaluated
  • Logical operators: && (and) ll(or) and !(not)
  • Short circulated evaluation: the result of a compound Boolean expression can be determined just by looking at a few expressions

3.6 Compound Boolean Expressions

  • De Morgan's laws: simplifies Boolean expressions
  • !(a&7B) = (!a ll !b)
  • !(a ll b) = (!a && !b)

3.7 Comparing Objects

  • Use '==' to see if two objects are referring to aliases for the same object
  • Use '.equals()' to see if the attributes of two objects are the same
  • Ex: vehicle car = new Vehicle("blue," 4); vehicle car2 = new Vehicle ("blue," 4) --> Vehicle blue car = car
  • Both the vehicles have the same attributes
boolean cloudy = true;
boolean rainy = false;

if (!cloudy && !rainy) {
    System.out.println("Don't forget to bring a hat!");
}

2019 FRQ 1

Part A

public static int numberOfLeapYears(int year1, int year2)
{
    int count = 0;
    for (int y = year1; y <= year2; y++) {
    if (isLeapYear(y))
    {
    count++; }
    }
    return count;
}

Part B

public static int dayOfWeek(int month, int day, int year)
{
 int startDay = firstDayOfYear(year);
 int nthDay = dayOfYear(month, day, year);
 int returnDay = (startDay + nthDay - 1) % 7;
 return returnDay;
}

Problem 2:

import java.util.Scanner;
public class Problem2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("coeff a: ");
        double a = input.nextDouble();
        System.out.println("coeff b: ");
        double b = input.nextDouble();
        System.out.println("coeff c: ");
        double c = input.nextDouble();

        double root1 = (-b + (b * b - 4 * a * c)/(2*a));
        double root2 = (-b - (b * b - 4 * a * c)/(2*a));
        System.out.println("The roots are " + root1 + " and " + root2);
        }
    }

Problem2.main(null);
coeff a: 
coeff b: 
coeff c: 
The roots are -8.75 and 2.75

Problem 4:

public class Problem4 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("num: ");
        double number = input.nextDouble();
        System.out.println(number);

        if (number > 0) {
            System.out.println("pos");
        } else if (number == 0) {
            System.out.println("zero");
        } else {
            System.out.println("neg");
        }

        if (number < 1) {
            System.out.println("neg");
        } else if (number > 1000000) {
            System.out.println("too big ");
        }

    }
}

Problem4.main(null);
num: 4.0
pos

Problem 6:

public class Problem6 {

    static double truncate(double n, int decimalPlace) {
        n = n*Math.pow(10, decimalPlace);
        n = Math.floor(n);
        n = n/Math.pow(10, decimalPlace);

        return n;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("num 1: ");
        double number1 = input.nextDouble();
        double num1Trun = truncate(number1, 3);
        System.out.println(number1);

        System.out.print("num 2: ");
        double number2 = input.nextDouble();
        double num2Trun = truncate(number2, 3);
        System.out.println(number2);

        if (num1Trun == num2Trun) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}

Problem6.main(null);
num 1: 2.0
num 2: 3.0
false

Problem 8:

public class Problem8 {

    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("enter letter: ");
        String letter = input.nextLine();
        System.out.println(letter);

        String[] vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"};

        if(letter.length() > 1) {
            System.out.println("not one letter");
        } else if (!letter.matches("[a-zA-Z]+")) {
            System.out.println("not a letter");
        } else if (vowels.contains(letter)) {
            System.out.println("vowel");
        } else {
            System.out.println("consonant");
        }


        input.close();

        
    }
}

Problem8.main(null);
|           } else if (vowels.contains(letter)) {
cannot find symbol
  symbol:   method contains(java.lang.String)

Problem 10:

public class Problem10 {

    public static void main(String[] args) {

    int[] n = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int num : n) {
            System.out.println(num);
        }
    }
}

Problem10.main(null);
1
2
3
4
5
6
7
8
9
10

Problem 12:

public class Problem12 {

    
    public static void main(String[] args) {
        int sum = 0;
        double average;

        System.out.println("Input nums:");
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            int num = input.nextInt();
            System.out.println(num);
            sum += num;
        }
        average = sum/5;

        System.out.println("sum: " + sum);
        System.out.println("avg: " + average);
        
    }
}

Problem12.main(null);
Input nums:
1
2
3
4
5
sum: 15
avg: 3.0

Problem 14:

public class Problem14 {

    public static void main(String[] args) {

        System.out.println("num: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();

        System.out.println("terms: ");
        int terms = input.nextInt();

        for (int i = 0; i <= terms; i++) {
            System.out.println(num + " x " + i + " = " + num*i);
        }        
    }
}

Problem14.main(null);
num: 
terms: 
1 x 0 = 0
1 x 1 = 1
1 x 2 = 2

Problem 16:

public class Problem16 {

    public static void main(String[] args) {
        
        System.out.print("rows: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println(num);

        for (int i = 1; i <= num; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Problem16.main(null);
rows: 3
1
12
123

Problem 18:

public class Problem18 {

    
    public static void main(String[] args) {
        

        System.out.print("rows: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println(num);

        int count = 1;

        for (int i = 1; i <= num; i++) {
            
            for (int j = 1; j <= i; j++) {
                System.out.print(count + "\t");
                count++;
            }
            System.out.println();
        }
    }
}

Problem18.main(null);
rows: 3
1	
2	3	
4	5	6	

Problem 20:

public class Problem20 {

    
    public static void main(String[] args) {
        

        System.out.print("rows: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println(num);

        int count = 1;

        for (int i = 1; i <= num; i++) {
            
            for (int j = 1; j <= i; j++) {
                System.out.print(count + "\t");
                count++;
            }
            System.out.println();
        }
    }
}

Problem20.main(null);
rows: 3
1	
2	3	
4	5	6