SlideShare a Scribd company logo
Data Structure and Algorithm Design
CSCI 3000
Name: Date:
Programming Final Credit – HashMap (Lyric Analyzer)
Since the lyrics of most pop music contains words that repeat a
number of times, a simple way to compress
a lyric file is to create a map that stores each word once along
with the positions of each word in the file.
For example, suppose the lyric consists of the lines:
What have I
1 2 3 <-- word position in lyrics
What have I
4 5 6 <-- word position in lyrics
What have I done to deserve this
7 8 9 10 11 12 13 <-- word position in lyrics
We would form a map that maps each unique word to a list of
word positions in the lyric. NOTE: The
word position for a word at the end of a line is stored as a
negative integer rather than a positive integer so
you can recreate the lyrics later when you iterate through the
words in the map.
Sample map for the lyric above (order of words may vary):
Word Word Position(s)
===========================
WHAT 1, 4, 7
HAVE 2, 5, 8
I -3, -6, 9
DONE 10
TO 11
DESERVE 12
THIS -13
This project currently contains only one class that models the
analyzer using a HashMap that maps a lyric
word to a list of integers representing the locations of the word
in the lyrics.
1. Complete the class for the lyric analyzer by adding the
following methods listed below.
a. Write an add method with the following signature:
public void add(HashMap<String,ArrayList<Integer>> map)
This method will determine if the given lyric word is in the
map. If the word is not in the
map, then a mapping is added that maps that word to a list
containing the position of the
word in the lyric. If the word is already in the map, then its
word position is added to the list
of word positions for this word. Do not create a new mapping if
the lyric word is already in
the map. Use the one that is already there and just update its
list. Remember to negate the
word position if the word is at the end of a line in the lyrics.
b. Write a displayWords method with the following signature:
public void
displayWords(HashMap<String,ArrayList<Integer>> map)
This method should display the words of the song along with
the word positions of the
word, one word per line, in alphabetical order. You should do
this without creating
another map. Instead, get a set of all the words stored in the
map. Sort this set using one of
the sort methods from the Java API. Then iterate over this
sorted set and print out each
word along with the word positions associated with each word.
You may leave the negative
integers in the word position list. (see sample output below)
Iterate through the array of
words using the enhanced for loop.
c. Write a displayLyrics method with the following signature:
public void
displayLyrics(HashMap<String,ArrayList<Integer>> map)
This method will display the lyrics for the song (in uppercase)
stored in the map. Start with
an empty array of strings whose size is the number of words in
the lyric plus 1 (don't use
cell 0). Initialize this array with empty strings (not null). Then,
get a set of all of the words
stored in the map. For each word, store the word in the array
cells corresponding to its word
position(s). If a word position is negative, add on an extra
newline character to the word
when you store the word in the array. Once you finish
processing all words that are in the
map, iterate through the array and print out each string, and you
should see the lyrics
appear, line by line. Note that if the original lyrics had blank
lines, you won't have these in
the reconstructed lyrics. Iterate through the array of words
using the enhanced for loop.
d. Write a method with the following signature:
public int count(HashMap<String,ArrayList<Integer>> map)
This method will return the total number of unique words in the
lyric by analyzing the map.
e. Write a method with the following signature:
public String
mostFrequentWord(HashMap<String,ArrayList<Integer>> map)
This method will return the word that occurs most frequently in
the lyric. Do this by getting
a set of all the words in the map and then for each word, find
the one that has the largest set
of word positions. Use the enhanced for loop in your solution.
Do not create any
additional data structures to solve this problem. If there is a tie
for the most frequent word,
print out the most frequent word that comes first alphabetically.
2. Complete the main method that reads the file Minimal.txt.
The text file contains the lyrics for a pop
song. You may assume that the files have all punctuation
filtered out (except for apostrophes), but
there may be uppercase and lowercase letters mixed together.
For example, the string "Don't" is
considered one word. Some lines of the file may also be empty.
If the file is not found, exit the
program with a warning message.
Once you open the text file, read each word from the file one at
a time, convert the word to
uppercase, and insert the word into the map along with its word
position using your add method.
After you add all words to the map, display the word/position
list using your displayWords
method.
Next, reconstruct and display the lyrics using your
displayLyrics method.
Then, display the total number of unique words in the lyrics by
using your count method.
Finally, output the word that occurs most frequently using your
mostFrequentWord method.

