RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lab09b
Pig La(b)tin I

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.


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 “thiscan'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.

     

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Nov.05 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme