Skip to main content

Section 7.4 Stretch Level

ArrayLists
Data: song-lyrics.txt
As he came into the window
It was the sound of a crescendo
He came into her apartment
He left the bloodstains on the carpet
She ran underneath the table
He could see she was unable
So she ran into the bedroom
She was struck down
It was her doom
Annie, are you okay
So, Annie, are you okay
Are you okay, Annie
Annie, are you okay
So, Annie, are you okay
Are you okay, Annie
Annie, are you okay
So, Annie, are you okay
Are you okay, Annie
Annie, are you okay
So, Annie are you okay
Are you okay, Annie
Annie are you okay?
Will you tell us that you're okay
There's a sign in the window
That he struck you - a crescendo, Annie
He came into your apartment
He left the bloodstains on the carpet
Then you ran into the bedroom
You were struck down
It was your doom
Annie are you okay
So Annie are you okay
Are you okay Annie
You've been hit by
You've been hit by a smooth criminal
Ow!
Ow!
Aw!
Annie are you okay (I don't know)
Will you tell us that you're okay (I don't know)
There's a sound at the window (I don't know)
Then he struck you, a crescendo Annie (I don't know)
He came into your apartment (I don't know)
Left bloodstains on the carpet (I don't know why, baby)
And then you ran into the bedroom (help me)
You were struck down
It was your doom Annie (dag gone it)
Annie are you okay
So, Annie are you okay
Are you okay Annie
You've been hit by
You've been hit by a smooth criminal

Subsection 7.4.1 Overview

LyricProcessor is a Java program that will read and manipulate the text that is stored in a hidden text file named song-lyrics.txt, which currently contains the lyrics to Michael Jackson’s "Smooth Criminal". Each line of the song will be stored as an element in an ArrayList. The program will have the following functionalities:
  1. Prints all the lines in the file
  2. Prints lines that contain a specific word
  3. Replaces every instance of a word with a specific word

Subsection 7.4.2 Instructions

Your task is to implement the methods inside the LyricProcessorclass using the provided BufferedReader code that reads the file.
  1. Define an ArrayList of String to store the lines from the file
    • Initialize this ArrayList in the constructor
    • In readFile(String filename), ensure that each line that is being read is added to the ArrayList
  2. Implement the printAllLines() method
    • This method iterates through the ArrayList to print each line
  3. Implement printLinesContainingWord(String word)
    • Similar to printAllLines(), this method will iterate through the ArrayList but will only print the lines that contain word
    • Make sure to also print the total number of occurrences of word in the entire song
  4. Implement findAndReplace(String oldWord, String newWord)
    • This method will iterate through the ArrayList and replace each occurence of oldWord with newWord
In the provided template, a BufferedReader is used to read the lyrics from the file. Since you have not yet learned about File I/O, you don’t need to worry about how this part of the code works for now.
The template also includes a System.out.println() statement to display the contents of the text file. This is simply to help you see the lyrics that are being read in, even though the text file itself is hidden. You may comment this out when starting to write your code. Try and press the Save and Run button to see the contents of the file!
Hint.
The following methods may help in your implementations:
  • String.contains(word) This method returns a boolean and checks if String contains the specified substring word
  • String.replace(oldString, newString) This method returns a string and replaces every occurrence of oldString in String with newString.
The expected output for the provided tests is:
Lines containing 'okay':
Line 10: Annie, are you okay
Line 11: So, Annie, are you okay
Line 12: Are you okay, Annie
Line 13: Annie, are you okay
Line 14: So, Annie, are you okay
Line 15: Are you okay, Annie
Line 16: Annie, are you okay
Line 17: So, Annie, are you okay
Line 18: Are you okay, Annie
Line 19: Annie, are you okay
Line 20: So, Annie are you okay
Line 21: Are you okay, Annie
Line 22: Annie are you okay?
Line 23: Will you tell us that you're okay
Line 31: Annie are you okay
Line 32: So Annie are you okay
Line 33: Are you okay Annie
Line 39: Annie are you okay (I don't know)
Line 40: Will you tell us that you're okay (I don't know)
Line 48: Annie are you okay
Line 49: So, Annie are you okay
Line 50: Are you okay Annie
Total occurences of okay: 22

Lines containing 'apartment':
Line 3: He came into her apartment
Line 26: He came into your apartment
Line 43: He came into your apartment (I don't know)
Total occurences of apartment: 3

Replacing 'Annie' with '[YOUR NAME]':
Line 10: YOURNAME, are you okay
Line 11: So, YOURNAME, are you okay
Line 12: Are you okay, YOURNAME
Line 13: YOURNAME, are you okay
Line 14: So, YOURNAME, are you okay
Line 15: Are you okay, YOURNAME
Line 16: YOURNAME, are you okay
Line 17: So, YOURNAME, are you okay
Line 18: Are you okay, YOURNAME
Line 19: YOURNAME, are you okay
Line 20: So, YOURNAME are you okay
Line 21: Are you okay, YOURNAME
Line 22: YOURNAME are you okay?
Line 25: That he struck you - a crescendo, YOURNAME
Line 31: YOURNAME are you okay
Line 32: So YOURNAME are you okay
Line 33: Are you okay YOURNAME
Line 39: YOURNAME are you okay (I don't know)
Line 42: Then he struck you, a crescendo YOURNAME (I don't know)
Line 47: It was your doom YOURNAME (dag gone it)
Line 48: YOURNAME are you okay
Line 49: So, YOURNAME are you okay
Line 50: Are you okay YOURNAME
Total occurences of YOURNAME: 23

Line count:
Total number of lines: 52
You have attempted of activities on this page.