Question

Need Help Using Java programming only import java.util.ArrayList; //Import anything you need here public class Dictionary...

Need Help

Using Java programming only

import java.util.ArrayList;
//Import anything you need here

public class Dictionary {
    // Declare any variables you feel necessary here

    public Dictionary(/*DO NOT PUT ANY PARAMETERS HERE!*/) {
        // Initialise those variables
    }

    /***
     * @return number of terms currently in the dictionary hint: starts at 0 when
     *         there are no terms
     */
    public int numberOfTerms() {
        return 0;
    }

    /***
     * Add a Term to your dictionary, given a word and a definition, store them for
     * later use in your dictionary
     *
     * @param word word portion of Term
     * @param definition definition portion of Term
     *
     */
    public void addTerm(String word, String definition) {
    }

    /**
     * Implement functionality to retrieve a random term from your dictionary. This
     * could be used for a word of the day on a website.
     *
     * @return the random term
     */
    public Term getRandomTerm() {
        return null;
    }

    /**
     * Given a partial search string, check if any of the words you have stored match the given search string.
     *
     * If your dictionary contained Terms: apple, pineapple, paper, master.
     *
     * If search contained "ap" the results should be an ArrayList containing: apple, pineapple, paper.
     *
     * @param search string
     * @return An ArrayList of Strings containing the results
     */
    public ArrayList<String> searchForWord(String search) {
        return null;
    }


    /**
     * Given a partial search string, check if any of the words you have stored start with a given character.
     * 'p'
     * If your dictionary contained Terms: apple, pineapple, paper, master.
     *
     * If c contained 'p' the results should be an ArrayList containing: pineapple, paper.
     *
     * @param c character that begins words
     * @return An ArrayList of Strings containing the results
     */
    public ArrayList<String> searchForWordStartsWith(char c) {
        return null;
    }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, for the problem you need to implement one more class called Term.java. as the method getRandomTerm () returns and object of Term.java class.

Here are the Term.java class and also the implemented methods of the Dictionary.java, we have used HashMap ADT to store the words and their corresponding meaning. Words will form the key while the meaning will be the value.

Let me know for any changes or for any further help. Thank You !

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

public class Term {

    private String word;
    private String definition;

    public Term(String word, String definition) {
        this.word = word;
        this.definition = definition;
    }

    public String getWord() {
        return word;
    }

    public String getDefinition() {
        return definition;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public void setDefinition(String definition) {
        this.definition = definition;
    }
}

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

import java.util.*;

import java.util.ArrayList;

//Import anything you need here



public class Dictionary {

    // Declare any variables you feel necessary here

    private HashMap<String, String> map;



    public Dictionary(/*DO NOT PUT ANY PARAMETERS HERE!*/) {

        // Initialise those variables

        map = new HashMap<>();

    }



    /***

     * @return number of terms currently in the dictionary hint: starts at 0 when

     *         there are no terms

     */

    public int numberOfTerms() {

        return map.size();

    }



    /***

     * Add a Term to your dictionary, given a word and a definition, store them for

     * later use in your dictionary

     *

     * @param word word portion of Term

     * @param definition definition portion of Term

     *

     */

    public void addTerm(String word, String definition) {



        map.put(word, definition);

        System.out.println("Added to dictionary successfully.");



    }



    /**

     * Implement functionality to retrieve a random term from your dictionary. This

     * could be used for a word of the day on a website.

     *

     * @return the random term

     */

    public Term getRandomTerm() {

        Random random = new Random();

        int randNumber = random.nextInt(map.size());



        int i = 0;

        for (Map.Entry<String, String> iter : map.entrySet()) {

            if (i++ == randNumber - 1) {

                return new Term(iter.getKey(), iter.getValue());

            }



        }

        return null;

    }



    /**

     * Given a partial search string, check if any of the words you have stored match the given search string.

     * <p>

     * If your dictionary contained Terms: apple, pineapple, paper, master.

     * <p>

     * If search contained "ap" the results should be an ArrayList containing: apple, pineapple, paper.

     *

     * @param search string

     * @return An ArrayList of Strings containing the results

     */

    public ArrayList<String> searchForWord(String search) {



        ArrayList<String> matches = new ArrayList<>();

        for (String key : map.keySet()) {

            if (key.contains(search)) {

                matches.add(key);

            }

        }



        return matches;



    }





    /**

     * Given a partial search string, check if any of the words you have stored start with a given character.

     * 'p'

     * If your dictionary contained Terms: apple, pineapple, paper, master.

     * <p>

     * If c contained 'p' the results should be an ArrayList containing: pineapple, paper.

     *

     * @param c character that begins words

     * @return An ArrayList of Strings containing the results

     */

    public ArrayList<String> searchForWordStartsWith(char c) {



        ArrayList<String> matches = new ArrayList<>();

        for (String key : map.keySet()) {

            if (key.length() > 0 && key.charAt(0) == c) {

                matches.add(key);

            }

        }



        return matches;





    }



}
Add a comment
Know the answer?
Add Answer to:
Need Help Using Java programming only import java.util.ArrayList; //Import anything you need here public class Dictionary...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT