teaching machines

CS 145 Lecture 6 – The String Class

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

Dear students,

If this class was a book, the chapter we’re in right now is Computer as Calculator. I’d mentioned that computers got their start in the world of mathematics. Why exactly did computers start with the mathematicians?

Lately I’ve been reading some graphic novels related to technology and computer science for a conference I’ll attend in the spring. The novel I’m reading currently is The Thrilling Adventures of Lovelace and Babbage, by Sydney Padua. These two, who lived before in England before the Civil War in the United States, are among the pioneers of computers. They worked for decades on mechanical computers—not electronic ones. Sadly, both were a bit nutty, and then there was cancer, and then there was never any machines. Babbage was 100% mathematician and scientist, but Lovelace was not. Her father was Lord Byron, a poet. I won’t say much about him, except that there’s a research paper on him entitled Byron as Cad. Her mother was more rational, a mathematician. Byron left his family a month after Ada’s birth, never to return. Her mother reacted by squelching every hint of her father’s “poeticism” from Ada’s character. Let me share with you a few pages from the book…

Ada had a vision for computation that extended beyond numbers and math problems. We, too, shall pivot away from numbers, to the great relief of some of you. Today we’ll talk about text. We introduce the String class, which stands for a String of characters.

First, we should make it clear that there are two types of citizens in Java: primitives and objects. Primitives are the dregs of society, they are simple numbers or letters or trues or falses. Their type names are all lowercase. Objects are bigwigs, having their type names capitalized. We create them in slightly different ways:

Type myObject = new Type(...);
type myPrimitive = ...;

We also act upon them in different ways. Primitives are bullied about with operators like + and %. Objects are deferentially asked to do things through their methods:

prim1 + prim2 + prim3           // operators
object.pleaseDeleteAllMyFiles() // method call

So, we can make this analogy:

operators : primitives :: methods : objects

The data type of an object is its class. We’ve formally met one object class so far: Scanner. Now it’s time to meet String. Based on our discussion above, we create a String like this:

String name = new String("Frank Lee");

The thing is, we use Strings so often, the Java folks have allowed us to say it more compactly:

String name = "Frank Lee";

Now, we could jump immediately into describing the superpowers that String confers to you, but I’d rather we get a feel for what kinds of things we do with text. Let’s play a little game. Here goes. I’m thinking of a String. You need to figure out what it is by asking only yes or no questions. Can you do it? Can you do it quickly?

It’s likely you are going to ask me questions that correspond to methods in the String class. If so, we have achieved our purpose.

We’ll discuss how Strings are laid out in memory, how we can index into them, extract substrings and so on, through a few example problems:

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

See you next class!

Sincerely,

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

CreditCard.java

package lecture0919;

import java.util.Scanner;

public class CreditCard {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("CCN: ");
    String number = in.nextLine();
    String prefix = "************";
    String masked = number.substring(12);
    System.out.println(prefix + masked);
  }
}

EduDiscount.java

package lecture0919;

import java.util.Scanner;

public class EduDiscount {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Email: ");
    String address = in.nextLine();

    boolean isDiscounted = address.endsWith(".edu");
    System.out.println(isDiscounted);
  }
}

CleanerUpper.java

package lecture0919;

import java.util.Scanner;

public class CleanerUpper {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("ID: ");
    String line = in.nextLine();

    int indexOfAt = line.indexOf('@');
    String id = line.substring(0, indexOfAt);
    System.out.println(id);
  }
}