Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CO320 Assignment 2: A Spellchecker 
University of Kent               Copyright © M. Kölling 1 
 
Goal 
The goal of this assignment is to implement a class that serves as a spellchecker for a text application (for example, a word 
processor, or something similar). 
This time, you do no get an existing project. You need to create a project and required class(es) yourself. However, your 
project and the class(es) must strictly fulfill the specification below. 
The “spelling” project: 
The project you create must be called “spelling”. Do not call it anything else. 
The SpellChecker class: 
You must create a class called SpellChecker. It must have exactly this name; this is important.  
You will be given a dictionary text file with lots of words in the English language. You will also be given the source code 
for another class called DictReader. When an object of this class is constructed, it reads in a dictionary (from a text file 
whose name is given as a String in the class constructor). You should construct a DictReader object in a private field 
of your SpellChecker. From this DictReader, a dictionary can be obtained as an ArrayList (via its 
getDictionary method). That dictionary should also be saved in a private field of your SpellChecker. 
    
Base Tasks (level 1) 
Your SpellChecker must have the following public methods: 
public int numberOfWords() 
This method returns the number of words in the dictionary (that is held in its private field – see above). 
public boolean isKnownWord(String word) 
This method returns true, if (and only if) the given word is found in the dictionary. This method is case-sensitive 
– for example, it must return false on “Highlander”, even though “highlander” is in the dictionary. 
public boolean allKnown(ArrayList words) 
This method returns true if (and only if) all words in the given list are found in the dictionary. This method also 
is case-sensitive. 
 
Base Tasks (level 2) 
public ArrayList wordsStartingWith(String prefix) 
This method returns a list of all words from the dictionary that start with the given prefix. This method is not 
case-sensitive. This means, for instance, that looking up words starting with “gilb” must find “Gilbert”, “gilbert”, “gil-
bertage” … plus some others. Look through the Java documentation for String (in the Java API) for a method to 
help you! 
public ArrayList wordsContaining(String text) 
This method returns a list of all words from the dictionary that include the given substring. This method is not 
case-sensitive. This means, for instance, that looking for words containing with “RAX” must find “abraxas”, “actino-
praxis” … plus some others. In the slides, you have already seen a suitable String method to help you with this.
CO320 Assignment 2 
University of Kent              Copyright © M. Kölling 2 
Base Tasks (level 3) 
public void insert(String newWord) 
Insert the given word into the dictionary. If the word already exists, this method does nothing. If the word does 
not already exist, it is inserted so that the alphabetic order of the dictionary is maintained (i.e. you cannot just add it to 
the end – you must add it to the right place!). Look through the Java documentation for ArrayList (in the Java 
API, package java.util) for a version of its add method to help you add items to some specified index in the 
collection! 
public boolean remove(String newWord) 
Remove the given word from the dictionary. If the word was successfully removed, return true. If not (because it 
did not exist), return false. 
public void save() 
Save the dictionary to disk. This is not meant to be hard – there is a method in the DictReader class that you 
can use. It is listed here only because it goes naturally with insert and remove. 
 
Challenge Tasks (only if you have time left over) 
public boolean isPalindrome(String word) 
Return true if (and only if) the given word in the dictionary and is a palindrome. A palindrome is a word that 
reads the same backwards and forwards. For example “racecar” is a palindrome. Ignore case differences in letters 
when testing for a palindrome. 
public ArrayList anagrams(String word) 
Return a list of all words that are anagrams of the given word. Ignore case differences in letters when testing for 
anagrams. 
public ArrayList difference(ArrayList dictionary) 
Given another dictionary as a parameter, compare the given dictionary to this one. Return a list of words that are 
in this dictionary, but not in the one supplied as parameter. To test this, use a text editor to modify a copy of the 
words.txt file (see next page) into a new file. Then, in some test method that you need to write, use a Dic-
tReader to make a test dictionary (i.e. ArrayList) from that new file. 
 
The Really Hard Challenge Task (really only if nothing else to do) 
public int distance(String word1, String word2) 
Compute the “distance” between two strings. The distance function is used in dictionaries to suggest spelling cor-
rections. Usually, when suggesting corrections, the application suggests the words with the smallest distance to 
the word found. 
To compute distance, we use the Levenshtein Distance. You can find definitions (and some tests) here: 
http://www.cut-the-knot.org/do_you_know/Strings.html 
http://www-igm.univ-mlv.fr/~lecroq/seqcomp/node2.html 
CO320 Assignment 2 
University of Kent               Copyright © M. Kölling 3 
How to approach the project 
I have divided the base tasks into three levels. This is intended to give you an order of attack: I suggest that you complete 
the methods in each level before you move on to the methods in the next level. I would rather see submissions that 
implement some methods well, rather than submissions that do all methods badly. 
 
Support material 
Linked from the “Assessments” box on this module’s web (Moodle) page, are the following: 
• a file called words.txt. This is a text file that lists the dictionary words (one word per line).  Place a copy of 
this file into your project folder for the spell checker to use. This file contains 234,937 words: from “A” to 
“Zyzzogeton” (yes, it’s a real word – try Googling for it!). 
• a Java file called DictReader.java. This is the source code for a class that you may use in your project. It 
reads the dictionary file from disk and provides it as an ArrayList. Add this class to your project by 
using the Edit–Add Class From File function in BlueJ. 
• this document (assignment2.pdf). 
These files are also on raptor and swallow in the folder (courses/co320_ca13/assignment2/). 
 
Submission and deadline 
You must submit your complete BlueJ project electronically. Submission will again be by copying your project into a 
submission folder on raptor (or swallow) You will find your personal submission folder in: 
proj/co320_ca13/assignment-2//. 
You must submit by Friday of week 20 (12th. March, 2010) before 23:59pm. Late submissions will not be accepted. 
 
Marking 
The submission will be marked for 
• completeness (have all required methods been implemented?) 
• correctness (does everything work as specified?) 
• commenting (is everything commented as it should?) 
• style (is the code nicely laid out and formatted, are the variable names chosen well?) 
• difficulty (are any of the challenge tasks included?) 
A submission with a perfect implementation of the base tasks will receive a good mark. To get a very good mark, you need 
to implement at least one challenge task as well.