CS51 - Spring 2010 - Lecture 21
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);
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
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++;
}
}
}
}
Lab10: Apples(Hangman)
- may work with a partner
- if you need a partner, send me an e-mail
- I can even pair you up on the day of if you'd like a partner then
- if you have a partner, you must both be there whenever you are working on the program
- may not use arrays to store the letters
TP2: Asteroids
- read the whole description!
- extensions
- design
- due on 4/22 at 1:15pm (i.e. beginning of class)
- won't grade until we grade your final submission