teaching machines

CS 1: Lecture 7 – String Cont’d

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.substring(3) is all numbers
 7: s.substring(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(s.charAt(2)) is 2
14: s.charAt(i % 2) is a consonant

A homework is due soon. I want to stress the following things as you work on homework:

  • Think away from the computer.
  • Solve a problem first as a human with a real set of inputs. Write out your solution; make it visible to your brain.
  • Solve the problem next as a human with symbols.
  • Solve the problem next as a translator between humanese and Java.
  • Commit and push after each coding session. Proper and timely submission is one of the expectations of the homework.

As we have time, let’s solve a few more problems related to Strings:

  • Prompt the user for her first name. Truncate it to no more than 10 characters and print the result.
  • Compute a shorthand form of a word comprised of just its first, middle, and last letters.
  • Print a file’s extension, which is just the last four characters of its name.
  • Print out how many periods (.) are in a String—without using a loop.

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.

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

  • Watch Education: An End to Fear, an episode of Extra Credits, which is a YouTube Channel on game design. The video explains why I use SpecCheckers in this class. There’s no quarter sheet for this.

See you next class!

Sincerely,

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

What’s more permanent
Tattoos or words on the web?
We should reset it

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

Truncator.java

package lecture0920;

import java.util.Scanner;

public class Truncator {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Name: ");
    String name = in.nextLine();
    int endIndex = Math.min(10, name.length());
    String truncatedName = name.substring(0, endIndex);
    System.out.println(truncatedName);
  }
}

Mad.java

package lecture0920;

import java.util.Scanner;

public class Mad {
  public static void main(String[] args) {
    // NOUN
    // ADJECTIVE
    // NOUN
    // VERB
    // NOUN
    // JOB
    // PLURAL NOUN
    // PLURAL NOUN
    // PLURAL NOUN
    // PLURAL NOUN
    // GERUND
    // GERUND
    // NOUN
    // ADVERB
    
    Scanner in = new Scanner(System.in);
    
    System.out.print("NOUN: ");
    String noun1 = in.next();
    
    System.out.print("ADJECTIVE: ");
    String adjective = in.next();
    
    System.out.print("NOUN: ");
    String noun2 = in.next();
    
    System.out.print("VERB: ");
    String verb = in.next();
    
    System.out.print("NOUN: ");
    String noun3 = in.next();
    
    System.out.print("JOB: ");
    String job = in.next();
    
    System.out.print("PLURAL NOUN: ");
    String noun4 = in.next();
    
    System.out.print("PLURAL NOUN: ");
    String noun5 = in.next();
    
    System.out.print("PLURAL NOUN: ");
    String noun6 = in.next();
    
    System.out.print("PLURAL NOUN: ");
    String noun7 = in.next();
    
    System.out.print("GERUND: ");
    String gerund1 = in.next();
    
    System.out.print("GERUND: ");
    String gerund2 = in.next();
    
    System.out.print("NOUN: ");
    String noun8 = in.next();
    
    System.out.print("ADVERB: ");
    String adverb = in.next();
    
    System.out.printf(message, noun1, adjective, noun2, verb, noun3, job, noun4, noun5, noun6, noun7, gerund1, gerund2, noun8, adverb);
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    



  }
  
  private static final String message = "The Kiddie Patch Early Learning Center is currently seeking a %s who is %s, loves %s, and is ready to %s various %s. Our %s position is in charge of preparing and cooking %s, washing %s, light duty cleaning, and keeping %s required by the state Food Program. Meals and snacks are healthy and pretty easy to prepare. Hours are generally 7:00am-1:00pm, Monday through Friday. After lunch is served, duties may include covering lunch breaks for other teachers, light duty cleaning, washing %s, etc. This position requires a lot of %s, some %s, and great %s.%nWe are looking to fill this position %s.";
  /*
   * The Kiddie Patch Early Learning Center is currently seeking a Cook who is energetic, loves children, and is
   * ready to complete various tasks. Our cook position is in charge of preparing and cooking meals, washing dishes,
   * light duty cleaning, and keeping production records required by the state Food Program. Meals and snacks are
   * healthy and pretty easy to prepare. Hours are generally 7:00am-1:00pm, Monday through Friday. After lunch is
   * served, duties may include covering lunch breaks for other teachers, light duty cleaning, washing toys, etc.
   * This position requires a lot of standing, some lifting, and great time management. We are looking to fill this
   * position immediately.
   */
  
  /*
   * The Kiddie Patch Early Learning Center is currently seeking a badger who is stupid, loves foot, and is ready to run various cake. Our chancellor position is in charge of preparing and cooking clouds, washing kittens, light duty cleaning, and keeping clowns required by the state Food Program. Meals and snacks are healthy and pretty easy to prepare. Hours are generally 7:00am-1:00pm, Monday through Friday. After lunch is served, duties may include covering lunch breaks for other teachers, light duty cleaning, washing potatoes, etc. This position requires a lot of eating, some smelling, and great Christopher.
We are looking to fill this position quickly.
   */
}

Comments

Leave a Reply

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