Answered step by step
Verified Expert Solution
Link Copied!

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 2(60 marks)
This question explores the TreeMap and TreeSet 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 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.
a.Launch 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.
b.Write a zero-argument constructor that initialises anagrams to a new instance of TreeMap.
c.Add a public, zero-parameter, method called populate to the Anagram class. The method should put these two sample (key-value) 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.
d.Write a public, zero-parameter 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.
e.Write a method that checks whether its two parameters are anagrams of each other. The method has this header:
public boolean areAnagrams(String 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.
f.Write a method to add a new entry to the anagrams map with the header:
public void addAnagram(String 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.
g.Write a public, zero-parameter method called crossReference with no parameters or return value. The method will change the anagrams map so that each anagram is cross-referenced 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

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Development Of Knowledge Framework For Affective Content Analysis

Authors: Swarnangini Sinha

1st Edition

B0CQJ13WZ1, 979-8223977490

More Books

Students also viewed these Databases questions

Question

1. What makes Zipcar an attractive employer for whom to work?

Answered: 1 week ago