Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java: Learning Objectives Declare and construct an array of String objects where the size is known in advance (perfect size array). (10 points) Declare

In Java:

Learning Objectives

Declare and construct an array of String objects where the size is known in advance (perfect size array). (10 points)

Declare and construct an array of String objects where the size of the active portion of the array may change during execution (oversize array). (10 points)

Perform spelling checking using a standard dictionary. (10 points)

Perform spelling checking using a personal dictionary, updating the personal dictionary with user selected words. (10 points)

Use the sort() and binarySearch() methods in the Arrays class. (10 points)

Use a for loop. (10 points)

Save an oversized array to a file. (10 points)

Create at least four meaningful methods that pass arrays as parameters and/or return them. (20 points)

10 points will be awarded for the documentation of your program. That means using good names for variables, proper and consistent indentation of code, and meaningful use of whitespace.

Description

Many software programs have a spelling checker. Spelling checkers work by comparing each word in a given file to an existing dictionary to see if the word is in the dictionary. If it is, it is determined to be spelled correctly. If it is not, then it is not spelled correctly. This strategy has numerous well known shortcomings, particularly that the dictionary cannot tell if the word is being used properly, just if it was somewhere in the dictionary. So if you type here when you meant hear a spelling dictionary will not be able to detect the problem.

In this lab, you will implement a spelling checker that reads from a standard dictionary file. Youll need to find the length of the dictionary so you can use it to create your dictionary array. Then you will read the words from the dictionary into the array. The words in the dictionary are in sorted order, so this is a big advantage. The program should then read words from the console using a Scanner and print out a warning if the word is not in the spelling dictionary. Make sure you take advantage of the fact that the dictionary is sorted by using binary search.

The global dictionary file is stored in american-words.txt and is available on Canvas. The best place to put this is in the Project main directory. This dictionary will not change size.

Many good word processing programs have two separate dictionaries for spelling checking: the global dictionary and a personal dictionary. The personal dictionary contains words that are correctly spelled that are not in common usage. For example, I use technical jargon in a lot of my writing. Most word processors dont know that accessor and mutator are spelled correctly. But they are spelled correctly and I know they are spelled correctly. It would be nice for them not to be flagged as wrong every single darn time. To work around this problem, a word processor can keep a personal dictionary. The personal dictionary is used in the same way that that the global dictionary is usedexcept that words can be added to it. One of my favorite old word processor programs gave you the option of adding any word that it thought was spelled wrong that you subsequently confirmed to your personal dictionary. After using that program for a number of years, spelling checking was really great.

Here is a sample interaction.

Enter a word or QUIT to stop:

hello

That word is spelled correctly

Enter a word or QUIT to stop:

goodbye

That word is spelled correctly

Enter a word or QUIT to stop:

jambone

That word is not spelled correctly

Would you like to add it to your personal dictionary Yes/No

yes

Enter a word or QUIT to stop:

jambone

That word is spelled correctly

Enter a word or QUIT to stop:

quit

The array that stores the personal dictionary will need to be allocated to a large enough size (100 is fine for this project). The active size will need to be stored and manipulated. You do not need to consider what will happen if more than 100 personal dictionary words are entered, although an error message to the user is always considerate.

Remember to create an empty PersonalDictionary.txt file for your program so that it will find the file the first time it runs.

Selecting Method Signatures

This is the first project where youll need to find the method signatures yourself. To help you along, Ill describe what the methods should do. You should work with your partner to determine the signatures for the methods before you implement the method. Ive left some space below for you to write the method signatures in.

One method should read the global dictionary into a perfect sized array.

One method should read the personal dictionary into an oversized array.

One method should write the personal dictionary (stored in an oversized array) out to a file.

One method should take the two dictionaries and perform spell checking. One of these dictionaries is perfect size and the other is oversize so think through the signature carefully. This method will have to update the array that contains the oversize dictionary.

There will also be a main program that will call the methods above.

Writing A File

Writing files is similar to reading them. The class that writes simple files is called PrintWriter. It is constructed as below:

PrintWriter pw = new PrintWriter(new File(filename));

Any method that constructs a PrintWriter or calls a method that constructs a PrintWriter will have to throw a FileNotFoundException, just like using File with the Scanner class.

To write a String to a PrintWriter object use the code below:

pw.write(Any String );

It is critically important that you remember to close files when you are writing them. If you do not close a file, the file contents will be lost.

pw.close();

Command Line Arguments (Optional)

This project allows you an opportunity to final use String[] args for something useful! The parameter to the main program is called a command line argument, although the rationale for this name only makes sense if you program in Java without an IDE like eclipse. If you have a full installation of Java, you can run a Java program from the Command Line. First, export your Java file from eclipse and store it on your desktop. To compile this .java file into a class file, open the command prompt, navigate to the desktop (cd directoryName will change directories), and compile your code by entering:

javac MyClass.java

If you look at your desktop, you will now see a file called MyClass.class has been added. This is the compiled version of your Java program in a language called bytecode. You run this program by entering:

java MyClass

When you run a program this way, you have the option of entering (at the command line) additional argumentshence command line arguments. For example, you could enter:

java SpellCheck globalDictionary.txt personalDictionary.txt

Inside your program, the String array args will now have a length of 2.

args[0] will be globalDictionary.txt, the name of the global dictionary

args[1] will be personalDictionary.txt. You can use these Strings to construct your Scanner objects that read files.

Doing this in eclipse is more complicated. First go to Project -> Properties . Select the Run/Debug Setting category, and create a New Setting. Eclipse will ask you if you want a Java Applet or a Java Application. We want a Java Application, so select that and click OK.

You will see another tabbed interface appear. Select the Arguments tab and enter the file names. These will be fed in as command line arguments when your program runs.

The purpose of command line arguments is to provide another communication channel to your program. Things like resource file names are nicely handled as command line arguments.

----

We were given this to start with:

public class Project10{

import java.util.Arrays;

public static String[] globalDictionary(String fileName) throws FileNotFoundException {

return null;

}

public static int personalDictionary(String[] array, String fileName) {

return 0; // will change

}

public static void writer (String [], personalArray, int size, String fileName) {

}

public static int inputData(String[] global, String [] personal, int personalSize) {

// do a checker here, use binary search

return 0;

}

}

---

Then we are given a "dictionary" .txt file which I have already imported.

Thanks.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions