CS 150, Fall 2017
Lab 7 - For Loops, Strings, and Character Manipulation
Monday - October 9th, 2017
Objectives of this Lab
- Review the concept and execution of a for loop,
- practice writing for loops to manipulate Strings, and
- play around with the character primitive type.
Create a new Java Project named R7, and make a class named R7 with a main method.
Character Wrapper Class
Your TA will quickly review character primitives, the reason for the Character wrapper class, and the underlying ASCII representation for characters.
- Listed below are some of the Character wrapper class methods:
- isDigit() - returns true if char is '0'..'9'
- isWhitespace() - returns true if char is white space
- isLowerCase() - returns true if char is 'a'..'z'
- isUpperCase() - returns true if char is 'A'..'Z'
- isLetter() - returns true if char is 'A'..'Z' or 'a'..'z'
Understanding For Loops
Consider what each loop will output without first running it in Eclipse. You are certainly welcome to copy and paste this code into Eclipse to verify your expectations for each loop; however, this code and its successive output to the console are not required for today's lab grade. Note that even though most for loops are designed to start at a variable initialized to zero and are incremented after each iteration, it is useful at times to initialize your loop variable to a value other than zero and decrement the loop variable. Can you think of a situation where counting down will work better than counting up?
for(int i = 5; i <= 30; i+=5)
for (int k = 7; k > 0; k--)
for (int l = 0; l > 0; l++)
for (int i = 0; i < myString.length(), i++)
- Declare a String variable called s0 and initialize it to "BASIC,C++,Erlang,Fortran,Python".
- Declare a String variable called s0ReverseCase and initialize it to the empty String.
- Delcare a String variable called s1 and initialize it to the Confucius quote, "It does not matter how slowly you go, so long as you do not stop.".
- Declare a String variable called s2 and initialize it to "drawer keels".
- Declare a String variable called s2Reversed and initialize it to the empty String.
- Declare five int variables with the following names: upperCaseCounter, lowerCaseCounter, specialCharCounter, whitespaceCounter, letterOCounter. Initialize all to 0.
1st For Loop
- Your first for loop will count the number of upper case, lower case, and special case characters in s0. To do this you will need to use a few of the Character wrapper class methods. For this loop you will use the isUpperCase() and isLowerCase() methods as well as the String class charAt() method we have seen previously.
- First, write an if statement that handles the occurence of an upper case letter in s0. If there is an upper case letter at the index location, increment upperCaseCounter.
- Next, write another conditional that handles the occurence of a lower case letter in s0. If there is a lower case letter at the index location, increment lowerCaseCounter.
- Otherwise, increment specialCharCounter. Here is an example of how to use the Character wrapper class method, isLetterOrDigit(), for a single character from s0 in an if statement:
2nd For Loop
- The second for loop is meant to reverse the case of every letter in s0. For this loop you will again use isUpperCase() and isLowerCase(), but you will also need to incorporate toUpperCase() and toLowerCase().
- Start by checking if a character at an index is lower case. If it is, then modify the same character using the toUpperCase() method and add the altered character to s0ReverseCase.
- Next write another conditional that handles the occurence of an upper case letter in s0, then change the character to lower case using the toLowerCase() method and append the character to s0ReverseCase.
- Otherwise the character at a specific index does not meet either of the above conditions, so simply append it unmodified to s0ReverseCase.
Here is example of using a Character wrapper class method called isMirrored() in an if statement as well as using another Character method in the body of the if statement to modify one character in a String and then add the modified character to a String variable called s0Modified while also preserving any characters already in soModified:
s0Modified = s0Modified + Character.toTitleCase(s0.charAt(index));
- Next, outside of the for loop, print the following four lines of output to the console followed by the variable with the appropriate value. Add a blank line after the four lines regarding information about s0.
s0 Upper Case Letters:
s0 Lower Case Letters:
s0 Special Characters:
s0 Reverse Case:
3rd For Loop
- The next for loop will iterate over s1.
- First, write an if statement that checks if the current character in s1 is whitespace. If so, increment whitespaceCounter.
- Second, write another if statement that uses the boolean comparison operator == to check if the current character in s1 is equal to 'o'. If so, increment letterOCounter.
- Next, outside of the for loop, print the following two lines of output to the console followed by the appropriate variable. Add a blank line after the two lines regarding information about s1.
s1 Number Whitespaces:
s1 Number o's:
4th For Loop
- The last for loop will reverse all of the characters in s2.
- To do this you will need to write a for loop that starts at the end of s2 instead of at zero like we normally see with for loops. Remember the characters in a String are index based, but the length() method of the String class starts counting at one.
- Using the appropriate String method, get one character from s2 at the appropriate index and append it to s2Reversed.
- Lastly, outside of the for loop, print the following line of output to the console followed by the appropriate variable.
s0 Upper Case Letters: 9
s0 Lower Case Letters: 16
s0 Special Characters: 6
s0 Reverse Case: basic,c++,eRLANG,fORTRAN,pYTHON
s1 Number Whitespaces: 14
s1 Number o's: 12
s2 Reversed: sleek reward
- Make sure your R7.java compiles and produces the correct output.
- Your output should have 9 lines of output.
- You must complete the first 2 loops above for credit for the lab
- You may complete the last 2 loops for extra credit.
- Have your TA check your output for credit and take the associated Canvas quiz.
- Even if you do not finish today's lab, you still must complete the first question of the Canvas quiz.