GWU

CSCI 1111

Introduction to Software Development, Spring 2022

GWU Computer Science


Homework 6

This homework will be a little different than your other homeworks, as you will be practicing writing your own class that will meet a set of specifications.

In this homework, you will be writing a class in Java that will act as a dictionary. It will store a word, along with its associated meaning. We'll also write a bunch of methods to operate on words in this dictionary. You should create a file called Dictionary.java and declare a public class called Dictionary in that file.

Note: you may have come across dictionaries in a language like python, or you may even be familiar with a HashMap object in Java; these concepts are useful, but for this assignment, we're going to stick with writing on own dictionary from scratch.

Lists to store words and their meanings

A dictionary entry will typically contain a word, followed by its definition. There may also be other information, such as parts-of-speech and pronounciation. For this homework, we're going to associate each word in the dictionary with a definition, and a part-of-speech (represented by a char).

In your Dictionary class, create three arrays to store a word and its definition, and its part of speech. Initially, our dictionaries will be empty, but you can initialize these arrays to be able to hold 10 enties. Override the default constructor of the Dictionary class to create these three lists with appropriate variable names.

Next, write a public String toString() method that would, when passed to a print statement, print out the word, part of speech, and definition for all the words in the dictionary, using whatever exists in these lists. Each entry should be on its own line, with each component separated by a tab.

Finally, write getters for the three fields of the class.

Adding an entry to the dictionary

Next, write a method with the signature public boolean addWord(String, String, char) that adds the new entry into the same place for each of the three lists. The method should return true if there was space, or false if the dictionary was already full. Hint: you will probably want to add a counter variable to keep track of the next available index into the dictionary. Words should be added to your dictionary in the order this method is called; your dictionary should NOT store them in alphabetical order.

Searching for words

Write a method with the signature public int findWord(String, char) that searches all the entries in the dictionary, and returns the index of the first word it finds where the String argument to the method matches the word, and the character argument to the method matches the part of speech. If you cannot find the word, your method should return -1.

Write a method with the signature public String findWord(String) that searches all the entries in the dictionary, and returns the first word it finds where the argument to the method is a substring (or full match) to the word in the dictionary. If no matching words are found, your method should return null.

Creating dictionary objects

Now that we can make dictionaries, write a method with the signature public static Dictionary[] makeDicts() where you will create two Dictionary objects, one to hold English definitions, and one to hold Spanish definitions. Populate each dictionary with the entries for the words {apple, banana, tree, house, cat} and {manzana, plátano, árbol, casa, gato}; you can look up the appropriate definitions online (or use your own). All of the words here are nouns. When you've finished populating these dictionaries, return them in an array of size two, one for each of the dictionaries.

GRADING RUBRIC for Homework 6:

Like for other homeworks, you will be graded based on the test cases passed in this driver that you should download and run in the same directory where you saved your Dictionary.java file. You will also be earning four points on this homework for passing the style checker.

Submission

Please submit your Dictionary.java only to BB, due 11:59pm on 04/05