# 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:

• You ask the user for a credit card number. Print it back to the console with all but the last four digits as asterisks.
• Students and teachers get a discount at your website. How can you tell if someone’s a student or teacher?
• You ask 128 people for their UWEC username—and username only. All of them append @uwec.edu anyway. How can you clean up one such input?
• You are writing a MUD and want to respond only to the commands north, south, east, and west. But the players are entering in all sorts of perversions of this like NORTH and east  (with trailing spaces). How can you “canonicalize” their input to what you expect?

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

• Homework 1 is due before Friday. I will grade Friday. Visit office hours now, both mine and the TAs, if you need to think out loud with someone. There are 11 scattered throughout the week. You are likely to encounter SpecChecker and Git issues—resolving them is as much a part of the homework as writing the code. Give yourself enough time to actually learn the ideas of the assignment. Do not wait until the last minute; I will not be around to support last-minute scrambling.
• Lab 2 is posted. You are always invited to work ahead.
• Read sections 1.4 (which we start discussing on Wednesday) and 3.3 (which we have already started discussing). On a 1/4 sheet, write down 2-3 questions or observations related to the reading. For one these items, consider writing a method that does something interesting.

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 = "************";
}
}


#### 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: ");

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);
}
}