CS51 - Spring 2010 - Lecture 20

  • Exercise 16.5.2:
       - String s = "I drank java on the island of Java."
       - s.indexOf("java");
       - s.indexOf("java", 8);
       - s.indexOf("java", 9);
       - s.indexOf("an");
       - s.indexOf("an", 4);
       - s.indexOf("an", 5);

  • Quick review from last time:
       - what String methods do we have?
          - length
          - indexOf
          - substring
          - trim
          - replace
          - toLowerCase/toUpperCase
          - startsWith/endsWith
          - split
       - Strings are immutable... what does that mean?
          - e.g. String test = " this is a string ";
          - test.trim() does NOT change test!

  • A few more methods:
       test = " A string";

       - test.length();
          9
          (don't confuse this with array .length, which is not a method call)
       - How do you think a String is represented/implemented?
          - array!
          - of characters

       - test.toCharArray()
          [' ', 'A', ' ', 's', 't', 'r', 'i', 'n', 'g']
       - test.charAt(4)
          't'
       - test == " A string";
          false
       - test.equals(" A string");
          true
       - test.equals(" a string");
          false
       - test.equalsIgnoreCase(" a string");
          true
       - " A string" == " A string"
          true
          (Java uses a pool of Strings for string literals, so if a String literal occurs multiple times, it actually refers to the same String. Bottom line, just use .equals!)

  • show StringDemos demo

  • show BuildDemoPage demo
       - produces html code
       - this is what generates web pages (along with scripts, etc)
       - html tags
          - <tag>...</tag>
          - a few special tags that are by themselves: <br>
          - view page source
          - sometimes tags can contain "attributes"
             - attribute_name="attribute value"
             - these provide additional information but are not part of the tag name
             - for example, the applet tag
       
  • look at BuildDemoPage code
       - '+' for concatenate
       - \n
       - \"
       - other special characters (http://java.sun.com/docs/books/tutorial/java/data/characters.html)
          - \t
          - \\
          - \'
       - why do we need these escape characters?
       - something to watch out for
          - with any code with user input, be careful about malicious users or corner cases
          - what if the class name has an html tag in it?

  • counting occurrences of a word in a string
       - public int wordCount(String text, String word){
          int count = 0;
          int index = text.indexOf(word, 0);

          while ( index >= 0 ){
             index = text.indexOf(word, index + 1);
             count++;
          }

        return count;
       }
       
       - what if we wanted non-overlapping occurrences?
          - index = text.indexOf(word, index + word.length());
  • characters
       - char: another built-in type (like int, double, ...)
          - char c = 'a';
          - char c = "this is a string".getChar(7);
          - char[] charArray = "this is a string".toCharArray();
       - characters have integer values
          - what can we do with Integer?
             - >, <, >=, <=, ...
             characters have a defined sequence a...z, A...Z, 0...9
       - Character class has some useful static methods (like Integer or Double)
          - http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html
          - Character.isUpperCase(char c)
          - Character.isWhiteSpace(char c);
          - Character.isLetter(char c);
          - Character.isLetterOrDigit(char c);
          - Character.isDigit(char c);

  • compareTo
       - We can write:
          private void method(int x, int y){
             if( x < y ){
                // do something
             }
          }
       
       - What if we wanted to write:
          private void method(String x, String y ){
             if( x < y ){
                // so something
             }
          }

       - the comparison operators (<, >, <=, >=) only work on built-in data types, not on classes
       - However, seems like there is a natural ordering to String and we could compare them...
       - compareTo(String s) method
          - another method of the String class is the compareTo method
          - returns a number < 0 if this String is less than the String passed in
          - returns 0 if this String is equal to the String passed in
          - return a number > 0 if this String is greater than the String passed in

       - look at the Comparable interface (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html)
          - "Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object."
          - The String class implements the Comparable interface and has a compareTo method
          - can be used in general to compare objects if you want

          private void method(String x, String y ){
             if( x.compareTo(y) < 0 ){
                // so something
             }
          }

  • counting the number of sentences
       - For our purposes a sentence is:
          - a string of characters ending in a period followed by a space (or the end of the string)
          - the last word in the sentence should not be capitalized

       - public int sentenceCount(String text){
          String[] words = text.split(" ");
          int count = 0;

          for( String word: words ){
             if( word.endsWith(".") ){
                char first = word.getChar(0);

                if( !(first >= 'A' && first <= 'Z' ){
                // if( Character.isUpperCase(first) )
                   count++;
                }
             }
          }
       }

  • TP2: Centipede
       - read the whole description!
       - design
          - due on 11/24 at 11am (i.e. beginning of class)
          - won't grade until we grade your final submission
          - my advice:
             - make sure you understand the general problem
             - think at a high-level about how things interact
                - think through the questions in "The Design" section
             - incrementally develop the design by walking through the "Implementation Order" section, updating your design as you go
             - go back to the question in "The Design" section and make sure you've addressed them all
       - grading
          - 100 points total
          - like TP1 must to some extra credit to get 100 (this time, just 2 points worth)
          - up to 5 additional points of EC
          - 60 points come from just the style and design!
          - incremental design
             - much better to turn in good quality code that implements most of the functionality than bad code that implements everything
       - code quality
          - be careful not to introduce bad coding style with your extra credit
          - review the notes on 11/1 about common style mistakes