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 program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
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...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT