teaching machines

CS 145 Lecture 15 – Conditional Statements

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

Agenda

Code

Sidesteps.java

package lecture1006;

public class Sidesteps {

  public static void main(String[] args) {
    System.out.println(abs(5));
    System.out.println(abs(-5));
    System.out.println(sanitize("johnch"));
    System.out.println(sanitize("cookiemonster"));
    System.out.println(sanitize("no@uwec.edu"));
    System.out.println(sanitize("no@uwec"));

    System.out.println(getStatus(0));
    System.out.println(getStatus(1));
    System.out.println(getStatus(2));
  }

  public static String getStatus(int nEmails) {
    String status = "You've got " + nEmails + " new message";
    // if (nEmails > 1 || nEmails < 1) {
    if (nEmails != 1) {
      status += "s";
    }
    status += ".";
    return status;
  }

  public static double abs(double x) {
    if (x < 0.0) {
      x = -x;
    }
    return x;
  }

  public static String sanitize(String email) {
    if (!email.endsWith("@uwec.edu")) {
      // email = email + "@uwec.edu";
      email += "@uwec.edu";
    }
    return email;
  }

}

Bifurcations.java

package lecture1006;

import java.util.Random;

public class Bifurcations {

  public static void main(String[] args) {
    Random g = new Random();

    String color;
    if (g.nextBoolean()) {
      color = "blue";
    } else {
      color = "green";
    }

    System.out.println("<html>");
    System.out.println("<body style=\"background-color: " + color + "\">");
    System.out.println("</body>");
    System.out.println("</html>");
  }

}

BirthdayDay.java

package lecture1006;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Scanner;

public class BirthdayDay {
  public static void main(String[] args) {
    System.out.print("What year are you interested in? ");
    Scanner in = new Scanner(System.in);
    int year = in.nextInt();
    
    GregorianCalendar cal = new GregorianCalendar(year, 1, 29);
    int weekdayNumber = cal.get(Calendar.DAY_OF_WEEK);
    
    System.out.println(cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG_FORMAT, Locale.getDefault()));
  }
}

Haiku

on how it feels sometimes:
nPetals % 2
If 0, then she loves me
If 1, I love her