teaching machines

CS 1: Lecture 20 – Loops, Part 2

Dear students,

Three big ideas will consume the rest of our semester: loops, arrays, and objects. Nothing we’ve discussed so far will go away. We will be spending our days seeing applications of these ideas. Like today. Our entire time will be spent writing some applications that use loops.

  • 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 numbers the lines in a file. Like so:
    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 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 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 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 hijacks the mouse and spins it in a circle—or some other interesting shape.
  • Write code that generates all possible two-byte Unicode characters. There are 65536 of them.

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

  • Homework 4 has been posted. It is due before November 8. Homeworks 4, 6, and 7 are the full homeworks, each worth 10 Blugolds. The SpecChecker is 90% complete and will be posted before Friday. But the PDF is ready to go. You should start today—it feels far better than starting later.
  • Read sections 2.3, 2.4, 5.1, and 5.2. On a quarter sheet, write down 2-3 questions and observations from your reading or examples of loops that you have observed in your life.

See you next class!

Sincerely,

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

There’s a bar downtown
The Loophole, where one loop stops
Here, have another

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

WordCounter.java

package lecture1025;

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

import javax.swing.JFileChooser;

public class WordCounter {
  public static void main(String[] args) throws FileNotFoundException {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File file = chooser.getSelectedFile();
    
//    File file = new File("/Users/johnch/Desktop/foo.txt");
    Scanner in = new Scanner(file);
    
    int nWords = 0;
    while (in.hasNext()) {
      in.next();
      nWords++;
    }
    
    System.out.println(nWords);
    
    in.close();
  }
}

Blugold.java

package lecture1025;

public class Blugold {
  public static void main(String[] args) {
    for (int i = 1; i <= 100; i++) {
//      if (i % 15 == 0) {
//      if (i % 3 == 0 && i % 5 == 0) {
//        System.out.println("blugold");
//      } else if (i % 3 == 0) {
//        System.out.println("blu");
//      } else if (i % 5 == 0) {
//        System.out.println("gold");
//      } else {
//        System.out.println(i);
//      }
      
      if (i % 3 == 0) {
        System.out.print("blu");
      }
      
      if (i % 5 == 0) {
        System.out.print("gold");
      }
      
      if (i % 3 != 0 && i % 5 != 0) {
        System.out.print(i);
      }
      
      System.out.println();
    }
  }
}

Circler.java

package lecture1025;

import java.awt.AWTException;
import java.awt.Robot;

public class Circler {
  public static void main(String[] args) throws AWTException {
    Robot bot = new Robot();
    int n = 3000;
    double radius = 100;
    double theta = 2 * Math.PI / n;
    for (int i = 0; i < n; i++) {
      int x = (int) (radius * Math.cos(i * theta));
      int y = (int) (radius * Math.sin(i * theta));
      bot.mouseMove(x + 1000, y + 500);
      bot.delay(100);
    }
  }
}

Comments

Leave a Reply

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