teaching machines

CS 145 Lecture 13 – PrintWriter and Patterns

March 14, 2012 by . Filed under cs145, lectures, spring 2012.

Agenda

So-far (Cumulative) pattern

We can only work on one thing at a time. When problems require processing of a set or list, we’ll have to apply the so-far pattern:

init soFar
for each item
  update soFar based on item

Fencepost pattern

Suppose we want to print “XOXOXO…X”. A look can’t do this, but a loop-and-a-half can:

do half-step
for some number of steps
  do full step

Code

FileWriter.java

package preexam2;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;

public class FileWriter {
  public static void main(String[] args) throws FileNotFoundException {
    
    PrintWriter out = new PrintWriter("/home/user/Desktop/snowpeople.html");

    out.println("<html>");
    out.println("<body>");
    out.println("<h1>My first SVG</h1>");
    out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");

    Random generator = new Random();

    for (int i = 0; i < 1000; ++i) {
      out.println(getSnowperson(generator.nextInt(400), 50));
    }
    
    out.println("</svg>");
    out.println("</body>");
    out.println("</html>");
  }

  public static String getSnowperson(int x,
                                     int y) {
    int headRadius = 15;
    int torsoRadius = 20;
    int bottomRadius = 30;

    String bottom = getCircleTag(x, y + headRadius + 2 * torsoRadius + bottomRadius, bottomRadius, "green");
    String torso = getCircleTag(x, y + headRadius + torsoRadius, torsoRadius, "green");
    String head = getCircleTag(x, y, headRadius, "pink");

    // String joined = head + torso + bottom;
    // return joined;

    return head + torso + bottom;
  }

  public static String getCircleTag(int x,
                                    int y,
                                    int radius,
                                    String color) {
    return "<circle cx=\"" + x + "\" cy=\"" + y + "\" r=\"" + radius +
           "\" stroke=\"black\" stroke-width=\"2\" fill=\"" + color + "\" />";
  }
}

WordXCounter.java

package preexam2;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;

public class WordXCounter {
  public static void main(String[] args) throws FileNotFoundException {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File fileToRead = chooser.getSelectedFile();
    
    Scanner in = new Scanner(fileToRead);
    int szam = 0;
//    for each word in file {
    while (in.hasNext()) {
      in.next();
      ++szam;
    }
    
    in.close();
    
    System.out.println("You have " + szam + " words!");
  }
}

LoveGenerator.java

package preexam2;

public class LoveGenerator {
  public static void main(String[] args) {
    System.out.println(getXO(1));
    System.out.println(getXO(2));
    System.out.println(getXO(3));
  }
  
  public static String getXO(int nX) {
    String xox = "X";
    for (int i = 1; i < nX; ++i) {
      xox += "OX";
    }
    
    return xox;
  }
}

Haiku