26-String-Processing-1

26-String-Processing-1 - CS106A Handout 26 May 2nd, 2011...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106A Handout 26 Spring 2011 May 2 nd , 2011 Text, Strings, and Characters Here’s a short handout capturing all of the small examples I plan on going over today. We’re beginning to deal with some sophisticated coding constructs with the advent of the String class, so I’d like you to have an official working copy of all the code snippets that aren’t covered in the book. You all want to read Chapter 8, Sections 1 through 5 over the course of the next few days, as it addresses all of what’s interesting to us about characters and strings. Playing Scrabble Strings make me think of words, and words make me think of Scrabble. Scrabble’s view of the English language is such that not all words are created equal, that some are clearly more impressive than others. "sat" is weak, but "zyzzyva" is clutch. Of course, it’s not so much about the word length as it is letter distribution: tens of thousands of words contain the letter 'a' , but only hundreds contain the letter 'z' , and as a result, you get more points for 'z' than you do for 'a' . Anyone who’s ever played Scrabble knows this. Let’s write a program that accepts a series of words from the user, one at a time, and tells us how much each word counts in a game of Scrabble. We’ll simplify the rules and not be concerned with anything other than the value of the individual letters. public class ScrabbleAssistant extends ConsoleProgram { public void run() { while ( true ) { String word = readLine("Please enter a word (or enter to quit): "); if (word.length() == 0) break ; int value = computeWordValue(word); println("\"" + word + "\" is worth " + value + " points."); } } private int computeWordValue(String word) { int value = 0; for ( int i = 0; i < word.length(); i++) { value += computeLetterValue(word.charAt(i)); } return value; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
private int computeLetterValue( char ch) { switch (ch) { case 'q': case 'z': return 10; case 'j': case 'x': return 8; case 'k': return 5; case 'f': case 'h': case 'v': case 'w': case 'y': return 4; case 'b': case 'c': case 'm': case 'p': return 3; case 'd': case 'g': return 2; default : return 1; } } } Sample run Please enter a word (or enter to quit): sat "sat" is worth 3 points. Please enter a word (or enter to quit): kablooey "kablooey" is worth 17 points. Please enter a word (or enter to quit): eleemosynary "eleemosynary" is worth 20 points. Please enter a word (or enter to quit): banquet "banquet" is worth 18 points. Please enter a word (or enter to quit): zygote "zygote" is worth 19 points. Please enter a word (or enter to quit): zyzzyva "zyzzyva" is worth 43 points. Please enter a word (or enter to quit): quiz "quiz" is worth 22 points. Please enter a word (or enter to quit): Here are some simple but important facts we can draw from the above example and test run: "sat" , "kablooey" , "eleemosynary" (Katie Couric’s word of the day some time last week), "banquet" , etc. are all strings. "banquet"
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

26-String-Processing-1 - CS106A Handout 26 May 2nd, 2011...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online