Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Map Question 1 Overview Use HashMap to implement a word dictionary that stores the position of words in a paragraph. 2 Requirements Considering the

Java Map Question

1 Overview

Use HashMap to implement a word dictionary that stores the position of words in a paragraph.

2 Requirements

Considering the text below from a song, your program will store the positions of words in the text using a HashMap of the type Map>, where the words are the keys and the values are lists of position objects.

Again and again and again and again Do it again, do it again Again and again Its a shame, its a shame 
Its a perfect shame Creep under my door and we do it again, oh oh 

A position (x, y) specifies the position of a word as the xth sentence and yth word in a paragraph.

class Position { // x is line position, and y is word position in a line final int x,y; Position(int x, int y) { this.x = x; this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } 

}

For example, the positions of the word shame are (3,2), (3,5), and (4,3). Note that we always count from 0 and phrase like Its is treated as a single word. We ignore commas and periods.

class Dictionary { private Map> map = new HashMap<>(); 

1

 // add a word at position p to this dictionary void add(String word, Position p) { 

// TODO }

 // return the string representation of the dictionary public String toString() { 

// TODO }

 // return the most frequently used word String most() { 

// TODO }

 // return the paragraph that the dictionary is generated from // with the most frequently used word capitalized public String print() { 

// TODO }

}

You should implement four methods in the Dictionary class:

-add method adds a word at position p to the dictionary. We do not distinguish the same word with different cases. You should convert all words to lower cases first.-

-toString returns the string representation of the dictionary (see the sam- ple output).

-most method returns the most frequently used word. If there are more than one, then pick the first one.

-print method reconstructs the text that the dictionary is generated from except that all occurrences of the most frequently used word are all cap- italized (see the sample output). You dont have to recover commas or periods in the original text. All words are lower cases except the most frequently used word.

Driver Code:

package h;

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

public class H { public static void main(String[] args) { String lyrics = "Again and again and again and again "+ "Do it again, do it again "+ "Again and again "+ "It's a shame, it's a shame "+ "It's a perfect shame "+ "Creep under my door and we do it again, oh oh"; String[] sentences = lyrics.split(" "); List> words = new ArrayList<>(); for(String s : sentences) { String[] ws = s.split(" "); List l = new ArrayList<>(); words.add(l); for(String w: ws) { w = w.replace(",", "").replace(".", ""); l.add(w); } } Dictionary d = new Dictionary(); for(int i=0; i l = words.get(i); for(int j=0; j

class Position { // x is line position, and y is word position in a line final int x,y; Position(int x, int y) { this.x = x; this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } }

class Dictionary { private Map> map = new HashMap<>(); // add a word at position p to this dictionary void add(String word, Position p) { // TODO } // return the string representation of the dictionary public String toString() { // TODO } // return the most frequently used word String most() { // TODO } // return the paragraph that the dictionary is generated from // with the most frequently used word capitalized public String print() { // TODO } }

Sample output

The program should mimic the sample run using the provided driver.

a [(3,1), (3,4), (4,1)] shame [(3,2), (3,5), (4,3)] door [(5,3)] 

2

perfect [(4,2)] its [(3,0), (3,3), (4,0)] again [(0,0), (0,2), (0,4), (0,6), (1,2), (1,5), (2,0), (2,2), (5,8)] creep [(5,0)] do [(1,0), (1,3), (5,6)] it [(1,1), (1,4), (5,7)] my [(5,2)] we [(5,5)] and [(0,1), (0,3), (0,5), (2,1), (5,4)] oh [(5,9), (5,10)] under [(5,1)] 
most frequent word: again 
AGAIN and AGAIN and AGAIN and AGAIN do it AGAIN do it AGAIN AGAIN and AGAIN its a shame its a shame 
its a perfect shame creep under my door and we do it AGAIN oh oh 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions