Question

In: Computer Science

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

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. 

If you are satisfied with the solution, please leave a +ve feedback : ) 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;
import java.util.TreeSet;
public class TestWordDictionary {

    public static void main(String[] args) {


        System.out.println("---------- Set -----------------");
        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);
        }

        System.out.println("---------- Tree Set -----------------");
        TreeSet<DictionaryWord> treeSet = new TreeSet<>();
        treeSet.add(new DictionaryWord("bank robber", "Steals money from a bank"));
        treeSet.add(new DictionaryWord("burglar", "Breaks into a home to steal things"));
        treeSet.add(new DictionaryWord("forger", "Makes an illegal copy of something"));
        treeSet.add(new DictionaryWord("hacker", "Breaks into a computer system"));
        treeSet.add(new DictionaryWord("hijacker", "Takes control of an airplane"));
        treeSet.add(new DictionaryWord("kidnapper", "Holds someone for ransom money"));
        treeSet.add(new DictionaryWord("mugger", "Attacks and steals money from someone"));
        treeSet.add(new DictionaryWord("murderer", "Kills another person"));
        wordNum = 1;
        for(DictionaryWord word: treeSet){

            System.out.println(wordNum+++". "+word);
        }


    }
}

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


Related Solutions

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...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count = 0; double listPrice; String...
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.
write this java code: One statistical operation that is sometimes performed on a set of data...
write this java code: One statistical operation that is sometimes performed on a set of data values is to remove values that are far from the average. Write a program that reads real values from a text file, one per line. Store the data values as Double objects in an instance of the class java.util.ArrayList. Then Use an iterator to compute the average and standard deviation of the values. Display these results. Use a second iterator to remove any value...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use a hash...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use a hash table. You can use either Java HashTable or Java HashMap. Refer to Java API for the subtle differences between HashTable and HashMap. Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion "...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion " /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to read integers from the keyboard with the method nextInt. Use a try catch block to capture non-integer input, and tell the user, "This is not an Integer". The program loop should end when the user enters the string "quit". (Hint: "quit" is clearly not a number and will be captured in the try / catch block.) A typical Output Dialog is shown below: Enter...
Show a complete graph for both of these shocks. You will need two graphs only, one...
Show a complete graph for both of these shocks. You will need two graphs only, one for each shock. The graphs have to show: -The new short run equilibrium immediately after the shock occurs. -The new long run equilibrium. -The transition to the long run equilibrium. Properly label your axes and the curves in your graphs, and use arrows to show shifts of curves and the change in the price level and output. The two shocks are: The stock market...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT