CSU Banner

CS 150, Fall 2017

Lab 7 - For Loops, Strings, and Character Manipulation

Monday - October 9th, 2017


Objectives of this Lab

  1. Review the concept and execution of a for loop,
  2. practice writing for loops to manipulate Strings, and
  3. play around with the character primitive type.

Getting Started

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.

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?

Loop One
for(int i = 5; i <= 30; i+=5)
    System.out.println(i);
Loop Two
for (int k = 7; k > 0; k--)
    System.out.println(k);
Loop Three
for (int l = 0; l > 0; l++)
    System.out.println(l);
Loop Four
for (int i = 0; i < myString.length(), i++)
    System.out.print(myString.charAt(i));

Today's Assignment

  1. Declare a String variable called s0 and initialize it to "BASIC,C++,Erlang,Fortran,Python".
  2. Declare a String variable called s0ReverseCase and initialize it to the empty String.
  3. 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.".
  4. Declare a String variable called s2 and initialize it to "drawer keels".
  5. Declare a String variable called s2Reversed and initialize it to the empty String.
  6. Declare five int variables with the following names: upperCaseCounter, lowerCaseCounter, specialCharCounter, whitespaceCounter, letterOCounter. Initialize all to 0.

  7. 1st For Loop
  8. 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.
  9. 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.
  10. 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.
  11. Otherwise, increment specialCharCounter.
  12. Here is an example of how to use the Character wrapper class method, isLetterOrDigit(), for a single character from s0 in an if statement:
    if(Character.isLetterOrDigit(s0.charAt(index))) {     
           counter++; 
    }

    2nd For Loop
  13. 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().
  14. 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.
  15. 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.
  16. Otherwise the character at a specific index does not meet either of the above conditions, so simply append it unmodified to s0ReverseCase.
  17. 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:
    if(Character.isMirrored(s0.charAt(index))) { 
           s0Modified = s0Modified + Character.toTitleCase(s0.charAt(index)); 
    }

  18. 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
  19. The next for loop will iterate over s1.
  20. First, write an if statement that checks if the current character in s1 is whitespace. If so, increment whitespaceCounter.
  21. 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.
  22. 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
  23. The last for loop will reverse all of the characters in s2.
  24. 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.
  25. Using the appropriate String method, get one character from s2 at the appropriate index and append it to s2Reversed.
  26. Lastly, outside of the for loop, print the following line of output to the console followed by the appropriate variable.
    s2 Reversed: 


Console Output

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 




CS Banner
CS Building