teaching machines

CS 145 Lecture 7 – String Methods

September 18, 2015 by . Filed under cs145, fall 2015, lectures.

Agenda

TODO

Note

Writing code is much more enjoyable when you start to recognize the patterns that govern its structure. So, today we start with a little exercise I call Right and Wrong. Following are a set of problems that may or may not violate some of these patterns. Which are right and which are wrong?

  1. String s = 'Coma sus ranas primero.';
    System.out.print(s + " " + s);

  2. String Guild = user.getGuild();
    Guild = "Guild: " + Guild;

  3. Scanner in = Scanner(System.in);
    int nDependents = in.nextInt();

  4. String s1 = "";
    int i;
    String s2 = s1 + i;

  5. int a = Scanner.nextInt();
    int b = Scanner.nextInt();
    System.out.println(a / b);

  6. Scanner parser = new Scanner("12 percent");
    double percent = parser.nextDouble()

From here, go on to discuss the model of the String class. We’ll see how it truly is a compound object made of primitives, and we’ll see how we can reach inside of it and get out the individual chars that comprise it.

Encapsulating compound data is only half the beauty of an object. The other half is the set of operations that we can perform on the object—through its methods. Let’s have a look at many of the String methods by solving some problems:

  1. You ask the user for a credit card number. Print it back to the console with all but the last four digits as asterisks.
  2. Students and teachers get a discount at your website. How can you tell if someone’s a student or teacher?
  3. You ask 112 people for their UWEC username—and username only. Many of them append @uwec.edu anyway. How can you clean up their input?
  4. You always send emails and say there’s an attachment, but you forgot to actually attach anything. How can you fix this problem?
  5. You’ve got the dimensions of the images your camera takes into a string of the form WIDTHxHEIGHT, where WIDTH and HEIGHT are actual numbers. How many megapixels is your camera?
  6. You have the city, state ZIP line of an address and need to make sure the state is properly capitalized. How do you do it?

How do we know how what the String class can do for us? By consulting the API.

Code

CreditCard.java

package lecture0918;

import java.util.Scanner;

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

EduDiscount.java

package lecture0918;

import java.util.Scanner;

public class EduDiscount {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter your email address: ");
    String email = in.nextLine();
    
    System.out.println("Edu? " + email.endsWith(".edu"));
  }
}

RemoveDomain.java

package lecture0918;

import java.util.Scanner;

public class RemoveDomain {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter your email address: ");
    String email = in.nextLine();
    
    int indexOfAt = email.indexOf('@');
    String username = email.substring(0, indexOfAt);
    
    System.out.println("Username: " + username);
  }
}

Haiku

For i to team.length
  Gimme a team.charAt(i)
In the cheerleading book