Self-Test 7
Wed, Dec. 8 2010
Before you start:
Take a look at StringList and StringListDemo (downloadable from the
Examples link), which is an example of how an array can be used as an
instance variable.
Exercise (part 1):
Today we are going to modify our LexUnit class (hw 5) to store the
synonyms in an array of type String, which will be expanded if
necessary.
We will only change the implementation of synonyms today (changing the
implementation for orthForms requires the same steps):
- Change the declaration of your synonym instance variable
- Add 2 constructors (all constructors should initialize all
instance variables):
- default
- one that takes an orthForm as a parameter
- b oth should start out with a synonym array of length 2
- Modify the addSynonym
method appropriately
- if the array is not big enough to hold another element, resize
the array before adding
- Modify the getSynonyms
method so that it returns a copy
of the used portion of the synonym array
- Modify your toString
method appropriately
Write a demo program that creates a LexUnit and adds 3 synonyms.
Adding the 3rd synonym should cause
your resizing code to be executed.
Make sure your demo program calls each method in the LexUnit class at
least once.
Exercise (part 2):
In your demo program, create an array of LexUnits.
Repeatedly prompt the user to enter an orthForm and its synonyms,
create a LexUnit with this information, and add the LexUnit to your
array.
Print all LexUnits in the array.
Note that this array may also become full at some point - add code to
resize it if necessary.