CS 150, Fall 2017
Programming Assignment - P6
Use For Loops to Determine Statistics of Arrays and Manipulate Strings
Due - October 24th, 2017 at 6:00pm
Late - October 25th, 2017 at 8:00am
Objectives of this Assignment
- Write a program that calculates statistics of two different arrays and two Strings, and
- do so by utilizing for loops to access array elements or characters of Strings.
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:
- Declare and initialize an integer array with space to hold five elements - these will be integer values.
- 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.
- 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.
- Declare and initialize a Scanner object to read from the keyboard.
- You will need seven additional integer variables to hold the following information about this program:
- 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
- For the first part of this program, you will write a loop to iterate over the empty integer array.
- Inside this loop, print out the following prompt first:
Enter an integer value:
- 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?
- After the end of this for loop, print a blank line to the console.
- 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.
- Print the results after the following two prompts:
- First assign the variables for maximum and minimum number of home runs to the value in the first element of the home run array.
- Next, write another for loop to iterate over the home run array.
- 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.
- Next, outside of the for loop, calculate the average number of home runs and assign that value to the appropriate variable.
- Lastly, print the home run statistics to the console in the following format:
Home run average:
Maximum home runs in 1 year:
Minimum home runs in 1 year:
- 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.
- 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.
- Next, write the fifth for loop to iterate over the second String (defined above).
- In this for loop you will gradually build a new String using the String object currently containing only the empty string.
- 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.
- 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
- 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.
- Remember you will need to concatenate one character at a time to build the resulting String.
- After the fourth and fifth for loops, print the following prompts with their associated answers:
String without digits:
- And lastly, as good practice, close your Scanner object at the end of your code in main.
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 ,,,.
- Work on your own, as always.
- The name of the source code file must be exactly
- 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
- testCompile: checks that program compiles. (0 points)
- testMenu: verify correct menu printed (12 points)
- testEven: 12 points
- testOdd: 12 points
- testAvg: 12 points
- testMax: 13 points
- Final Tests
Final testing tests
- testMin: 13 points
- testVowels: 13 points
- testNoDig: 13 points
- Final grading includes the preliminary tests.
Submit P6.java to Checkin.