Boolean Expressions

  • all non zero numbers are true
    • the most common example: 1 = true; 0 = false
  • == checks for comparison
    • not to be confused with = which is assignment
  • != checks for inequality
  • <, >, >=, <=
    • these operands do their normal functions
  • || relates to the or function
  • && related to and function

Logic Gates

  • Buffer/YES : output is the same as the input
  • NOT : output is the opposite of the input
  • AND : returns true if both inputs are true
  • NAND : returns false if both inputs are true
  • OR : returns true if at least one of the inputs are true, returns false if both inputs are false
  • NOR : returns false if at least one of the inputs are true, returns true if both inputs are false
  • XOR : returns true if the two inputs are different
  • XNOR : returns true if two inputs are the same

Truth Tables

  • A truth table is a breakdown of a logic function by listing all possible values the function can attain. Such a table typically contains several rows and columns, with the top row representing the logical variables and combinations, in increasing complexity leading up to the final function.

Homework: AP Exam FRQs

2009 FRQ 3B

Write the BatteryCharger method getChargeStartTime that returns the start time that will allow the battery to be charged at minimal cost. If there is more than one possible start time that produces the minimal cost, any of those start times can be returned. For example, using the rate table given at the beginning of the question, the following table shows the resulting minimal costs and optimal starting hour of several possible charges.

// @param chargeTime the number of hours the battery needs to be charged

public int getChargeStartTime(int chargeTime)  // determines start time to charge battery at lowest cost 
{
 int startTime = 0;
 for (int i = 1; i < 24; i++)  // iterate through the differeant start times with a returned values <= 23
 {
 if (this.getChargingCost(i, chargeTime)
 < this.getChargingCost(startTime, chargeTime))
 {
 startTime = i;
 }
 }
 return startTime;
}

2017 FRQ 1B

Write the Digits method isStrictlyIncreasing. The method returns true if the elements of digitList appear in strictly increasing order; otherwise, it returns false. A list is considered strictly increasing if each element after the first is greater than (but not equal to) the preceding element.

public boolean isStrictlyIncreasing(){
    for(int i = 1; i < digitList.size(); i++){  // iterate through the digits in digitList
        if (digitList.get(i-1).compareTo(digitList.get(i)) >= 0){
            return false;
        }
        else{
            return true;
        }
    }
}

2019 FRQ 3B

  • Write the method isBalanced, which returns true when the delimiters are balanced and returns false otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise, they are not balanced.
    1. When traversing the ArrayList from the first element to the last element, there is no point at
    
    which there are more close delimiters than open delimiters at or before that point.
    2. The total number of open delimiters is equal to the total number of close delimiters.
  • Consider a Delimiters object for which openDel is "" and closeDel is "". The examples below show different ArrayList objects that could be returned by calls to getDelimitersList and the value that would be returned by a call to isBalanced.
public boolean isBalanced(ArrayList<String> delimiters) {
    int open = 0;
    int close = 0;
    for (String d: delimiters) {
        if (d.equals(openDel)) {
            open++;
        } else if (d.equals(closeDel)) {
            close++;
        }

        if (close > open) return false; 
    }
    return open == close; 
}