# CS 150, Fall 2017

## Use For Loops to Determine Statistics of Arrays and Manipulate Strings

#### Objectives of this Assignment

1. Write a program that calculates statistics of two different arrays and two Strings, and
2. do so by utilizing for loops to access array elements or characters of 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) determine number of even and odd values in an integer array; (2) determine the average, maximum, and minimum number of home runs Babe Ruth hit in the last ten years of his professional career; (3) count the number of vowels in a predefined String; and (4) remove all of the digits from a predefined String. For this assignment, you must follow the directions for output formatting below exactly:

Part One
1. Declare and initialize an integer array with space to hold five elements - these will be integer values.
2. Declare another integer array and initialize with the following values: 47, 60, 54, 46, 49, 46, 41, 34, 22, and 6. These ten values are the number of home runs Babe Ruth hit each year over a period of ten years - this should help you name your array with a name that is easy to track throughout your program.
3. Next, you will need to declare and initialize three Strings.
The first String object should hold the following sentence: "The band includes a guitar and a tambourine."
The second String object should hold the following sentence: "At 3:16pm on October 11th 2017, the world population was 7,573,517,788."
The third String should be initialized to the empty string and will, after your program executes, hold the characters in the second String that are not digits.
4. Declare and initialize a Scanner object to read from the keyboard.
• counter for number of even letters
• counter for number of odd letters
• average number of home runs
• maximum number of home runs
• minimum number of home runs
• total number of home runs
• counter for number of vowels in a String

Part Two
6. For the first part of this program, you will write a loop to iterate over the empty integer array.
7. Inside this loop, print out the following prompt first:
8. ```  Enter an integer value:
```
9. Next, use your Scanner object to initialize one element of the array to an integer entered by the user. The location in the array the user's value is initialized to should change with each loop - in effect, what variable do you already have within your for loop that you can also use as the location accessor when initializing the elements of your array?
10. After the end of this for loop, print a blank line to the console.
11. Now, write a second for loop that iterates over the array your code just initialized and determines the number of even and odd values in the array. To do this, think about what math operator we have seen that will help determine whether a value is even or odd. Do not forget the difference when it comes to math with integers and math with doubles as peformed by computers.
12. Print the results after the following two prompts:
13. ```  Even numbers:
Odd numbers:
```
Part Three
14. First assign the variables for maximum and minimum number of home runs to the value in the first element of the home run array.
15. Next, write another for loop to iterate over the home run array.
16. Inside this loop you will do three things:
• First, write a line of code that successively adds each value in the array to the variable representing the total number of home runs. Each time the for loop iterates, you will add the value in the next element to the total.
• Second, using a conditional, check if the current element in the array is greater than the value in the maximum home runs variable. If so, reassign maximum home runs to be the value at the current array location.
• Third, using another conditional, check if the current element in the array is less than the value in the minimum home runs variable. If so, reassign minimum home runs to be the value at the current array location.
17. Next, outside of the for loop, calculate the average number of home runs and assign that value to the appropriate variable.
18. Lastly, print the home run statistics to the console in the following format:
19. ```  Home run average:
Maximum home runs in 1 year:
Minimum home runs in 1 year:
```
Part Four
20. Your fourth for loop will count the number of vowels in the first String of this project (defined above). To do this, you should write a for loop that iterates over the first String described above and write a switch block inside the for loop.
21. Implement your switch block to switch on one character of the String per loop iteration. It is recommended that your cases be the vowel letters (do not include y as a vowel). Do not forget about flow through if several cases perform the same process in a program. Also, it may seem odd, but your default case does not actually have to do anything; so do not get caught up trying to figure out what it is supposed to do if you implement the solution to this problem in previous cases.

22. Next, write the fifth for loop to iterate over the second String (defined above).
23. In this for loop you will gradually build a new String using the String object currently containing only the empty string.
24. 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.
25. When building a new String without any of the digits currently in the second String, one of your conditional cases will not actually do anything; so you may leave its braces empty or use `continue`.
26. Note: When you create the resulting 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.
27. Remember you will need to concatenate one character at a time to build the resulting String.
28. After the fourth and fifth for loops, print the following prompts with their associated answers:
29. ```  Number vowels:
String without digits:
```
30. And lastly, as good practice, close your Scanner object at the end of your code in main.

#### Sample Output

Your program should print 13 lines each with an end of line. This includes the first five, where the user enters the values for the first array, followed by a blank line. User input is shown in blue.

```Enter an integer value: 23
Enter an integer value: 11
Enter an integer value: 1
Enter an integer value: 5
Enter an integer value: 6

Even numbers: 1
Odd numbers: 4
Home run average: 40
Maximum home runs in 1 year: 60
Minimum home runs in 1 year: 6
Number vowels: 16
String without digits: At :pm on October th , the world population was ,,,.
```

#### Specifications

• Work on your own, as always.
• The name of the source code file must be exactly
` P6.java `
• Name the file exactly - upper and lower case matters!
• Comments at the top as shown above.
• Assignments should be implemented using Eclipse.
• Assignments should be implemented using Java, version 1.8.
• Make sure your code runs on machines in the CSB 120 lab.
• Turn in through the Checkin tab.
• Read the syllabus for the late policy.
• We will be checking programs for plagiarism, so please don't copy from anyone else.

• 100 points for perfect submission.
• 0 points for no submission, will not compile, submitted class file, etc.
• Preliminary Tests
Preliminary testing.
• testCompile: checks that program compiles. (0 points)