Questions:
1. What is an Index Out of Range Exception? Give an example of a code where your program encounters it.
The character is beyond the number of characters in a string.
(the length of a string is 10 and the character is at 11)
2. What is wrong with this code? Its purpose is to print a message vertically.
charPos should be initialized to 0
public void printMsg()
{
String message = “Subscribe to TelCO and save!!!”
int charPos = 1;
charPos is e.message.length() therefore it would be out of range
while (charPos <= message.length())
{
System.out.println(message.charAt(charPos);
charPos = charPos + 1;
}
}
3. What is an immutable class? What are the advantages and disadvantages of an immutable class? An immutable class is a class that cannot be changed.
Advantage: stays the same
Disadvantage: harder for the programmer (each time it should be changed, another new string must be made)
What is the advantage of immutability as it applies to the String class? The disadvantages ?
What must you do to make a class immutable?
Use the keyword final
Do you have to use the finalize keyword?
Yes
4. What is a wrapper class? What are some examples and their methods? What is the purpose of a wrapper class?
A wrapper class provides more methods and functionality to primitive types.
Example: integer.compare( intx, int y )
5. Do the repeatLetters program in the Study Guide PowerPoint. Paste this into StringUpdatedCollatedTests to test: