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;
}

2019 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;
}

Quizziz

Even Numbered Conditional Exercises

Problem 2

import java.util.Scanner;
public class Problem2 {
  public static void main(String[] Strings) {
        Scanner input = new Scanner(System.in);
            System.out.print("What is the coefficient A?: ");
            double a = input.nextDouble();
            
            System.out.print("What is the coefficient B?: ");
            double b = input.nextDouble();
            
            System.out.print("What is the coefficient C?: ");
            double c = input.nextDouble();

            double result = b * b - 4.0 * a * c;

            if (result > 0.0) {
                double r1 = (-b + (b * b - 4 * a * c)/(2*a));
                double r2 = (-b - (b * b - 4 * a * c)/(2*a));
                System.out.println("The roots are " + r1 + " and " + r2);
            } else if (result == 0.0) {
                double r1 = -b / (2.0 * a);
                System.out.println("The root is " + r1);
            } else {
                System.out.println("The equation has no real roots.");
            }

    }
}
Exercise2.main(null);
Input a: Input b: Input c: The roots are -0.20871215252208009 and -4.7912878474779195

Problem 4

public class Problem4 {

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

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

        if (number > 0) {
            System.out.println("positive");
        } 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);
number: 25.0
positive

Problem 6

import java.util.Scanner;
public class Problem6 {

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

        System.out.print("Input number: ");
        double x = in.nextDouble();
        System.out.print("Input another number: ");
        double y = in.nextDouble();

        x = Math.round(x * 1000);
        x = x / 1000;

        y = Math.round(y * 1000);
        y = y / 1000;

        if (x == y)
        {
            System.out.println("They are the same up to three decimal places");
        }
        else
        {
            System.out.println("They are different");
        }
    }
}
Problem6.main(null);
Input number: Input another number: They are different

Problem 8

Wasn't sure how to do this one. Had to look at solutions

import java.util.Scanner;
public class Exercise8 {

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

        System.out.print("Input an alphabet: ");
        String input = in.next().toLowerCase();

        boolean uppercase = input.charAt(0) >= 65 && input.charAt(0) <= 90;
        boolean lowercase = input.charAt(0) >= 97 && input.charAt(0) <= 122;
        boolean vowels = input.equals("a") || input.equals("e") || input.equals("i")
                || input.equals("o") || input.equals("u");

        if (input.length() > 1)
        {
            System.out.println("Error. Not a single character.");
        }
        else if (!(uppercase || lowercase))
        {
            System.out.println("Error. Not a letter. Enter uppercase or lowercase letter.");
        }
        else if (vowels)
        {
            System.out.println("Input letter is Vowel");
        }
        else
        {
            System.out.println("Input letter is Consonant");
        }
    }
}
Exercise8.main(null);
Input an alphabet: Input letter is Consonant

Problem 10

public class Problem10 {

	public static void main(String[] args) {
		
		System.out.println("The First 10 Natural Numbers are");
		for(int i = 1; i <= 10; i++) 
		{
			System.out.println(i);
		}
	}
}
Problem10.main(null);
The First 10 Natural Numbers are
1
2
3
4
5
6
7
8
9
10

Problem 12

import  java.util.Scanner;
public class Problem12 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int sum = 0;
        int avg = 0;
        System.out.println("Input the 5 numbers: ");
        for(int i=1; i<=5; i++){
            int num = input.nextInt();
            sum +=  num;
        }
        System.out.println("The sum of the given number is " + sum);
        avg = sum/5;
        System.out.println("The average of the given number is " + avg);

    }
}
Problem12.main(null);
Input the 5 numbers: 
The sum of the given number is 15
The average of the given number is 3

Problem 14

public class Problem14 {

    public static void main(String[] args) {

        int num = 5;
        for(int i = 1; i <= 10; ++i)
        {
            System.out.printf("%d * %d = %d \n", num, i, num * i);
        }
    }
}
Problem14.main(null);
5 * 1 = 5 
5 * 2 = 10 
5 * 3 = 15 
5 * 4 = 20 
5 * 5 = 25 
5 * 6 = 30 
5 * 7 = 35 
5 * 8 = 40 
5 * 9 = 45 
5 * 10 = 50 

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: 4
1
12
123
1234

Problem 18

I didn't know how to do this one so I looked at solutions

import java.util.Scanner;
public class Exercise18 {

  public static void main(String[] args)

{
   		int i,j,n,k=1;

   		System.out.print("Input number of rows : ");

   		Scanner in = new Scanner(System.in);
		    n = in.nextInt();

   		for(i=1;i<=n;i++)
   		{
		for(j=1;j<=i;j++)
	   	System.out.print(k++);
	   	System.out.println("");
	   	}  		
	}
	}
Exercise18.main(null);
Input number of rows : 1
23
456
78910

Problem 20

Didn't know how to do this one either

import java.util.Scanner;
public class Main {
public static void main(String[] args)
 {
   int numberOfRows;
   System.out.print("Input number of rows : ");
   Scanner in = new Scanner(System.in);
		    numberOfRows = in.nextInt();
   int number = 1;
   for (int row = 1; row <= numberOfRows; row++)
    {
   for (int column = 1; column <= row; column++)
     {
       System.out.print(number + " ");
       number++;
     }
     System.out.println();
    }
  }
}
Main.main(null);
Input number of rows : 1 
2 3 
4 5 6