In: Computer Science
Code in Java, Use both Set and TreeSet in only one main to show the differences between the Set and TreeSet that Set still remove the duplicate but won't sort the elements.
Create a class named DictionaryWord as:
DictionaryWord |
- word: String
|
+ DictionaryWord (String word, String meanings) |
Write a program with the following requirements:
Creates 8 DictionaryWord objects with:
word |
meanings |
bank robber |
Steals money from a bank |
burglar |
Breaks into a home to steal things |
forger |
Makes an illegal copy of something |
hacker |
Breaks into a computer system |
hijacker |
Takes control of an airplane |
kidnapper |
Holds someone for ransom money |
mugger |
Attacks and steals money from someone |
murderer |
Kills another person |
Displays all DictionaryWord with the format as:
---------- Set -----------------
<.<>
<>
<.<>
<>
----------- TreeSet ------------
<.<>
<>
<.<>
<>
Where: <>=1,2…
Hint:
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== public class DictionaryWord implements Comparable<DictionaryWord> { private String word; private String meanings; public DictionaryWord(String word, String meanings) { this.word = word; this.meanings = meanings; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public String getMeanings() { return meanings; } public void setMeanings(String meanings) { this.meanings = meanings; } @Override public int compareTo(DictionaryWord o) { return word.compareTo(o.word); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DictionaryWord that = (DictionaryWord) o; return word.equals(that.word); } @Override public String toString() { return word + "\n" + meanings; } }
=====================================================================
import java.util.HashSet; import java.util.Set; import java.util.TreeSet;
public class TestWordDictionary { public static void main(String[] args) { System.out.println("---------- Set -----------------"); Set<DictionaryWord> words = new HashSet<>(); words.add(new DictionaryWord("bank robber", "Steals money from a bank")); words.add(new DictionaryWord("burglar", "Breaks into a home to steal things")); words.add(new DictionaryWord("forger", "Makes an illegal copy of something")); words.add(new DictionaryWord("hacker", "Breaks into a computer system")); words.add(new DictionaryWord("hijacker", "Takes control of an airplane")); words.add(new DictionaryWord("kidnapper", "Holds someone for ransom money")); words.add(new DictionaryWord("mugger", "Attacks and steals money from someone")); words.add(new DictionaryWord("murderer", "Kills another person")); int wordNum = 1; for(DictionaryWord word: words){ System.out.println(wordNum+++". "+word); } System.out.println("---------- Tree Set -----------------"); TreeSet<DictionaryWord> treeSet = new TreeSet<>(); treeSet.add(new DictionaryWord("bank robber", "Steals money from a bank")); treeSet.add(new DictionaryWord("burglar", "Breaks into a home to steal things")); treeSet.add(new DictionaryWord("forger", "Makes an illegal copy of something")); treeSet.add(new DictionaryWord("hacker", "Breaks into a computer system")); treeSet.add(new DictionaryWord("hijacker", "Takes control of an airplane")); treeSet.add(new DictionaryWord("kidnapper", "Holds someone for ransom money")); treeSet.add(new DictionaryWord("mugger", "Attacks and steals money from someone")); treeSet.add(new DictionaryWord("murderer", "Kills another person")); wordNum = 1; for(DictionaryWord word: treeSet){ System.out.println(wordNum+++". "+word); } } }
=====================================================================