Introduction to Software Development, Spring 2023
GWU Computer Science
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.
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.
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.
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
.
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.
Note: please only use the 26 letters in the English alphabet when writing your words and definitions; the submit server isn't able to handle characters such as ñ
and will not run correctly if you include such letters.
Dictionary.java
file. To compile both files at the same time, type javac *.java
in your terminal; this will compile all java files in the folder you are currently in.
Homework7.tar
to the submit server.