Unit 3 - Boolean Expressions and If Statements
Notes
- Boolean - logical statement that can be evaluated to the values true/false
- EX:
if (true) { System.out.println("True code block"); }
- LOGICAL OPERATORS
- && - and
- || - or
- ! - not
- Parenthesis matter
- De Morgan's Law
- (A ∪ B)' = A' ∩ B'
- "The complement of the union of the two sets A and B will be equal to the intersection of A' (complement of A) and B' (complement of B)."
- EX:
!((false || !true) || (false && true))
// assume that getChargingCost() is implemented
public class BatteryCharger
{
public int getChargingStartTime (int chargeTime)
{
int start = 0;
for (int loop = 0; loop < 25; loop++)
{
if (this.getChargingCost(loop, chargeTime) < this.getChargingCost(start, chargeTime))
{
start = loop;
}
}
return start;
}
}
BatteryCharger batteryCharger = new BatteryCharger();
batteryCharger.getChargeStartTime(6);
import java.util.*;
public class Digits
{
private ArrayList<Integer> digitList;
public Digits(int num)
{
digitList = new ArrayList<Integer>();
if (num==0)
{
digitList.add(new Integer(0));
}
while (num > 0)
{
digitList.add(0, new Integer(num % 10));
num /= 10;
}
}
public boolean isStrictlyIncreasing()
{
boolean increase = true;
for (int loop = 0; loop < digitList.size() - 1; loop++)
{
if (digitList.get(loop).intValue() >= digitList.get(loop + 1).intValue())
{
increase = false;
return increase;
}
}
return increase;
}
}
boolean digits1 = new Digits(1356).isStrictlyIncreasing();
boolean digits2 = new Digits(1536).isStrictlyIncreasing();
System.out.printf("%b\n%b", digits1, digits2);
public class Delimiters
{
private String openDel;
private String closeDel;
public boolean isBalanced(ArrayList<String> delimiters)
{
int opencount = 0;
int closecount = 0;
for (int loop = 0; loop < delimiters.size(); loop++)
{
if (delimiters.get(loop) == openDel)
{
opencount++;
}
else if (delimiter.get(loop) == closeDel)
{
closecount++;
}
if (closecount > opencount)
{
return false;
}
}
return true;
}
}