teaching machines

CS1: Lecture 25 – Loops Continued

October 30, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

You know that trope in movies where someone invents a new technology? At first it seems to be doing going good, but then it starts to get out of control. That’s where we are in the semester. Our software up until this point was very mild and had a limited reach. Now we have loops, and you probably feel like humanity is on the brink of destruction.

What we need is practice. Today we’ll keep using loops to solve these problems:

TODO

Here’s your TODO list to complete before we meet again:

See you next class!

Sincerely,

P.S. It’s time for a haiku!

Are we in a loop?
I’ve felt that way for a while
i += 1

P.P.S. Here’s the code we wrote together in class…

Factors.java

package lecture1030.cs145;

public class Factors {
  public static void main(String[] args) {
    for (int n = 1; n <= 1000; n++) {
      System.out.printf("%5d -> %d%n", n, getFactorCount(n));
    }
  }

  public static int getFactorCount(int n) {
    int nFactors = 0;
    for (int i = 1; i <= n; i++) {
      if (n % i == 0) {
        nFactors += 1;
      }
    }
    return nFactors;
  }
}

Loops.java

package lecture1030.cs145;

public class Loops {
  public static void main(String[] args) {
    for (int i = 1; i <= 100; i++) {
      if (i % 15 == 0) {
        System.out.printf("%3d - blugold%n", i);
      } else if (i % 3 == 0) {
        System.out.printf("%3d - blu%n", i);
      } else if (i % 5 == 0) {
        System.out.printf("%3d - gold%n", i);
      } else {
        System.out.printf("%3d%n", i);
      }
    }

//    for (int i = 0; i < 65536; i++) {
//      System.out.print((char) i);
//      if (i % 60 == 0) {
//        System.out.println();
//      }
//    }
  }
}

Workout.java

package lecture1030.cs145;

import java.util.Random;

public class Workout {
  public static void main(String[] args) {
    Random generator = new Random();
    for (int i = 0; i < 7; i++) {
      int nStars = generator.nextInt(8) + 3;
      System.out.println("*".repeat(nStars));
    }
  }
}

Anycode.java

package lecture1030.cs148;

public class Anycode {
  // 
  public static void main(String[] args) {
    for (int i = 0; i < 65536; i++) {
      if (i % 50 == 0) {
        System.out.println();
      }
      System.out.print((char) i);
    }
  }
}

Blugold.java

package lecture1030.cs148;

public class Blugold {
  public static void main(String[] args) {
    for (int i = 1; i <= 100; i++) {
      if (i % 3 == 0 && i % 5 == 0) {
        System.out.printf("%3d - blugold%n", i);
      } else if (i % 5 == 0) {
        System.out.printf("%3d - gold%n", i);
      } else if (i % 3 == 0) {
        System.out.printf("%3d - blu%n", i);
      } else {
        System.out.printf("%3d%n", i);
      }
    }
  }
}

Factors.java

package lecture1030.cs148;

public class Factors {
  public static void main(String[] args) {
    for (int i = 1; i < 1000; ++i) {
      System.out.printf("%5d - %d%n", i, getFactorCount(i));
    }
  }

  public static int getFactorCount(int n) {
    int count = 0;
    for (int i = 1; i <= n; i++) {
      if (n % i == 0) {
        count++;
      }
    }
    return count;
  }
}

Spinner.java

package lecture1030.cs148;

public class Spinner {
  public static void main(String[] args) throws InterruptedException {
    String frames = "-\\|/";
    int i = 0;
    while (true) {
      System.out.printf("\r%c", frames.charAt(i));
      i = (i + 1) % frames.length();
      Thread.sleep(100);
    }
  }
}