Unit 3 Boolean Expressions and if Statements
Unit 3 Boolean Expressions and if Statements
public int getChargeStartTime (int chargeTime) {
int startHour = 0;
int lowPrice = 500_000;
for (int x = 0; x < 23; x++) {
if (lowPrice > getChargingCost(x, chargeTime)) {
lowPrice = getChargingCost(x, chargeTime);
startHour = x;
}
}
return startHour;
}
public boolean isStrictlyIncreasing () {
boolean increasing = true;
for (int i = 1; i < digitListSize(); i++) {
if (digitList.get(i-1) >= digitList.get(i)) {
increasing = false;
}
}
return increasing;
}
public boolean isBalanced(ArrayList<String> delimiters) {
int numOpen = 0, numClosed = 0;
for(String d : delimiters) {
if(d.equals(openDel))
numOpen++;
if(d.equals(closeDel))
numClosed++;
if(numClosed > numOpen)
return false;
}
return numOpen == numClosed;
}
Compund Boolean Expression: Combinations of Boolean operators result in the creation of compund boolean operators, which include the &&, ||, or ! operators. (And, Or, Not, respectively)
boolean IsDecember = true;
boolean IsNovember= false;
boolean compound = !(IsDecember && IsNovember) && (IsNovember || IsDecember);
System.out.println(compound);
DeMorgan's Law: Helps with the simplification and abstraction of boolean expressions.
boolean first = true;
boolean second = false;
// complicated boolean expression
boolean res1 = !((!(first && second)) || (!(first || second)));
// simplified using De Morgan's Law once
boolean res2 = !((!first || !second) || (!first && !second));
System.out.println(res1 + " " + res2 + " ");
Truth Tables: Reveal the actual values ot boolean expressions, 0 is false, 1 is true
0 | 0 | 1 | 1 | 0 | 1 |