teaching machines

CS1: Lecture 23 – While Loops Continued

Dear students,

Loops turn us into pattern hunters. Behind some complex output is a much simpler machine using repetition. Let’s keep building those repetitive machines today. We’ll keep exploring while loops.

While

Last time we introduced while. Let’s see it at work in a few more programs.

  • Write a program that starts with a number and repeatedly alters it. If the number is even, it halves the number. If odd, it triples the number and adds 1. Print out each intermediate result. Stop altering the number when it reaches 1. The belief that we can start at any number and eventually reach 1 is the Collatz conjecture.
  • Write a program that numbers the lines of a file:
    1: I once met a man who was miffed
    2: 'Tween email and him was a rift
    3: When he pressed the @ key
    4: Number 2 it would be
    5: So I told him press it with Shift
  • Write a program to generate the vertices of an n-sided polygon.

TODO

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

  • Keep at homework 3, which I’ll grade tomorrow morning at an indeterminate time. Remember to commit and push after every coding session! Resolving Git and SpecChecker issues are part of completing the homework. Leave yourself enough time to deal with trouble. Add a comment with test hw3 to any commit on GitLab to run the remote SpecChecker and make sure your committing and pushing succeeded.

See you next class!

Sincerely,

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

Apocalypse now?
Not according to my count
We’ve got n times yet

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

Circlererererer.java

package lecture1025.cs145;

public class Circlererererer {
  public static void main(String[] args) {
    int n = 57;
    double delta = 360.0 / n;

    int i = 0;
    while (i < n) {
      double angle = i * delta;
      System.out.printf("(%f,%f)%n", Math.cos(Math.toRadians(angle)), Math.sin(Math.toRadians(angle)));
      i++;
    }
  }
}

Collatz.java

package lecture1025.cs145;

import java.util.Random;

public class Collatz {
  public static void main(String[] args) {
    Random generator = new Random();
    int number = generator.nextInt(100000000);
    System.out.println(number);
    while (number != 1) {
//      System.out.println(number);
      if (number % 2 == 0) {
        number /= 2;
      } else {
        number = 3 * number + 1;
      }
      System.out.println(number);
    }
  }
}

LineNumberererererer.java

package lecture1025.cs145;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LineNumberererererer {
  public static void main(String[] args) throws FileNotFoundException {
    File file = new File("/Users/johnch/Desktop/limerick.txt");
    Scanner in = new Scanner(file);
    int count = 1;
    while (in.hasNextLine()) {
      System.out.printf("%d: %s%n", count, in.nextLine());
      count++;
    }
    in.close();
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *