CSU Banner

CS 150, Spring 2018

Programming Assignment - P6

Use For Loops to Determine Statistics of Strings and Manipulate Strings

Due - April 3rd, 2018 at 6:00pm

Late - April 4th, 2018 at 8:00am


Objectives of this Assignment

  1. Write a program that prints specific numerical values,
  2. calculates a statistic of one String,
  3. manipulates the characters in two other Strings, and
  4. do so by utilizing for loops to access characters of the Strings.

Instructions

Create a Java program called P6 with a class named P6, which after created should contain a file named P6.java in the src folder. All of the Java code you write should be contained in the main method. The goals of this program are as follows: (1) print the numbers in the range 1-50 (inclusive) that are divisible by both 2 and 5; (2) count the number of vowels in a predefined String; (3) remove all of the digits from a predefined String, and (4) reverse the characters in a predefined String. For this assignment, you must follow the directions for output formatting below exactly:

    Part One
  1. First, declare and initialize five Strings.
  2. You will also need one integer variable to represent a counter for the number of vowels in the first String object. Initialize to zero.

  3. Part Two
  4. Print out the following prompt including a newline character:
      Numbers (1-50) divisible by both 2 and 5: 
  5. Next, write a print statement that only prints the tab character.
  6. Now write a for loop designed to iterate 50 times.
  7. Inside this loop, write a conditional that is true only when a value in the range 1-50 is evenly divisible by both 2 and 5. If so, print the value to the console followed by a comma and one space. (HINT: What variable do you already have that can be reused as the testing value?)
  8. After the end of this for loop, print a blank line to the console.

  9. Part Three
  10. Your second for loop will count the number of lower case vowels in the first String of this project (defined above). To do this, you can write a for loop that iterates over the first String described above and write a switch block inside the for loop.
  11. Implement your switch block to switch on one character of the String per loop iteration. Make your cases the vowel letters (do not include y as a vowel). Do not forget about flow through if several cases perform the same function in a program. Also, it may seem odd, but your default case does not actually have to do anything; so do not get hung up trying to figure out what it is supposed to do if you implement the solution to this problem in previous cases.
  12. Next, write a third for loop to iterate over the second String (defined above).
  13. In this for loop you will gradually build a new String using your third String object currently containing only the empty string.
  14. This loop with utilize one of the Character wrapper class methods simultaneously with a conditional. The link to the Oracle document on the Character class, which has everything defined in this class, can be found here.
  15. There are two ways to implement building a new String without any of the digits currently in the second String. You can negate the first conditional statement and build the new String within the first conditional, or build the new String as your default condition. Either way works; so design your program using the way that makes the most sense to you.
  16. Additionally, one of your conditional cases will not actually do anything; so you may leave its braces empty or use the appropriate Java keyword related to for loops.
  17. Remember you will need to concatenate one character at a time to build the resulting String.

  18. Part Four
  19. The last for loop will reverse all of the characters in the fourth String object (defined above).
  20. To do this you will need to write a for loop that starts at the end of the String 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.
  21. Using the appropriate String method, get one character from the source String at the appropriate index and append it to your fifth String object, which should contain the empty String to start.
  22. Lastly, print the results of the previous three for loops using the following three prompts:
      Number of vowels in string:
      String without digits: 
      drawer keels reversed is: 
    Additionally, the results should print below the prompts and one tab from the left.
  23. Finally, make sure to include a newline after printing the last result.

Console Output

Your program should print 8 lines.

Numbers (1-50) divisible by both 2 and 5: 
	10, 20, 30, 40, 50, 
Number of vowels in string: 
	16
String without digits: 
	At :pm on March th , the world population was ,,,.
drawer keels reversed is: 
	sleek reward

Specifications

Grading Criteria


Submit P6.java to Checkin.


© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building