To complete this set of problems you must have gone through the following lessons:
- All lessons for Problem Sets 1, 2, 3, 4 & 5
- String Manipulation
String Functions Quick Reference
charAt() |
to find the character at a position in a String |
compareToIgnoreCase() |
to compare two Strings while ignoring the cases of the letters |
compareTo() |
to compare the spelling of two Strings. Is case sensitive |
concat() |
to join Strings together |
endsWith() |
to determine the ending of a String |
equalsIgnoreCase() |
to determine if two Strings are identical while ignoring the case of the letters |
equals () |
to determine if two Strings are identical |
indexOf(“x”) |
to find the first position of a character, in this example the letter x, in a String |
indexOf(“x”,2) |
to find the position of a character, in this example the letter x, in a String starting at a position, in this case 2 |
lastIndexOf() |
to find the last position of a character in a String |
length() |
to find the length of a String() |
replaceAll() |
to replace all occurrences of a letter or group of letters in a String |
replace() |
to replace all occurrences of a letter with another letter |
startsWith() |
to determine what letters a String starts with |
substring(x) |
to get a part of a String starting at position x in the String and going to the end of the String |
substring(x,y) |
to get a part of a String starting at position x and going to one less than position y |
toLowerCase() |
to force the String to all lower case letters |
toUpperCase() |
to force the String to all upper case letters |
trim() |
to remove any spaces at the beginning and ending of a String |
Integer.parseInt() |
not a String function but can take a String and turn it into an integer |
Problem 1 - use substring(x,y) |
Write a method that prompts the user to enter his or her first and last names and then displays the initials of the name in uppercase no matter the case used in the entry. |
Output Example |
Enter your first name: joe
Enter your last name: smith
Your initials are: JS |
Problem 2 - use indexOf(), lastIndexOf(), length(), toUpperCase(), toLowerCase() and either charAt() or substring(x,y); |
Write a method that prompts the user to enter his or her first, middle, and last names and then displays a monogram with the first and middle initials in lowercase and the last initial in uppercase. |
Output Example |
Enter your whole name: Joe William Smith
Your monogram is: jSw |
Problem 3 - use replaceAll() |
Write a method that asks for a sentence to be entered, some text to be searched, and a substitute if the text is found during the search. |
Output Example |
Enter a sentence: This is a very boring sentence.
Enter the search text: boring
Enter the replacement text: exciting
The new sentence is: This is a very exciting sentence. |
Problem 4 - use replace() |
Write a method that asks for a sentence to be entered, a letter to be replaced and the letter to replace it with. Every occurrence of the letter should be replaced. |
Output Example |
Enter a sentence: This is interesting.
Enter the letter to be replaced: i
Enter the replacement letter: A
The new sentence is: ThAs As AnterestAng.
|
Problem 5 - uses toUpperCase(), charAt() or substring(x,y) |
Write a method that asks the user to enter a number that determines the number of words that will be used to create an acronym. Each word entered will need to stored in a String array. After the words have been entered, the computer will generate an acronym by combining the first letter of each word. (Hint: declare the array after you know how many words will be entered.) |
Output Example |
Enter the number of words to use: 3
Enter word 1: Dan
Enter word 2: observes
Enter word 3: ghosts
The acronym is: DOG |
Problem 6 - uses toLowerCase(), length() and either substring(x,y) or charAt() |
Write a method that asks for a name to be entered and then prints the name in lowercase and reverse order. |
Output Example |
Enter your name: Tom Thumb
In reverse order: bmuht mot |
Problem 7 - uses length() and charAt() or substring(x,y) with compareToIgnoreCase() |
Write a method that asks for a word or phrase to be entered. The computer should then count the number of vowels and display the results. |
Output Example |
Enter a word or phrase: bananas are good
The number of vowels is: 7 |
Problem 8 - uses length(), charAt() and (int) |
Write a method that asks for a word to be entered. The computer should display the ASCII Code (Unicode) number for each letter of the word. |
Output Example |
Enter a word: Cat
C = 67 a = 97 t = 116 |
Problem 9 - uses trim(), indexOf(), lastIndexOf() length(), charAt(), (char), Integer.parseInt() and (int) |
Write a method that decodes a numeric message. The computer should ask for a numeric message and then either decode the message. |
Output Example |
Enter the secret message: 77 101 101 116 32 109 101 32 102 111 114 32 108 117 110 99 104 46
The decoded message is: Meet me for lunch. |
Problem 10 - uses length(), charAt(), (char) and (int) |
Write a method that codes a message by using ASCII (Unicode) Code to convert the characters to numbers, and then adding 2 to each number and then converting back to characters. Keep all spaces (unicode value for a space is 32) in their original places and realize that the letters Y and Z are to be converted to A and B.
Unicode values you will need: Y=89, Z=90, y=121, z=122
|
Output Example |
Enter a message: The zoo has animals
The scrambled message is: Vjg bqq jcu cpkocnu |
Problem 11 - uses trim(), equals(), length(), replace(), charAt() and toLowerCase() |
A palindrome is a word or phrase that is spelled the same backwards and forwards (mom, dad, race car, A ram ate Tamara, etc.). Write a method that determines if an entry is a palindrome. |
Output Example |
Enter a word or phrase: Race car
You have entered a palindrome. |
Problem 12 - uses indexOf(), charAt(), substring() and toUpperCase() |
Write a method that determines which group a student belongs to depending on the name they enter. The groups to use are as follows: A through I are in Group 1, J through S are in Group 2, T through Z are in Group 3. |
Output Example |
Enter a student name: Gus Neatnik
Gus is in Group 2 |
Problem 13 - uses trim(), toLowerCase(), substring(), length() and charAt() |
Write a method that takes a sentence and writes the words in the reverse order that they were entered. Make sure the same punctuation is used and that the first word is capitalized, while the last word is lowercase. You can assume that the entered sentence will have punctuation and that there are no proper nouns that need to retain their capitalization. |
Output Example |
Enter a sentence: This programming is too much fun.
Reverse: Fun much too is programming this. |
Problem 14 - uses trim(), toLowerCase(), (int), length() and charAt() |
Write a method that takes a sentence and analyzes it for the number of vowels and consonants. |
Output Example |
Enter a sentence: I want to be a Java programmer.
Vowels: 10
Consonants: 14 |
Problem 15 - uses length(), trim() and indexOf(x,y) |
Write a method that takes a sentence and counts the number of spaces found before, within and after the entry. |
Output Example |
Enter a sentence: Way to go!
Total spaces: 6
Spaces before text: 3
Spaces after text: 1
Spaces inside text: 2 |
|