teaching machines

CS 145 Lecture 20 – For Loops

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

Agenda

TODO

Program This 1

Program This 2

Code

Alphabet.java

package lecture1017;

public class Alphabet {
  public static void main(String[] args) {
    for (char c = 'a'; c <= 'z'; ++c) {
      System.out.print(c);
    }
  }
}

Spreadsheet.java

package lecture1017;

public class Spreadsheet {
  public static void main(String[] args) {
    fillDown(1, 3, 10);
  }
  
  public static void fillDown(int first,
                              int second,
                              int times) {
    int difference = second - first;
    
    System.out.println(first);
//    System.out.println(second + 0 * difference);
//    System.out.println(second + 1 * difference);
//    System.out.println(second + 2 * difference);
//    System.out.println(second + 3 * difference);
//    System.out.println(second + 4 * difference);

    for (int i = 0; i <= times - 2; ++i) {
      System.out.println(second + difference * i);
    }

  }
}

StringUtilities.java

package lecture1017;

public class StringUtilities {
  public static void main(String[] args) {
    System.out.println(indexOf("Lincoln, Abraham", '!'));
  }
  
  public static int indexOf(String s, char c) {
    for (int i = 0; i < s.length(); ++i) {
      if (s.charAt(i) == c) {
        return i;
      }
    }
    return -1;
  }
}

Grid.java

package lecture1017;

public class Grid {
  public static void main(String[] args) {
    for (int r = 1; r <= 8; ++r) {
      for (int c = 1; c <= 8; ++c) {
        System.out.printf("%3d", c * r);
      }
      System.out.println();
    }

//    for (int c = 1; c <= 8; ++c) {
//      System.out.printf("%3d", c * 2);
//    }
//    System.out.println();
//
//    for (int c = 1; c <= 8; ++c) {
//      System.out.printf("%3d", c * 3);
//    }
//    System.out.println();

  }
}

Haiku

from a geek on the road to his love:
It’s a whirlwind tour
But i can’t wait to get home
He wants to see u