teaching machines

CS1: Lecture 25 – Loops Continued

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:

  • Write code that generates all possible two-byte Unicode characters. There are 65536 of them.
  • Write a program that prints the numbers from 1 to 100. But for multiples of three, print blu instead of the number, and for multiples of five, print gold. For numbers that are multiples of both three and five print blugold. Read more about this problem at Why Can’t Programmers.. Program?
  • Write a method that accepts an int and returns the number of factors that evenly divide that number. Do not count 1 or the number itself. Use that method to find the number in [0, 1000] that has the most factors.
  • Write a program that generates a random spelunking workout display. For example:
    **********
    *******
    *********
    ****
    ******
    *******
  • Write a program that presents a text-based spinner that cycles through -, /, |, and \. Print each new “frame” of the animation over the previous frame. Use a carriage return (\r) in your output to force the cursor back to the left margin.
  • Write a program that counts the words in a file. Use a Scanner, but feed it a File instead of System.in.
  • Write a program that prompts a user for a file. Report how many bytes it occupies on disk. If a bad file is entered, repeat the prompt until the user gets it right. Follow this general repeat until valid pattern:
    prompt
    x = get user input
    while x is not valid
      prompt
      x = get user input
    
    // if we get here, the input must be valid
    process x

TODO

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

  • CS 148, your lab is posted. Feel free to start early.
  • We will hold homework 3 peer reviews during lab this week.

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);
    }
  }
}

Comments

Leave a Reply

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