After reading Chapters 4.3, 4.4, and 10.10, you should be familiar with the following terms and concepts:
Have a look at the documentation for the String class and the documentation for the Character class.
We will be demonstrating many of the methods in Chapters 4.3 and 4.4 during class. In particular:
*See your Java documentation for information about these methods.
NOTE: Section 10.10.4 refers to the supplement on Regular expressions, which you can download from inside your textbook under "Resources". Or you can use the RegEx Notes from this course.
1. Write a program that asks the user for a sentence. Display the sentence backwards, letter by letter. Example:
Enter a sentence: Mary had a little lamb. Your sentence backwards: .bmal elttil a dah yraM
2. Write a program that asks the user for a sentence. Then ask the user which character they'd like to replace, and which character to replace the first character with. Create a new string with all instances of the first character replaced with the second character, and display the new sentence on the screen. Example:
Enter a sentence: Mary had a little lamb. Enter a character to replace: b Enter a character to replace it with: p Your new sentence: Mary had a little lamp.
3. Write a program that asks the user to enter two strings representing an amount of time in the form hh:mm:ss. Then write the code that calculates the total number of seconds in that amount of time. Finally, calculate and display difference in seconds between the two times. For example:
Enter an amout of time in the form hh:mm:ss 2:13:55 Enter another amount of time in the form hh:mm:ss 14:03:12 There is a difference of 42,557 seconds between 2:13:55 and 14:03:12
Write a method called getTotalSeconds() that accepts a time string in the format hh:mm:ss. This method should extract the number of hours, minutes, and seconds from the string and then use these values to calculate the total number of seconds. The result should be returned.
4. A program that asks the user for a sentence, and then returns that string with the words backwards. For example:
Enter a sentence! Mary had a little lamb. Your sentence backwards: lamb. little a had Mary
Write a method called reverse() that accepts a sentence as a string, and then returns that string with the words backwards. Write a main() method that gets the string from the user and then displays the string backwards.
5. An array contains 5 strings. Write a program that creates the array (you can hard-code the array or fill it with user inputs), then displays the index numbers of elements that contain the letter "e" in upper-case or lower-case.
6. (advanced!) Validate a Social Insurance Number.
Canada's social insurance numbers (SIN) are validated using a special checksum algorithm that goes something like this:
Example using a ficticious SIN 046 454 286:
Step 1: Separate the first 8 digits from the check digit:
Value to check: 04645428 Check-Digit: 6
Step 2 & 3: Multiply the odd-positioned digits by 1 and the even-positioned digits by 2. For any result that is more than one digit, add the solution's digits together to get a single digit.
0 * 1 = 0 4 * 2 = 8 6 * 1 = 6 4 * 2 = 8 5 * 1 = 5 4 * 2 = 8 2 * 1 = 2 8 * 2 = 16, 1 + 6 = 7
Step 4: Add the digits in Step 2/3:
0 + 8 + 6 + 8 + 5 + 8 + 2 + 7 = 44
Step 5: Find the next highest number that is evenly divisible by 10 and subtract:
Next highest number from 44 is 50. 50 - 44 = 6
Step 6: The solution to step 5 is 6, which equals our check-digit of 6, so this is a valid SIN.
Write a program that prompts the user for a SIN and then displays whether or not the SIN is valid or invalid.
Check your program with these SINs:
410345678
123456030
193456787
123456789