Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Module: Core Java Lab7: java.util package Create a class named DictionaryWord as

ID: 3626283 • Letter: M

Question

Module: Core Java
Lab7: java.util package

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
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

Ensure that there is no duplicate DictionaryWord objects (02 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:
•    class DictionaryWord implements Comparable to order 2 DictionaryWord objects.
•    override equals(..) method to compare 2 DictionaryWord objects.
•    override toString()
•    use Set to ensure no duplicate.
•    use support class Collections to sort 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());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote