CS 150, Fall 2017
Programming Assignment - P11
Music Singles: Top 15 Statistics
Due - December 7th, 2017 at 6:00pm
Late - December 8th, 2017 at 8:00am
Objectives of this Assignment
- Use methods to calculate statistics on song information,
- review the purpose behind different method signatures, and
- review file input/output.
Instructions
Create a Java program called P11 with a class named P11. Please complete the code required from this week's lab first if you have not already done so. All of the Java code you write should be contained in the P11 class. To complete the assignment, fill in the required code for the three remaining methods, as well as add more code to main() and writeFile().
Part One
- Start by writing the code necessary in averageYearReleased().
- First, declare three integer variables, one to represent the sum of release years, another to count the number of release years, and one for the resulting average. Initialize all to zero.
- Write a for loop that iterates over
songArray
. You will need to look at the input file to determine where your for loop should start and how to change your index variable per loop iteration.
- Inside the loop, successively add the values of only the release years and increment your counter variable.
- Because
songArray
is an array of Strings, you will need to parse each release year to an integer value. Do so by using the parseInt() method of the Integer wrapper class: Integer.parseInt(String argument)
- After your for loop, calculate the average and return it.
- Next, move to averageChartNumber().
- First, declare three integer variables, one to represent the sum of chart placements, another to count the number of chart placements, and one for the resulting average. Initialize all to zero.
- Write a for loop that iterates over
songArray
. You will need to look at the input file to determine where your for loop should start and how to change your index variable per loop iteration.
- Inside the loop, successively add the values of only the chart placement values and increment your counter variable.
- Because
songArray
is an array of Strings, you will again need to parse each song's chart value to an integer value.
- After your for loop, calculate the average and return it.
Part Two
- Now finish the code necessary for determineRangeOfYears(). Notice the return type of this method is different than the average methods.
- First, declare and initialize two integers to represent the minimum and maximum year from the input file. These two variables with both be initialized to the first year listed in the file which you should access via
songArray
. Again, you will need to parse the value from the array, since songArray
is a String array.
- Second, declare and initialize an integer array to a size of two. This array with hold the minimum and maximum year values.
- Now write a for loop that iterates over
songArray
. You will need to look at the input file to determine where your for loop should start and how to change your index variable per loop iteration.
- Inside the for loop, you will need two conditionals. One should check if the current element of
songArray
is less than the current value of the minimum year. The other will check if the current element of songArray
is greater than the current value of the maximum year.
- Utilize parsing again to do this comparison - the program should check an integer against another integer.
- If either of the above conditionals are true, reassign the minimum or maximum year appropriately. You will need to parse the value in
songArray
again during the reassignment.
- After the for loop, set the first element of your local integer array to the minimum year and the second element to the maximum year.
- Then, return the integer array.
Part Three
- The last part of this assignment requires you to write several more lines of code main() and writeFile().
- In writeFile() add the following three prompts along with the associated variables:
Average Year Released:
Average Chart Placement:
Range of Release Years:
- After the Range of Release Years prompt, you must also include a hyphen between the year values with no spaces. This line should also include a new line character.
- Now move to main() where you will finish writing the missing method calls and assignments.
- Before the call to writeFile(), make calls to averageYearReleased(), averageChartNumber(), and determineRangeOfYears(). Assign all three method calls to the appropriate local variables in main().
Output File Contents
TOP 15 STATISTICS
Average Title Length: 15
Average Year Released: 1967
Average Chart Placement: 9
Range of Release Years: 1958-1991
Specifications
- Work on your own, as always.
- The name of the source code file must be exactly
P11.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.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
Preliminary testing tests lines 1-5 .
- testCompile: checks that program compiles. (0 points)
- lineTests: verify correct output based on String method used.
- test1:
- test2:
- test3:
- test4:
- test5:
- Final Tests
Final testing tests lines 6-8.
- test8:
- test9:
- test10:
- Final grading includes the preliminary tests.
Submit P11.java to Checkin.