Compound Boolean Expressions

  • test two things to be true before body of conditional is executed
  • '&&' used as logical operator and to join 2 booleans
//ex
//want to play video games, but parents only let you if you've finished your homework and taken a shower

public class compoundBooleans
{
   public static void main(String[] args)
   {
     boolean didHomework = true;
     boolean tookShower = false;
     if (tookShower && didHomework)
     {
         System.out.println("You can play");
     }
     else
     {
         System.out.println("No, you can't play");
     }
   }
}

Truth Tables and De Morgan's Law

  • simplification of booleans through truth tables
  • organized way to define a boolean function
  • De Morgan's law: De Morgan's first law states that the complement of the union of two sets A and B is equal to the intersection of the complement of the sets A and B.

q5 q5

//demorgans law
//has to be back home before 10, and has to walk the dog 

public class demorgans
{
   public static void main(String[] args)
   {
     boolean backby10 = true;
     boolean walkedDog = false;
     if (!(backby10 && walkedDog))
     //means not A OR not B
     {
         System.out.println("True. You were back by 10 but didn't walk the dog");
     }
     else
     {
         System.out.println("You were back in time and walked the dog");
     }}}
public class demorgans
{
   public static void main(String[] args)
   {
     boolean backby10 = true;
     boolean walkedDog = false;

    if (!(backby10 || walkedDog))
//means not A AND not B
{ 
System.out.println("False. You were back by 10 but didn't walk the dog");
}
else
{
 System.out.println("You were back in time and walked the dog");


}
}
}

Comparing Numbers

  • use == or compareTo() method
//comparing numbers
public class wrappers
{
   public static void main(String[] args)
{}}

    int x = 10;
    int y = 14;
    if(x==(y)) {
        System.out.println("Both x and y are equal");
}else {
        System.out.println("x and y are not equal");
}
x and y are not equal
public class wrappers
{
   public static void main(String[] args)
{}}

// int x = 10;
// System.out.println(x.toString()); ------ doesn't work because x is a primitive data type, not a modifiable object
Integer x = new Integer(10);
//to get around this, you define an integer wrapper class
System.out.println(x.compareTo(15));
//x is now an object, not a primitive data type
System.out.println(x.compareTo(5));
//-1 is b/c 10 is less than 15
//1 is because 5 is less than 10
-1
1
int xy = 83;
int yz = 20;

int compare = Integer.compare(xy, yz);

if(compare>0) {
    System.out.println("xy is greater than yz");
}else if (compare<0){
    System.out.println("xy is less than yz");
}else{
     System.out.println("xy and yz are equal");
}
x is greater than y

Comparing Strings

  • use .equals() or ==
  • equals() used to check if 2 strings contain the same content
class comparingStrings{ 

    public static void main(String args[]){  
    }  
   }  
   String s1="hello";  
   String s2="goodbye";  
   String s3=new String("hello");  
   String s4="you're a good friend"; 
   String s5="goodbye";

   if (s2 == s5){
    System.out.println("true");
   }
   System.out.println(s1.equals(s2));  
   System.out.println(s1.equals(s3));  
   System.out.println(s1.equals(s4));
true
false
true
false

HOMEWORK

//UNIT 3
//2009 3b
public int getChargeStartTime(int chargeTime)
{
  int lowestPrice = 0;
  for(int i = 1; i < rateTable.length; i++)
    if(getChargingCost(startHour, chargeTime) <
          getChargingCost(i, chargeTime))
      i = startHour;
  return i;
}
//2017 1b
    public boolean isStrictlyIncreasing() {
        boolean increase = true;
        for (int i = 0; i < digitList.size() - 1; i++) {
            if (digitList.get(i).intValue() >= digitList.get(i + 1).intValue()) {
                increase = false;
        return true;
    }
//2019 3b
public boolean isBalanced(ArrayList<String> delimiters) {
    int openDels = 0;
    int closeDels = 0;
    for (String del : delimiters) {
        if (del.equals(openDels)) {
            openDels++;
        } else {
            closeDels++;
        if (closeDels > openDels) {
            return false;
        }
    }
    return openDels == closeDels;
}