teaching machines

CS 145 Lecture 17 – Pigeon-holing with Conditional Statements

October 10, 2014 by . Filed under cs145, fall 2014, lectures.

Agenda

TODO

Revisiting Precedence of && and ||

Is there a combination of values for a, b, and c that produces different values for the two expressions  (a && b) || c and  a && (b || c)?

Enumerating a truthtable helps.

What Does This Do?

  1. show
  2. show
  3. show
  4. show

Program This

show show

Code

Precedence.java

package lecture1010;

public class Precedence {
  public static void main(String[] args) {
    System.out.println(true && true || false && false);
  }
}

Pad.java

package lecture1010;

public class Pad {
  public static void main(String[] args) {
    
  }
  
  public static String padTo4(int n) {
    if (n < 10) {
      return "000" + n;
    } else if (n < 100) {
      return "00" + n;
    } else if (n < 1000) {
      return "0" + n;
    } else {
      return "" + n;
    }
  }
}

Randaurant.java

package lecture1010;

import java.util.Random;

public class Randaurant {
  public static void main(String[] args) {
    System.out.println(getRandomRestaurant());
  }

  public static String getRandomRestaurant() {
    Random g = new Random();
    int i = g.nextInt(100);
    
    if (i < 3) {
      return "Cancun";
    } else if (i < 78) {
      return "Kwik Trip";
    } else if (i < 90) {
      return "Holiday";
    } else {
      return "McDonalds";
    }
    
  }
}

Haiku

show