Question

In: Computer Science

Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                            &n

Code in Java

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:

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 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.
  • Do not use TreeSet (to show the differences between the Set and TreeSet that Set still remove the duplicate but won't sort the elements)

Solutions

Expert Solution

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);
        }


    }
}

=========================================================================


Related Solutions

In java: -Create a class named Animal
In java: -Create a class named Animal
This code in java: Create a class named car. A car has color, model, company, registration...
This code in java: Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car. Kindly add some other functionalities like refuel
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT