Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class Thesarus private HashMap synonyms = new HashMap (); /** * Constructor for objects of class Thesarus */ public Thesarus() { synonyms =
public class Thesarus private HashMap synonyms = new HashMap (); /** * Constructor for objects of class Thesarus */ public Thesarus() { synonyms = new HashMap (); } /** */ public HashMap getThesaurus() { { return synonyms; /** * */ public void populate() { String key = "happy"; HashSet valuesOne = new HashSet (); valuesOne.add("joyful"); valuesOne.add("contented"); valuesOne.add("cheerful"); saved Errors: 22 /** * */ public void populate () { String key = "happy"; HashSet valuesOne = new HashSet (); valuesOne.add("joyful"); valuesOne.add("contented"); valuesOne.add("cheerful"); synonyms.put(key, valuesOne); key = "angry"; valuesOne = new HashSet (); valuesOne.add("annoyed"); valuesOne.add("vexed"); synonyms.put(key, valuesOne); } public void addSynonyms (String aWord, String aSynonym) { if (synonyms.containsKey(aWord)) else { synonyms.get(aWord).add(aSynonym); HashSet valuesOne = new HashSet (); valuesOne.add(aSynonym); synonyms.putIfAbsent (aWord, valuesOne); } public void replaceWord (String aWord, String aSentence) { for (String synonym: synonyms.get(aWord)) { String phrase = aSentence; System.out.println(phrase.replace(aWord, synonym)); } public static void main(String[] args) { Thesaurus one = new Thesaurus(); one. populate(); one.replaceWord("happy", "A policeman's lot is not a happy one"); } } Question 2 (55 marks) This question explores the HashMap and HashSet classes. You should be able to answer this question once you have completed Chapter 6. Assume that sensible arguments are received by methods, unless otherwise stated. As you complete each part remember to paste the code you have written for it into your Solution Document. Scenario A thesaurus gives you a list of alternative words that mean the same as a given word (such a word is called a synonym). So, for example, if you look up happy in a thesaurus you might find the synonyms joyful, contented, cheerful. In this question you will build a simple thesaurus and use it to create new sentences by replacing a given word by its synonyms. You will then add more entries to your thesaurus by cross referencing the existing entries. So, for example, if you look up joyful, you should find the synonyms happy, contented, cheerful. a. Launch BlueJ and create a new project TMA02_Q2_SolXX, where XX should be replaced by your own initials, in the TMA02Download folder you unzipped earlier. Begin by adding a new class called Thesaurus. Retain the class comment but edit it to include your own name and the date and a description of the class Thesaurus. Remove the sample code. (5 marks) b. In the class Thesaurus add one private field, synonyms, which is a HashMap. Each key of the map is a String object, and each value is a HashSet containing String objects. (5 marks)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started