In: Computer Science
Code in Java, Use both Set and TreeSet 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:
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR
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;
public class TestWordDictionary {
public static void main(String[] args) {
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);
}
}
}
============