Question

In: Computer Science

Code in Java, Use both Set and TreeSet to show the differences between the Set and...

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

---------- Set -----------------

<.<>
<>

<.<>
<>

----------- TreeSet ------------

<.<>
<>

<.<>
<>

Where: <>=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 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

Solutions

Expert Solution

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


    }
}

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

Related Solutions

Code in Java, Use both Set and TreeSet in only one main to show the differences...
Code in Java, Use both Set and TreeSet in only one main 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                                                              - 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...
Write a java code that could be used to show the simulation of a tornado. use...
Write a java code that could be used to show the simulation of a tornado. use the understanding of the mix of low temperature and high temperature wind go create a spinning vortex. ***POSTED INCORRECT QUESTION** here is the real question: plz write a simple java code to show a spinning circle of particles.
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of       ...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of        * the comparator is to compare the alphabetic order of integers. With Q3,        * the order of Integer elements will be sort according to the order of        * digit Unicode.        * For example, the value of {11,3,31,2} will return {11,2,3,31}        * NOTE: You don't need to compare each digit one by one. Just compare them System.out.println("Q3...
Please create a Java Eclipse code and show output and explanation. MUST USE EXCEPTION! Step 1....
Please create a Java Eclipse code and show output and explanation. MUST USE EXCEPTION! Step 1. Ask users how much money was spent on an orange. Step 2. Then ask the user how many oranges they purchased Step 3. Lastly calculate the price for each orange purchased.
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
using this data set, use statkey to show if theres a correlation between age and alcohol...
using this data set, use statkey to show if theres a correlation between age and alcohol consumpion please? shown appropriate form of graphs and or calculations. Alcohol consumption key is 1= regular drinker. 2= not regular drinker Age 21 19 34 19 19 21 24 20 20 18 18 18 18 18 19 20 19 20 18 19 19 20 18 19 19 20 19 22 22 45 23 19 21 20 23 20 20 19 19 18 20 22...
using this data set, use statkey to show if theres a correlation between age and alcohol...
using this data set, use statkey to show if theres a correlation between age and alcohol consumpion please? shown appropriate form of graphs and or calculations. Alcohol consumption key is 1= regular drinker. 2= not regular drinker Age,   Alcohol 21, 2 19, 2 34, 2 19, 2 19, 2 21, 2 24, 2 20, 1 20, 2 18, 1 18, 2 18, 1 18, 1 18, 2 19, 2 20, 1 19, 2 20, 2 18, 1 19, 1 19,...
java code: adds a new regular task, delete a task , show all tasks, and show...
java code: adds a new regular task, delete a task , show all tasks, and show regular tasks, mark a task as important (possibly through ID), complete task, show all completed tasks, show important tasks. I also need a UML diagram for the code update: The "task" is like something in a to do list. example) task 1. name: get carrots important: no completed: yes. you can run my code as an example if needed
10. Use the model of aggregate demand and aggregate supply to show the differences between demand-pull...
10. Use the model of aggregate demand and aggregate supply to show the differences between demand-pull inflation and cost-push inflation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT