teaching machines

CS 145 Lecture 7 – String Cont’d and Methods

September 21, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

With Strings at our disposal, our programs are going to start to feel human. We can write programs that process and generate words, and that’s pretty amazing! But we need more practice using them before we determine the authenticity of Shakespeare’s works. Let’s play some Stringo!

Generate a String of five characters. Use lowercase letters, numbers, and punctuation—all your choice. Label each character with its index. We’ll refer to your String through identifier s.

I will call out a series of predicates that describe parts of s. If a predicate is true for your String cross off the characters involved in making the predicate true. Write the predicate number next to the characters that it crossed off. First one to cross off all five characters wins a meager prize and enduring fame. Here are the predicates:

 0: s.charAt(0) is equal to s.charAt(s.length() - 1)
 1: s.charAt(3) is a vowel
 2: s.indexOf('a') % 2 is 0
 3: s.charAt(3) >= 'm' and s.charAt(3) <= 'z'
 4: s.indexOf('f') <= 3
 5: s.charAt(i) is punctuation and s.charAt(i + 1) is a letter
 6: s.substr(3) is all numbers
 7: s.substr(2, 4).charAt(0) is not a letter
 8: s.indexOf('a') + 1 < 5
 9: s.indexOf('o') < s.indexOf('k')
10: s.charAt(1) does not equal s.charAt(4)
11: s.charAt(i) is equal to s.charAt(i + 1)
12: s.indexOf('5') >= 0
13: s.indexOf('%') % 3 <= 1
14: s.charAt(i % 2) is a consonant

Next we’ll write a MADLIB, an activity which I enjoyed far too much when I was a kid. We’ll take a piece of text that has had a few words extracted from it, and then we’ll ask the user to replace those words with their own inputs of the same grammatical category.

Finally, we introduce methods. So far, all our has code has been tucked away inside of main. Eventually, we will outgrow this method. Today, in fact.

Sometimes we write code that we want to execute again and again. Let’s see an example of this. We will generate some random circle art using the SVG protocol. An outlined circle looks like this in SVG:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<circle cx="CENTERX" cy="CENTERY" r="RADIUS" stroke="black" stroke-width="2" fill="rgb(RED, GREEN, BLUE)"/>

The capitalized words are just placeholders. The first three are in pixel units, and the last three are in [0, 255]. Let’s start by filling them in with random values. We’ll use the Random class this time, because unlike Math.random, it can generate ints for us.

Once we get that working, let’s add a second random circle. And a third. And a fourth. And so on. We’ll find that the code quickly gets away from us if we simply copy and paste. Instead, we factor out the repeated code to a method. For the time being, our programs with multiple methods will have this structure:

public class ClassWithMultipleMethods {
  public static void main(String[] args) {
    // ...
    // ...
    // ...

  public static void helperMethodA() {
    // ...

  public static void helperMethodB() {
    // ...

Let’s make a method that generates a random circle and call it a bunch of times!

Here’s your TODO list to complete before next class:

See you next class!


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


package lecture0921;

import java.util.Scanner;

public class Madlib {
  public static void main(String[] args) {
    String messageWithBlanks = String.format("Desperately looking for my lost NOUN1 which I%nADVERB placed on the NOUN2 of my NOUN3%nand then PASTVERB away. It contains all of my%nNOUN4 (including my thesis), NOUN5, music, documents, and%nwork material. It fell off on Sept 10th probably somewhere%nin the PLACE1 or along Clairemont on the way to%nPLACE2. It was in a ADJECTIVE1 ADJECTIVE2 case and had some%npapers along with it that will prove it belongs to PERSON.%n%nPlease, please, pleeeease let me know if you%nhave seen it or have found it!%n%nThank you!");
    String message = String.format("Desperately looking for my lost computer which I absentmindedly placed on roof of my car and then drove away. It contains all of my school work (including my thesis), photos, music, documents, and work material. It fell off on Sept 10th probably somewhere in the Third Ward or along Clairemont on the way to Minneapolis. It was in a black soft-shell case and had some papers along with it that will prove it belongs to me.%n%nPlease, please, pleeeease let me know if you have seen it or have found it!%n%nThank you!");

    Scanner in = new Scanner(System.in);

    System.out.print("NOUN: ");
    String noun1 = in.nextLine();

    System.out.print("ADVERB: ");
    String adverb = in.nextLine();

    System.out.print("NOUN: ");
    String noun2 = in.nextLine();

    System.out.print("NOUN: ");
    String noun3 = in.nextLine();

    System.out.print("PAST VERB: ");
    String verb = in.nextLine();

    System.out.print("NOUN: ");
    String noun4 = in.nextLine();

    System.out.print("NOUN: ");
    String noun5 = in.nextLine();

    System.out.print("PLACE: ");
    String place1 = in.nextLine();

    System.out.print("PLACE: ");
    String place2 = in.nextLine();

    System.out.print("ADJECTIVE: ");
    String adjective1 = in.nextLine();

    System.out.print("ADJECTIVE: ");
    String adjective2 = in.nextLine();

    System.out.print("PERSON: ");
    String person = in.nextLine();

    messageWithBlanks = messageWithBlanks.replace("NOUN1", noun1);
    messageWithBlanks = messageWithBlanks.replace("ADVERB", adverb);
    messageWithBlanks = messageWithBlanks.replace("NOUN2", noun2);
    messageWithBlanks = messageWithBlanks.replace("NOUN3", noun3);
    messageWithBlanks = messageWithBlanks.replace("PASTVERB", verb);
    messageWithBlanks = messageWithBlanks.replace("NOUN4", noun4);
    messageWithBlanks = messageWithBlanks.replace("NOUN5", noun5);
    messageWithBlanks = messageWithBlanks.replace("PLACE1", place1);
    messageWithBlanks = messageWithBlanks.replace("PLACE2", place2);
    messageWithBlanks = messageWithBlanks.replace("ADJECTIVE1", adjective1);
    messageWithBlanks = messageWithBlanks.replace("ADJECTIVE2", adjective2);
    messageWithBlanks = messageWithBlanks.replace("PERSON", person);


     * NOUN: car ADVERB: quickly NOUN: Chris NOUN: fish PAST VERB: ran NOUN:
     * dog NOUN: Chancellor Jim PLACE: basement PLACE: Mars ADJECTIVE: rusty
     * ADJECTIVE: spooky PERSON: JOHN CENA Desperately looking for my lost
     * car which I quickly placed on the Chris of my fish and then ran away.
     * It contains all of my dog (including my thesis), Chancellor Jim,
     * music, documents, and work material. It fell off on Sept 10th
     * probably somewhere in the basement or along Clairemont on the way to
     * Mars. It was in a rusty spooky case and had some papers along with it
     * that will prove it belongs to JOHN CENA.
     * Please, please, pleeeease let me know if you have seen it or have
     * found it!
     * Thank you!


package lecture0921;

import java.util.Random;

public class Circles {
  public static void main(String[] args) {
    System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
    Random generator = new Random();
    int originX = generator.nextInt(1000);
    int originY = generator.nextInt(800);
    int radius = generator.nextInt(600);
    int r = generator.nextInt(256);
    int g = generator.nextInt(256);
    int b = generator.nextInt(256);

    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);