Java Map Question. I have everything done except for the print method, which I a
ID: 3700608 • Letter: J
Question
Java Map Question. I have everything done except for the print method, which I am having trouble with. Is there any way to do that without the Comparable interface?
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<String, List<Position>>, where the words are the keys and the values are lists of position objects.
A position (x, y) specifies the position of a word as the xth sentence and yth word in a paragraph.
}
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 It’s is treated as a single word. We ignore commas and periods.
1
// TODO }
// TODO }
// TODO }
// 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 don’t 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<List<String>> words = new ArrayList<>();
for(String s : sentences) {
String[] ws = s.split(" ");
List<String> 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<words.size(); i++) {
List<String> l = words.get(i);
for(int j=0; j<l.size(); j++) {
d.add(l.get(j), new Position(i, j));
}
}
System.out.println(d);
System.out.println("most frequent word: " + d.most() + " ");
System.out.println(d.print());
}
}
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<String, List<Position>> 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.
2
Explanation / Answer
here is your program : ----------------->>>>>>>>>>>>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
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<List<String>> words = new ArrayList<>();
for(String s : sentences) {
String[] ws = s.split(" ");
List<String> 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<words.size(); i++) {
List<String> l = words.get(i);
for(int j=0; j<l.size(); j++) {
d.add(l.get(j), new Position(i, j));
}
}
System.out.println(d);
System.out.println("most frequent word: " + d.most() + " ");
System.out.println(d.print());
}
}
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 + ")"; }
public int getX(){
return x;
}
public int getY(){
return y;
}
}
class Dictionary {
private Map<String, List<Position>> map = new HashMap<>();
// add a word at position p to this dictionary
void add(String word, Position p) {
// TODO
if(map.containsKey(word)){
List<Position> temp = map.get(word);
temp.add(p);
map.replace(word,temp);
}else{
List<Position> temp = new ArrayList<>();
temp.add(p);
map.put(word,temp);
}
}
// return the string representation of the dictionary
public String toString() {
// TODO
String s1 = "";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
s1 = s1 + pair.getKey().toString()+" "+pair.getValue().toString()+" ";
}
return s1;
}
// return the most frequently used word
String most() {
// TODO
String s1 = "";
int n = 0;
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if(n < ((List<Position>)(pair.getValue())).size()){
n = ((List<Position>)(pair.getValue())).size();
s1 = (String)pair.getKey();
}
}
return s1;
}
// return the paragraph that the dictionary is generated from
// with the most frequently used word capitalized
public String print() {
// TODO
String s1 = "";
ArrayList<String[]> words = new ArrayList<>(map.size());
for(int i = 0;i<map.size();i++){
String[] temp = new String[20];
words.add(temp);
}
int n = 0;
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
List<Position> temp = ((ArrayList<Position>)(pair.getValue()));
n = temp.size();
for(int i = 0;i<n;i++){
Position tt = temp.get(i);
words.get(tt.getX())[tt.getY()] = new String((String)pair.getKey());
}
}
for(int i = 0;i<map.size();i++){
String[] str = words.get(i);
for(int j = 0;j<str.length;j++){
if(str[j] != null){
s1 = s1 + str[j] + " ";
}
}
s1 = s1 + " ";
}
return s1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.