More Related Content

More from whittemorelucilla

Database reports provide us with the ability to further analyze ou.docx
Database reports provide us with the ability to further analyze ou.docxDatabase reports provide us with the ability to further analyze ou.docx
Database reports provide us with the ability to further analyze ou.docx
whittemorelucilla
 
DataInformationKnowledge1.  Discuss the relationship between.docx
DataInformationKnowledge1.  Discuss the relationship between.docxDataInformationKnowledge1.  Discuss the relationship between.docx
DataInformationKnowledge1.  Discuss the relationship between.docx
whittemorelucilla
 
DataHole 12 Score6757555455555455575775655565656555655656556566643.docx
DataHole 12 Score6757555455555455575775655565656555655656556566643.docxDataHole 12 Score6757555455555455575775655565656555655656556566643.docx
DataHole 12 Score6757555455555455575775655565656555655656556566643.docx
whittemorelucilla
 
DataDestination PalletsTotal CasesCases redCases whiteCases organi.docx
DataDestination PalletsTotal CasesCases redCases whiteCases organi.docxDataDestination PalletsTotal CasesCases redCases whiteCases organi.docx
DataDestination PalletsTotal CasesCases redCases whiteCases organi.docx
whittemorelucilla
 
DataIllinois Tool WorksConsolidated Statement of Income($ in milli.docx
DataIllinois Tool WorksConsolidated Statement of Income($ in milli.docxDataIllinois Tool WorksConsolidated Statement of Income($ in milli.docx
DataIllinois Tool WorksConsolidated Statement of Income($ in milli.docx
whittemorelucilla
 
DataIDSalaryCompa-ratioMidpoint AgePerformance RatingServiceGender.docx
DataIDSalaryCompa-ratioMidpoint AgePerformance RatingServiceGender.docxDataIDSalaryCompa-ratioMidpoint AgePerformance RatingServiceGender.docx
DataIDSalaryCompa-ratioMidpoint AgePerformance RatingServiceGender.docx
whittemorelucilla
 
DataCity1997 Median Price1997 Change1998 Forecast1993-98 Annualize.docx
DataCity1997 Median Price1997 Change1998 Forecast1993-98 Annualize.docxDataCity1997 Median Price1997 Change1998 Forecast1993-98 Annualize.docx
DataCity1997 Median Price1997 Change1998 Forecast1993-98 Annualize.docx
whittemorelucilla
 
DataClientRoom QualityFood QualityService Quality1GPG2GGG3GGG4GPG5.docx
DataClientRoom QualityFood QualityService Quality1GPG2GGG3GGG4GPG5.docxDataClientRoom QualityFood QualityService Quality1GPG2GGG3GGG4GPG5.docx
DataClientRoom QualityFood QualityService Quality1GPG2GGG3GGG4GPG5.docx
whittemorelucilla
 
Database Project CharterBusiness CaseKhalia HartUnive.docx
Database Project CharterBusiness CaseKhalia HartUnive.docxDatabase Project CharterBusiness CaseKhalia HartUnive.docx
Database Project CharterBusiness CaseKhalia HartUnive.docx
whittemorelucilla
 
Databases selected Multiple databases...Full Text (1223 .docx
Databases selected Multiple databases...Full Text (1223  .docxDatabases selected Multiple databases...Full Text (1223  .docx
Databases selected Multiple databases...Full Text (1223 .docx
whittemorelucilla
 
Database SystemsDesign, Implementation, and ManagementCo.docx
Database SystemsDesign, Implementation, and ManagementCo.docxDatabase SystemsDesign, Implementation, and ManagementCo.docx
Database SystemsDesign, Implementation, and ManagementCo.docx
whittemorelucilla
 
DATABASE SYSTEMS DEVELOPMENT & IMPLEMENTATION PLAN1DATABASE SYS.docx
DATABASE SYSTEMS DEVELOPMENT & IMPLEMENTATION PLAN1DATABASE SYS.docxDATABASE SYSTEMS DEVELOPMENT & IMPLEMENTATION PLAN1DATABASE SYS.docx
DATABASE SYSTEMS DEVELOPMENT & IMPLEMENTATION PLAN1DATABASE SYS.docx
whittemorelucilla
 
Database Security Assessment Transcript You are a contracting office.docx
Database Security Assessment Transcript You are a contracting office.docxDatabase Security Assessment Transcript You are a contracting office.docx
Database Security Assessment Transcript You are a contracting office.docx
whittemorelucilla
 
Data.docx
Data.docxData.docx
Database Design Mid Term ExamSpring 2020Name ________________.docx
Database Design Mid Term ExamSpring 2020Name ________________.docxDatabase Design Mid Term ExamSpring 2020Name ________________.docx
Database Design Mid Term ExamSpring 2020Name ________________.docx
whittemorelucilla
 
Database Justification MemoCreate a 1-page memo for the .docx
Database Justification MemoCreate a 1-page memo for the .docxDatabase Justification MemoCreate a 1-page memo for the .docx
Database Justification MemoCreate a 1-page memo for the .docx
whittemorelucilla
 
Database Concept Maphttpwikieducator.orgCCNCCCN.docx
Database Concept Maphttpwikieducator.orgCCNCCCN.docxDatabase Concept Maphttpwikieducator.orgCCNCCCN.docx
Database Concept Maphttpwikieducator.orgCCNCCCN.docx
whittemorelucilla
 
Database Dump Script(Details of project in file)Mac1) O.docx
Database Dump Script(Details of project in file)Mac1) O.docxDatabase Dump Script(Details of project in file)Mac1) O.docx
Database Dump Script(Details of project in file)Mac1) O.docx
whittemorelucilla
 
Database Design 1. What is a data model A. method of sto.docx
Database Design 1.  What is a data model A. method of sto.docxDatabase Design 1.  What is a data model A. method of sto.docx
Database Design 1. What is a data model A. method of sto.docx
whittemorelucilla
 
DataAGEGENDERETHNICMAJORSEMHOUSEGPAHRSNEWSPAPTVHRSSLEEPWEIGHTHEIGH.docx
DataAGEGENDERETHNICMAJORSEMHOUSEGPAHRSNEWSPAPTVHRSSLEEPWEIGHTHEIGH.docxDataAGEGENDERETHNICMAJORSEMHOUSEGPAHRSNEWSPAPTVHRSSLEEPWEIGHTHEIGH.docx
DataAGEGENDERETHNICMAJORSEMHOUSEGPAHRSNEWSPAPTVHRSSLEEPWEIGHTHEIGH.docx
whittemorelucilla
 

More from whittemorelucilla (20)

Database reports provide us with the ability to further analyze ou.docx
Database reports provide us with the ability to further analyze ou.docxDatabase reports provide us with the ability to further analyze ou.docx
Database reports provide us with the ability to further analyze ou.docx
 
DataInformationKnowledge1.  Discuss the relationship between.docx
DataInformationKnowledge1.  Discuss the relationship between.docxDataInformationKnowledge1.  Discuss the relationship between.docx
DataInformationKnowledge1.  Discuss the relationship between.docx
 
DataHole 12 Score6757555455555455575775655565656555655656556566643.docx
DataHole 12 Score6757555455555455575775655565656555655656556566643.docxDataHole 12 Score6757555455555455575775655565656555655656556566643.docx
DataHole 12 Score6757555455555455575775655565656555655656556566643.docx
 
DataDestination PalletsTotal CasesCases redCases whiteCases organi.docx
DataDestination PalletsTotal CasesCases redCases whiteCases organi.docxDataDestination PalletsTotal CasesCases redCases whiteCases organi.docx
DataDestination PalletsTotal CasesCases redCases whiteCases organi.docx
 
DataIllinois Tool WorksConsolidated Statement of Income($ in milli.docx
DataIllinois Tool WorksConsolidated Statement of Income($ in milli.docxDataIllinois Tool WorksConsolidated Statement of Income($ in milli.docx
DataIllinois Tool WorksConsolidated Statement of Income($ in milli.docx
 
DataIDSalaryCompa-ratioMidpoint AgePerformance RatingServiceGender.docx
DataIDSalaryCompa-ratioMidpoint AgePerformance RatingServiceGender.docxDataIDSalaryCompa-ratioMidpoint AgePerformance RatingServiceGender.docx
DataIDSalaryCompa-ratioMidpoint AgePerformance RatingServiceGender.docx
 
DataCity1997 Median Price1997 Change1998 Forecast1993-98 Annualize.docx
DataCity1997 Median Price1997 Change1998 Forecast1993-98 Annualize.docxDataCity1997 Median Price1997 Change1998 Forecast1993-98 Annualize.docx
DataCity1997 Median Price1997 Change1998 Forecast1993-98 Annualize.docx
 
DataClientRoom QualityFood QualityService Quality1GPG2GGG3GGG4GPG5.docx
DataClientRoom QualityFood QualityService Quality1GPG2GGG3GGG4GPG5.docxDataClientRoom QualityFood QualityService Quality1GPG2GGG3GGG4GPG5.docx
DataClientRoom QualityFood QualityService Quality1GPG2GGG3GGG4GPG5.docx
 
Database Project CharterBusiness CaseKhalia HartUnive.docx
Database Project CharterBusiness CaseKhalia HartUnive.docxDatabase Project CharterBusiness CaseKhalia HartUnive.docx
Database Project CharterBusiness CaseKhalia HartUnive.docx
 
Databases selected Multiple databases...Full Text (1223 .docx
Databases selected Multiple databases...Full Text (1223  .docxDatabases selected Multiple databases...Full Text (1223  .docx
Databases selected Multiple databases...Full Text (1223 .docx
 
Database SystemsDesign, Implementation, and ManagementCo.docx
Database SystemsDesign, Implementation, and ManagementCo.docxDatabase SystemsDesign, Implementation, and ManagementCo.docx
Database SystemsDesign, Implementation, and ManagementCo.docx
 
DATABASE SYSTEMS DEVELOPMENT & IMPLEMENTATION PLAN1DATABASE SYS.docx
DATABASE SYSTEMS DEVELOPMENT & IMPLEMENTATION PLAN1DATABASE SYS.docxDATABASE SYSTEMS DEVELOPMENT & IMPLEMENTATION PLAN1DATABASE SYS.docx
DATABASE SYSTEMS DEVELOPMENT & IMPLEMENTATION PLAN1DATABASE SYS.docx
 
Database Security Assessment Transcript You are a contracting office.docx
Database Security Assessment Transcript You are a contracting office.docxDatabase Security Assessment Transcript You are a contracting office.docx
Database Security Assessment Transcript You are a contracting office.docx
 
Data.docx
Data.docxData.docx
Data.docx
 
Database Design Mid Term ExamSpring 2020Name ________________.docx
Database Design Mid Term ExamSpring 2020Name ________________.docxDatabase Design Mid Term ExamSpring 2020Name ________________.docx
Database Design Mid Term ExamSpring 2020Name ________________.docx
 
Database Justification MemoCreate a 1-page memo for the .docx
Database Justification MemoCreate a 1-page memo for the .docxDatabase Justification MemoCreate a 1-page memo for the .docx
Database Justification MemoCreate a 1-page memo for the .docx
 
Database Concept Maphttpwikieducator.orgCCNCCCN.docx
Database Concept Maphttpwikieducator.orgCCNCCCN.docxDatabase Concept Maphttpwikieducator.orgCCNCCCN.docx
Database Concept Maphttpwikieducator.orgCCNCCCN.docx
 
Database Dump Script(Details of project in file)Mac1) O.docx
Database Dump Script(Details of project in file)Mac1) O.docxDatabase Dump Script(Details of project in file)Mac1) O.docx
Database Dump Script(Details of project in file)Mac1) O.docx
 
Database Design 1. What is a data model A. method of sto.docx
Database Design 1.  What is a data model A. method of sto.docxDatabase Design 1.  What is a data model A. method of sto.docx
Database Design 1. What is a data model A. method of sto.docx
 
DataAGEGENDERETHNICMAJORSEMHOUSEGPAHRSNEWSPAPTVHRSSLEEPWEIGHTHEIGH.docx
DataAGEGENDERETHNICMAJORSEMHOUSEGPAHRSNEWSPAPTVHRSSLEEPWEIGHTHEIGH.docxDataAGEGENDERETHNICMAJORSEMHOUSEGPAHRSNEWSPAPTVHRSSLEEPWEIGHTHEIGH.docx
DataAGEGENDERETHNICMAJORSEMHOUSEGPAHRSNEWSPAPTVHRSSLEEPWEIGHTHEIGH.docx
 

Recently uploaded

Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
 
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGHKHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
shreyassri1208
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
Celine George
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
indexPub
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
heathfieldcps1
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
giancarloi8888
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 

Recently uploaded (20)

Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
 
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGHKHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 

Data Structure and Algorithm Design CSCI 3000 Name D.docx

  • 1. Data Structure and Algorithm Design CSCI 3000 Name: Date: Programming Final Credit – HashMap (Lyric Analyzer) Since the lyrics of most pop music contains words that repeat a number of times, a simple way to compress a lyric file is to create a map that stores each word once along with the positions of each word in the file. For example, suppose the lyric consists of the lines: What have I 1 2 3 <-- word position in lyrics What have I 4 5 6 <-- word position in lyrics What have I done to deserve this 7 8 9 10 11 12 13 <-- word position in lyrics We would form a map that maps each unique word to a list of word positions in the lyric. NOTE: The word position for a word at the end of a line is stored as a
  • 2. negative integer rather than a positive integer so you can recreate the lyrics later when you iterate through the words in the map. Sample map for the lyric above (order of words may vary): Word Word Position(s) =========================== WHAT 1, 4, 7 HAVE 2, 5, 8 I -3, -6, 9 DONE 10 TO 11 DESERVE 12 THIS -13 This project currently contains only one class that models the analyzer using a HashMap that maps a lyric word to a list of integers representing the locations of the word in the lyrics. 1. Complete the class for the lyric analyzer by adding the following methods listed below. a. Write an add method with the following signature: public void add(HashMap<String,ArrayList<Integer>> map)
  • 3. This method will determine if the given lyric word is in the map. If the word is not in the map, then a mapping is added that maps that word to a list containing the position of the word in the lyric. If the word is already in the map, then its word position is added to the list of word positions for this word. Do not create a new mapping if the lyric word is already in the map. Use the one that is already there and just update its list. Remember to negate the word position if the word is at the end of a line in the lyrics. b. Write a displayWords method with the following signature: public void displayWords(HashMap<String,ArrayList<Integer>> map) This method should display the words of the song along with the word positions of the word, one word per line, in alphabetical order. You should do this without creating another map. Instead, get a set of all the words stored in the map. Sort this set using one of the sort methods from the Java API. Then iterate over this sorted set and print out each word along with the word positions associated with each word. You may leave the negative integers in the word position list. (see sample output below)
  • 4. Iterate through the array of words using the enhanced for loop. c. Write a displayLyrics method with the following signature: public void displayLyrics(HashMap<String,ArrayList<Integer>> map) This method will display the lyrics for the song (in uppercase) stored in the map. Start with an empty array of strings whose size is the number of words in the lyric plus 1 (don't use cell 0). Initialize this array with empty strings (not null). Then, get a set of all of the words stored in the map. For each word, store the word in the array cells corresponding to its word position(s). If a word position is negative, add on an extra newline character to the word when you store the word in the array. Once you finish processing all words that are in the map, iterate through the array and print out each string, and you should see the lyrics appear, line by line. Note that if the original lyrics had blank lines, you won't have these in the reconstructed lyrics. Iterate through the array of words using the enhanced for loop.
  • 5. d. Write a method with the following signature: public int count(HashMap<String,ArrayList<Integer>> map) This method will return the total number of unique words in the lyric by analyzing the map. e. Write a method with the following signature: public String mostFrequentWord(HashMap<String,ArrayList<Integer>> map) This method will return the word that occurs most frequently in the lyric. Do this by getting a set of all the words in the map and then for each word, find the one that has the largest set of word positions. Use the enhanced for loop in your solution. Do not create any additional data structures to solve this problem. If there is a tie for the most frequent word, print out the most frequent word that comes first alphabetically. 2. Complete the main method that reads the file Minimal.txt. The text file contains the lyrics for a pop song. You may assume that the files have all punctuation filtered out (except for apostrophes), but there may be uppercase and lowercase letters mixed together. For example, the string "Don't" is considered one word. Some lines of the file may also be empty. If the file is not found, exit the
  • 6. program with a warning message. Once you open the text file, read each word from the file one at a time, convert the word to uppercase, and insert the word into the map along with its word position using your add method. After you add all words to the map, display the word/position list using your displayWords method. Next, reconstruct and display the lyrics using your displayLyrics method. Then, display the total number of unique words in the lyrics by using your count method. Finally, output the word that occurs most frequently using your mostFrequentWord method.