Homework 10: Writing your own classes
The homework problems below will help you prepare for Quiz 6
Introduction
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.
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. 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.
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, its definition, and its part of speech. Initially, your 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
.
Also 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.
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.
Submission and grading
When you are ready to submit your assignment, inside that folder type the following command, which will zip up all your java files into a tarfile called Homework10.tar
:
tar -cvf Homework10.tar *.java
You will then upload this single Homework10.tar
file to the submitserver for grading. Make sure you verify that your score on the submitserver is the score you expected; if not, you can look at the “Autograder output” on the submitserver for your error messages. If you need help with interpreting these, please reach out during office hours or on Ed.
Please make sure to submit your code early enough before the deadline so that if you need any last-minute help you can visit a TA or get a response from Ed (during normal business hours).