Create a class named DictionaryWord as: DictionaryWord - word: String - meanings
ID: 3626391 • Letter: C
Question
Create a class named DictionaryWord as:
DictionaryWord
- word: String
- meanings: String
+ DictionaryWord (String word, String meanings)
+ getWord(): String
+ setWord (String word): void
+ getMeanings(): String
+ setMeanings(String meanings): void
Write a program with the following requirements:
Creates 8 DictionaryWord objects with:
? Word and meanings as the table bellow:
word Meanings
murderer Kills another person
kidnapper Holds someone for ransom money
burglar Breaks into a home to steal things
forger Makes an illegal copy of something
hacker Breaks into a computer system
mugger Attacks and steals money from someone
bank robber Steals money from a bank
hijacker Takes control of an airplane
? Ensure that there is no duplicate DictionaryWord objects (2 DictionaryWord objects a and b are equal when a.word=b.word).
Displays all DictionaryWord in ascending order of word with the format as:
<<no>.<<word>>
<<meanings>>
<<no>.<<word>>
<<meanings>>
Where: <<no>>=1,2…
Hint:
• Using TreeSet to guarantee no duplication and objects are stored in sorted order
• Class DictionaryWord implements Comparable to order 2 DictionaryWord objects.
Explanation / Answer
// Create a class named DictionaryWord as:
public class DictionaryWord implements Comparable<DictionaryWord>
{
// - word: String
private String word;
// - meanings: String
private String meanings;
// + DictionaryWord (String word, String meanings)
public DictionaryWord(String word, String meanings)
{
this.word = word;
this.meanings = meanings;
}
// + getWord(): String
public String getWord()
{
return word;
}
// + setWord (String word): void
public void setWord(String word)
{
this.word = word;
}
// + getMeanings(): String
public String getMeanings()
{
return meanings;
}
// + setMeanings(String meanings): void
public void setMeanings(String meanings)
{
this.meanings = meanings;
}
// override equals(..) method to compare 2 DictionaryWord objects.
public boolean equals(Object o)
{
DictionaryWord rhs = (DictionaryWord)o;
return rhs.word.equals(word) && rhs.meanings.equals(meanings);
}
// class DictionaryWord implements Comparable to order 2 DictionaryWord objects.
public int compareTo(DictionaryWord rhs)
{
reurn word.compareTo(rhs.word);
}
// override toString()
public String toString()
{
return word+" "+meanings;
}
}
import java.util.*;
public class Test
{
public static void main(String[] args)
{
// use Set to ensure no duplicate.
Set<DictionaryWord> set = new TreeSet<DictionaryWord>();
// Creates 8 DictionaryWord objects with: Word and meanings as the table bellow:
set.add(new DictionaryWord("bank robber", "Steals money from a bank"));
set.add(new DictionaryWord("burglar", "Breaks into a home to steal things"));
set.add(new DictionaryWord("forger", "Makes an illegal copy of something");
set.add(new DictionaryWord("hacker", "Breaks into a computer system"));
set.add(new DictionaryWord("hijacker", "Takes control of an airplane"));
set.add(new DictionaryWord("kidnapper", "Holds someone for ransom money"));
set.add(new DictionaryWord("mugger", "Attacks and steals money from someone"));
set.add(new DictionaryWord("murderer", "Kills another person"));
// Displays all DictionaryWord in ascending order of word
int loc = 1;
for(DictionaryWord d : set)
System.out.println((loc++)+"."+d.getWord()+" "+d.getMeanings());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.