Colorado State University Logo | Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3) Colorado State University Logo | Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Computer Science

Lab 15 - Reading Harry Potter


In this lab you will practice reading a file for the first time. Since Harry Potter is one of the greatest book series ever written, you all will be reading through the file to determine how many lines contain Harry Potter exactly, along with the number of vowels and consonants. You will also continue practicing to improve your knowledge of for loops, switch statements and String.format.

Step 0 - Reading Provided Code

In this lab there are two files provided for you: you will write your code in and is written for you.

Before you start the lab, take a moment to look over the code in as it will be important in the future. In the FileReader there are three methods. The first method creates a Scanner to read a file. The next method checks to make sure the Scanner has been initialized and that there is another line to read in the file. The last method returns the line of the file to be read.

The provided code already reads the file for you so in the other class you will only be calling the methods from the FileReader class in the FilePractice class.

Step 1 - Declare Class Variables

At the top of your class you should declare the following variables of type int:

  • harryPotterCounter;
  • aCounter;
  • eCounter
  • iCounter;
  • oCounter;
  • uCounter;
  • totalVowels
  • consonantCounter

Notice that there is an instance of the FileReader class called reader that is declared at the top of the class. You will use this in order to call the methods from the FileReader class in the next step.

Step 2 - Writing readFile

This method uses the methods in the FileReader class to read the file line by line and passing each line to a separate method that will parse through the line.

First you need to write the method signature. The method is called readFile and is a public void method with one parameter that is a String that represents the name of a file.

Inside the method you need to call the beginScanning method from the FileReader class and pass it the variable from the parameter. In order to call a method from the FileReader class you need to do something like the following:


Next you need to write a while loop. The loop should continue to run while there are more lines to read which can be done using the hasMoreLines method in the FileReader class. Inside the while loop you will call determineFileLineStats (which is the next method you need to write). determineFileLineStats takes a parameter of type String. You will want to pass the file line using getNextLine from the FileReader class.

Step 3 - Writing determineFileLineStats

This method is the core of your program where you will determine several stats.

First you need to write the method signature. The method is called determineFileLineStats and is public void method that has one parameter of type String that represents a line from the file.

Inside the method you will first write an if statement to see if the String from the parameter contains “Harry Potter”. If it does increment harryPotterCounter.

Next you want to write a for loop that starts at 0 and goes to the length of the String. Inside the loop you want to write an if statement that checks to see if the current character is a letter and if it is then you want to create a switch statement on the current character. You want to check to see if the character is the lowercase or uppercase version of each vowel and increment the corresponding counter. For the default case you want to increment the consonantCounter.

Outside of the for loop add up all of the vowel counters and assign the value to totalVowels.

Step 4 - Writing displayFileLineStats

This method display the the numbers that you previously calculated using String.format.

First you need to write the method signature. The method is called displayFileLineStats and is a public method that returns a String and does not have any parameters.

Using String.format and %d you need to return a String that says:

Harry Potter is said num times. There are num vowels and num consonants

The word num should be replaced with the correct class constant.

Step 5 - Testing

In order to make sure you used String.format correctly and your variables actually got incremented uncomment the commented out line in the main method.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)

Survey of computer science, formal logic, and computational thinking. Explores the historical, gender, and cultural perspectives on the role of technology in society. Includes learning a basic programming language. Students will be expected to write small programs, and construct written arguments on ways in which technology influences our modern culture. Previous computer science experience not necessary.