CS 201 - Computer Science I - Fall 2004
Lab 7 - arrays
Loyola College >
Department of Computer Science >
CS 201 >
Labs >
Lab 7
Due
Electronic Submission: Monday, November 29 at 11:59pm. Labs submitted one day late will be assessed a
20% penalty. Labs will not be accepted more than one day late.
Write-up and print out: Wednesday, December 1 at the beginning of class
Objectives
- to use arrays to hold data
- to search arrays
Reading
Koffman & Wolz, Sections 5.4 through 5.5
Assignment
Create a class called Dictionary<YourName>. Instances of this class will store
lists of words in an array. The class should have the following:
- a field that is an array of
Strings to hold the words;
- a field that keeps track of how many words are in the dictionary
(it will be possible that not all of the space in the array is used);
- a constructor that takes the maximum size of the dictionary as an
argument, creates the array and initializes the size field;
- a method contains that takes a String as an argument
and returns true if that String is somewhere in the array
and false otherwise. (Do not write a method that returns from inside the loop, as on page 298)
- a method addWord that takes a String as an argument,
stores that String in the array if it is not aleady present,
and updates the size field
(do not bother keeping the words in order and
do not worry about the case in which the array is already full);
- a method equals that takes a Dictionary as an
argument and returns true or false according to whether
the two dictionaries are the same, where dictionaries are the same if and
only if each contains all the words in the other (order does not matter).
Your class should work with the following test driver. The following
code can be downloaded from the file
DictionaryTest.java.
public class DictionaryTest
{
public static void main(String[] args)
{
Dictionary english = new Dictionary(10);
Dictionary german = new Dictionary(10);
Dictionary shark = new Dictionary(2);
Dictionary martian = new Dictionary(10);
english.addWord("eat");
english.addWord("five");
english.addWord("swim");
german.addWord("essen");
german.addWord("funf");
german.addWord("schwimmen");
shark.addWord("eat");
shark.addWord("swim");
martian.addWord("swim");
martian.addWord("eat");
martian.addWord("five");
System.out.println("=== TESTING contains ===");
System.out.println(english.contains("two")); // should be false
System.out.println(english.contains("funf")); // false
System.out.println(german.contains("funf")); // true
System.out.println(german.contains("fork".charAt(0) + "unf")); // true
System.out.println("=== TESTING equals ===");
System.out.println(english.equals(german)); // false
System.out.println(english.equals(shark)); // false
System.out.println(english.equals(english)); // true
System.out.println(martian.equals(english)); // true
}
}
Write-up
Begin with an introduction that explains the purpose of the lab. Then describe the methods contains, addWord, and equals. Use specific examples when describing the behavior of the method given different circumstancs. For contains and addWord, you should include at least two examples, and for equals, decribe two examples that return true and two that return false. Use different examples that than those given in DictionaryTest.java. Also describe any situations where your code initially outputted the wrong answer, and what you did to correct the problem. Conclude with any comments or concerns.
This write-up will be graded for quality of writing and use of technical terms. Sophistication of the write-up will also be accessed.
Grading
- Attendance in lab (5 pts)
- Code compiles and runs (10 pts)
- Code produces the expected output (12 pts)
- Data Fields (2 pts)
- Constructor (3 pts)
- contains (3 pts)
- addWord (4 pts)
- equals (6 pts)
- Program includes sufficient comments (5 pts)
- Write-up (50 pts)
Submissions
Submit the source code for your Dictionary<LastName1><LastName2> class using the class submission page. Turn in a print out of your code and write-ups in class.