CSU Banner

CS 150, Fall 2017

Lab 13 - More Practice With File I/O, Utilizing Methods in a Program, and the Start of P11

Monday - November 27th, 2017


Objectives of this Lab

  1. Review the structure and implementation of methods, then
  2. write code both in the main method and supporting methods, in order to
  3. read the contents of a text file into an array, and
  4. from this array, calculate the average song title length, then
  5. write the results to an output file, which
  6. will complete the first two methods of P11.

Getting Started

Create a new Java Project named P11, and make a class named P11. Once complete, P11 will have six additional methods, besides main, that perform the following: read a file, write to a file, calculate three different averages and a range based on the file information. In today's lab, after your TA briefly reviews the structure of the method signature and file input/output, you will be assisted over the course of the lab with the readFile(), averageTitleLength(), and writeFile() methods. The basis of this assignment is to calculate certain statistics on the top 15 singles in music. Song information was taken from the Rolling Stone article 500 Greatest Songs of All Time. The accuracy of this information may or may not be correct. Songs in the input file are listed number 1-15 as per the article. One song requires four lines in the file with the song title listed first, followed by the artist, then release year, and chart placement. If a song did not chart, its chart placement is listed as zero.

Today's Assignment

    Part One
  1. Create a new Java Project in Eclipse called P11. Make a class also called P11 and check the box to have Eclipse add the main method for you.
  2. It is recommended you read through the rest of the instructions first before beginning to write any code. Once you understand what you are expected to do in this assignment, you can better organize your thoughts and make a more efficient plan of action.
  3. Right click on the following link here and save it to your P11 project directory. Refresh your project to verify the file is in the correct place.
  4. Set up the program arguments for P11 to read SongList.txt SongStatistics.txt
  5. Copy the following code and paste it into your P11 class after the main method:
     
    public static void readFile(String fileName, String[] songArray) {
    	// YOUR CODE HERE
    	
    } // end readFile
    	
    public static int averageTitleLength(String[] songArray) {
    	// YOUR CODE HERE
    		
    } // end averageTitleLength
    	
    public static int averageYearReleased(String[] songArray) {
    	// YOUR CODE HERE
    	
    } // end averageYearReleased
    	
    public static int averageChartNumber(String[] songArray) {
    	// YOUR CODE HERE
    	
    } // end averageChartNumber
    	
    public static int[] determineRangeOfYears(String[] songArray) {
    	// YOUR CODE HERE
    	
    } // end determineRangeOfYears
    	
    public static void writeFile(String fileName, int avgTitle, int avgRelease, int avgChartNum, int[] songsRange) {
    	// YOUR CODE HERE
    	
    } // end writeFile
    

  6. Part Two
  7. First, in the main method, declare and initialize the following: three integers (representing average title length, average year song released, and average chart number) to zero, one integer array (representing range of years released) to a size of two, and a String array (representing all song information) to a size of 60.
  8. Next, make a call to the readFile() method with args[0] as the first argument and your String array as the second.
  9. After this, call averageTitleLength() with your String array as the argument and assign the call to your integer variable for average title length.
  10. Now make a call to writeFile() with the following as arguments: args[1], integer for average title length, integer for average year song released, integer for average chart number, and the integer array.
  11. Next, you will finish the code needed in readFile().
  12. First, create a try-catch block in readFile() which will hold all of your code for this method. For your catch, it is up to you what it does. The catch may even do nothing if you choose; though that is not very useful if your catch is executed.
  13. Inside the try, first create a Scanner to read from a File which should be created using the incoming fileName parameter.
  14. Following this, create an integer to represent the index of songArray and initialize it to zero.
  15. Using your Scanner object, create a while loop to read each line of the input file into successive elements of songArray.
  16. Your while loop should also increment the local integer variable each time it iterates, as this represents the next array element to initialize.
  17. After the while loop completes, close your Scanner.

  18. Part Three
  19. Now move to averageTitleLength().
  20. First, declare three integer variables, one to represent the sum of song title lengths, another to count the number of song titles, and one for the resulting average. Initialize all to zero.
  21. Write a for loop that iterates over songArray. You will need to look at the input file to determine how to change your index variable per loop iteration.
  22. Inside the loop, successively add the length of only the song titles and increment your counter variable as well.
  23. After your for loop, calculate the average and return it.
  24. Now move to writeFile().
  25. First, create a try-catch block in writeFile() which will hold all of your code for this method. For your catch, it is up to you what it does.
  26. Inside the try, first create a PrintWriter to write to a File which should be created using the incoming fileName parameter.
  27. Next, print TOP 15 STATISTICS to the output file.
  28. Then, print Average Title Length: along with avgTitle to the output file. This line should include a new line character.
  29. Lastly, close your PrintWriter.
  30. After you run your code, do not forget to refresh your project to be able to see SongStatistics.txt in the P11 directory.

Output File Contents

TOP 15 STATISTICS
Average Title Length: 15



CS Banner
CS Building