|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
For the following problems, remember that to extract the character at index i from a String s, you can either say s.charAt(i) (which returns a char), or s.substring(i,i+1) (which returns a String-of-length-1). Also, remember that to compare two Strings, use equals.
class TestVowelExpert { public static void main( String[] args ) { VowelExpert ve = new VowelExpert(); System.out.println( "Expect 3: " + ve.firstA( "senator" ) ); System.out.println( "Expect 1: " + ve.firstA( "yahoo!" ) ); System.out.println( "Expect 0: " + ve.firstA( "Aardvark" ) ); System.out.println( "Expect 4: " + ve.firstA( "huh?" ) ); // Some corner cases: System.out.println( "Expect 3: " + ve.firstA( "yoga" ) ); System.out.println( "Expect 0: " + ve.firstA( "a" ) ); System.out.println( "Expect 1: " + ve.firstA( "z" ) ); System.out.println( "Expect 0: " + ve.firstA( "" ) ); } } |
Challenge: don't have your code repeat the vowels in upper and in lower case. Instead, somehow use toLowerCase…
Challenge (optional): Write a single method firstCharFrom, which takes in two Strings: the word to search (as usual), plus a String of letters to search for (e.g. firstCharFrom("dyslexic quack","qxz") returns 5, because the character at index 5 ('x') occurs inside "qxz".
Once you have this method working, you can replace both of the above functions with single-line calls to firstCharFrom.
Write a method which takes in a single word, and converts it to pig latin. (Ta-dum!)
Do this by calling your previous method firstVowel1 using the result to decide which substrings to take from your input word.
(To think about: what does your method do, if the input contains no vowels at all?)
(For how to slice together substrings, recall also the solution to left2 on hw05-soln.html.)
1 How do you have one of your own methods call another? The same way we've always called methods: “object - dot - methodname - paren -arguments”. The question is, what to use for that first “object -dot”?
The answer is: use whichever object whose pig-latin method was called; you can do this by using the variable “this”, which always refers to "the object whose method is being called". It's just a regular variable, but one which Java secretly declares and initializes for you in every method. Example: inside pigLatinize, you might write something like “int splitPoint = this.firstVowel(…)”.
Note that if you call a method but you leave off the “object - dot” of then Java secretly writes in “this.” for you. Rather than think “Oh, the object-dot is optional”, I prefer to think “if I don't tell what Java which object I want to use, by default it uses the object whose method we're currently in.”
The variable “this” can't be used in BlueJ's Code Pad: because as you are typing, we're not in the middle of a method-call, so there's no object whose method we're currently in.
↩home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
©2008, Ian Barland, Radford University Last modified 2008.Nov.05 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |