Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 2 ( 6 0 marks ) This question explores the TreeMap and TreeSet classes. You should be able to answer this question once you
Question marks
This question explores the TreeMap and TreeSet classes. You should be able to answer this question once you have completed Chapter
Assume that sensible arguments are received by methods, unless otherwise stated.
As you answer the question parts you should provide multiline comments for your class, constructor, and methods. Make sure that your comments explain the purpose of the code and identify which part of the question you are answering in each case.
Scenario
In this question you will build a simple map to hold words and their anagrams.
aLaunch BlueJ and begin by adding a new class called Anagram. Retain the class comment but edit it to include your own name and the date and a description of the class Anagram. Remove the sample code.
In the class Anagram add a field anagrams, which is a TreeMap. Make this field public, for testing purposes.
Each key of the map will be a String representing a word and each value will be a TreeSet of String objects representing valid anagrams of the key.
Include the appropriate import statements.
bWrite a zeroargument constructor that initialises anagrams to a new instance of TreeMap.
cAdd a public, zeroparameter, method called populate to the Anagram class. The method should put these two sample keyvalue entries into the map:
list silt slit
saps asps pass spas
The method does not return a value.
Remember that the keys are strings, and the values are sets of strings. The first sample key list is mapped to a set containing two strings and the second example key saps is mapped to a set containing three strings.
dWrite a public, zeroparameter method, called print to the Anagram class that prints out the map entries in the form shown in part c The method does not return a value.
As illustrated, each key in the anagrams map is to be followed by a space, a dash, a space and then the anagrams associated with the key. The simplest way to do this is to have a space after each anagram, including the last one.
Your method should continue to work after new entries are added to the anagrams map.
eWrite a method that checks whether its two parameters are anagrams of each other. The method has this header:
public boolean areAnagramsString aWord, String anotherWord
The method returns true if the two parameters are anagrams and false otherwise.
There are many ways of doing this, but one approach would be to create two lists containing the sorted characters of each parameter and then compare the lists for equality.
fWrite a method to add a new entry to the anagrams map with the header:
public void addAnagramString aWord, String anAnagram
If the two parameters are anagrams and aWord already exists as a key in the anagrams map, the set corresponding to the key is retrieved and anAnagram is added to it If aWord does not exist as a key in the map, a new map entry is created with aWord as the key, and a set containing the single string anAnagram as the value.
If the two parameters are not anagrams, no action is taken.
gWrite a public, zeroparameter method called crossReference with no parameters or return value. The method will change the anagrams map so that each anagram is crossreferenced with every other anagram.
For example, if printing the anagrams map before this method is executed gives:
list silt slit
saps asps pass spas
then after executing crossReference printing the anagrams map will give:
asps pass saps spas
list silt slit
pass asps saps spas
saps asps pass spas
silt list slit
slit list silt
spas asps pass saps
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