Unit 3

Boolean Expressions and Operators

  • Operators used to create boolean expressions
  • == equals o (two primitive types have the same value)
  • "!= checks for inequality (not equal)
  • "<" less than
  • "<=" less than or equal to
  • ">" greater than
  • ">=" greater than or equal to
  • Use boolean statements in conjunction with other operators as well, such as in the following statement: (a%2) == 0
  • Thematic idea: you are buying a car and you want to compare prices. you can idealistically have a code that allows you to input different car prices and compares them using these boolean operators and then return true and false

Conditional Statements

  • If statements: occurs if a block of code is only run if the condition is true. Here is the anatomy of an if statement
  • Thematic idea: use conditional statements like and if statement if there are certain restrictions someone wants on a car
  • If else statements: We have just covered if statements, but what if we want something to be done if the condition that we are checking is false? This is where the else statement comes into play. The header for the else does not have a condition and is just "else" while the body is what is performed if the condition for the if statement is false.
  • Rounding numbers
  • Else if statements: The body of a certain else if statement only runs if all the previous conditions were false and the condition of that particular else if statement is true.
  • Example: seeing if a number is divisible or leap year

Compound Boolean

  • Nested conditionals
  • ! (NOT) negates whatever is in front of it
  • && (AND) returns true if BOTH the statement directly preceding it and the statement following it are true
  • || (OR) returns true if at least one of the statements directly preceding or following it is true
  • You can use compound operators to create compound boolean expressions

Comparing Object

  • check if two objects have the same characteristics, such as two different String objects saying the same text
  • the equals operator for most classes returns true if their attributes are the same, even though they aren't the same object (like identical twins!).

Unit 4

While Loops

  • loop keeps running while condition is true
  • Condition is a boolean statement and the loop body is a statement that is repeated while the condition is true. Once the condition is no longer true, the loop is exited
  • Problems: infinite loop or fully false loop
  • Structure: int i = 0; while (i < numberOfRepetitions) { do something i++; }
  • integer i as an increment and increases by 1 every time the loop is run
  • break statements to exit a loop immediately
  • continue statements for jumping loops
  • try catch statements for exceptions ## For Loops
  • used when the number of repetitions is fixed
  • for loop header includes: variable initialization, a conditional expression, and an incrementer
  • The initialization is the int i = 0, the conditional expression is the i < numberOfTimesToRepeat, and the incrementer is the i++.

Algorithms using Strings

  • Reversing a string = get character and make it into a string using a for loop
  • Get all substrings with a certain length "n" ## Nested Iteration
  • Loop inside a loop
  • If there are two nested for loops without break statements, and the outer loop runs n times, and the inner loop runs m times for every iteration of the outer loop, then the inner loop will run m*n times. This can be extended to situations where there are more than 2 nested loops. The total number of times the innermost loop is run is the product of the number of times that each loop runs per iteration.
boolean isEngineRunning = true;
int speed = 60;
boolean isSpeedAboveThreshold = speed > 50;

if (isEngineRunning && isSpeedAboveThreshold) {
    System.out.println("The car is moving fast!");
} else {
    System.out.println("The car is not moving fast.");
}
boolean isEngineRunning = true;
int speed = 60;
boolean isSpeedAboveThreshold = speed > 50;
boolean isEmergencyBrakeApplied = false;

// compound boolean
if (isEngineRunning && (isSpeedAboveThreshold || isEmergencyBrakeApplied)) {
    System.out.println("The car is in an emergency situation!");
} else {
    System.out.println("The car is in a normal situation.");
}
boolean isEngineRunning = true;
if (isEngineRunning) {
    System.out.println("The engine is running");
} else {
    System.out.println("The car's engine is not running.")
}
int speed = 80;
int speedLimit = 60;

if (speed > speedLimit) {
    System.out.println("Warning: you are above the speed limit!");
} else {
    System.out.println("You are below the speed limit");

}
String weather = "rainy";
int visibility = 30;

if (weather.equals("sunny")) {
    System.out.println("Driving in normal mode.");
} else if (weather.equals("rainy") && visibility < 50) {
    System.out.println("Driving in slow mode due to low visibility.");
} else if (weather.equals("snowy")) {
    System.out.println("Driving in snow mode.");
} else {
    System.out.println("Driving in normal mode.");
}
String carMake = "Toyota";
String carModel = "Camry";

if (carMake.equals("Toyota") && carModel == "Camry") {
    System.out.println("The car is a Toyota Camry.");
} else {
    System.out.println("The car is not a Toyota Camry.");
}
Car car1 = new Car("Toyota", "Camry");
Car car2 = new Car("Toyota", "Camry");

if (car1 == car2) {
    System.out.println("car1 and car2 are the same object.");
} else {
    System.out.println("car1 and car2 are different objects.");
}
public static void main(String[] args) {
    // Create two Car objects
    Car car1 = new Car("Toyota", "Camry", 2020);
    Car car2 = new Car("Toyota", "Camry", 2020);

    // Compare using equals() method
    if (car1.equals(car2)) {
        System.out.println("car1 is equal to car2");
    } else {
        System.out.println("car1 is not equal to car2");
    }
}