teaching machines

CS 1: Lecture 21 – Loops, Part 3

Dear students,

We explore more examples of loops today. Because we are, in fact, stuck in a loop. There’s no way out of this.

  • 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
  • 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 code that generates all possible two-byte Unicode characters. There are 65536 of them.
  • 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.

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

  • The homework 4 SpecChecker has been posted. Run Team / Pull… from template today to bring it into your repository. Solving configuration issues is part of completing a homework.

See you next class!

Sincerely,

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

Sorry I’m so late
My self-driving car got stuck
In a roundabout

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

Spacer.java

package lecture1027;

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

public class Spacer {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int nFailures = 0;
    File file;
    do {
      System.out.print("Enter a filename: ");
      String name = in.nextLine();
      file = new File(name);
      if (!file.exists()) {
        ++nFailures;
      }
    } while (!file.exists() && nFailures < 3);

    System.out.println(nFailures);
    if (nFailures == 3) {
      System.err.println("You shall not pass.");
    } else {
      System.out.println(file.length() / 1024.0 / 1024);
    }
  }
}

Spelunking.java

package lecture1027;

import java.util.Random;

public class Spelunking {
  public static void main(String[] args) {
    Random g = new Random();
    int n = g.nextInt(100);
    for (int i = 0; i < n; ++i) {
      System.out.println(getRandomLine(g));
    }
  }

  public static String getRandomLine(Random g) {
    int n = g.nextInt(15) + 1;
    String line = "";
    for (int i = 0; i < n; ++i) {
      line += "*";
    }
    return line;
  }
}

Unicode.java

package lecture1027;

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

Factor.java

package lecture1027;

public class Factor {
  public static void main(String[] args) {
    int max = 0;
    int winnerSoFar = 0;
    for (int i = 1; i <= 1000; ++i) {
      int count = factorCount(i);
//      max = Math.max(count, max);
      if (count > max) {
        System.out.println(i + " -> " + count);
        max = count;
        winnerSoFar = i;
      }
    }
    System.out.println(winnerSoFar);
  }

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

Comments

Leave a Reply

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