In: Computer Science
Code in Java
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:
<<no>.<<word>>
<<meanings>>
<<no>.<<word>>
<<meanings>>
Where: <<no>>=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. 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;
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); } } }
=========================================================================