[Solved]Intro Java Description Project Going Rework Program Someone Else Wrote Using New Way Stori Q37202553
Intro to Java
Description
In this project you are going to rework a program someone elsewrote using a new way of storing and organizing data. You will takea program that used arrays and implement it using ArrayListobjects. In general, ArrayList objects are easier to use thanarrays. For example, both perfect size and oversize arrays fitnicely into ArrayList objects. Commercial software engineers workwith other people’s code all the time, so it is a good experiencein general to have
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
/** This program is a word by word spelling checker. It relieson two dictionaries: a global
* dictionary and a personal dictionary that is initially empty. Theprogram reads the file
* names for these dictionaries from the command line.
* @author Deborah A. Trytten
*
*/
public class SpellingChecker
{
/** The personal dictionary may have only this manyelements.
*
*/
public static final int SIZE = 100;
/** The program reads the global and personaldictionaries from files into arrays.
*/
public static void main(String[] args) throwsFileNotFoundException
{
// This kind of guard is always agood idea, in case people misunderstand command
// line arguments
if (args.length != 2)
{
System.out.println(“Please enter the name of the global andpersonal dictionaries”
+ ” at thecommand line”);
System.exit(0);// failure
}
// The size of the globaldictionary is fixed
String[] globalDictionary =readDictionary(args[0]);
// The personal dictionary has tobe allocated extra size and the size must
// be tracked
String[] personalDictionary = newString[SIZE];
int personalDictSize =readDictionaryFixedSize(personalDictionary, args[1]);
// The size of the personaldictionary may change during execution
personalDictSize =processInput(globalDictionary, personalDictionary,personalDictSize);
// Write the personal dictionaryout to the file it came from
writePersonalDictionary(personalDictionary, personalDictSize,args[1]);
}
/** Repeatedly allow the user to enter a word. Theword will be sought in the
*/
public static int processInput(String[] global,String[] personal, int personalSize)
{
Scanner keyboard = newScanner(System.in);
String answer;
// user input will terminate thisloop
while (true)
{
// this means wecan’t spell check quit–but that’s OK.
System.out.println(“Enter a word or QUIT to stop:”);
String word =keyboard.next();
word =word.toLowerCase(); // the dictionary is in lower case
// Get out ofthis method if the user wants to quit
if(word.equalsIgnoreCase(“Quit”))
return personalSize;
// Search theglobal dictionary
if(Arrays.binarySearch(global, word) >= 0)
{
System.out.println(“That word is spelledcorrectly”);
}
// Search thepersonal dictionary
else if(Arrays.binarySearch(personal, 0, personalSize, word) < 0)
{
System.out.println(“That word is not spelledcorrectly”);
System.out.println(“Would you like to add it toyour personal dictionary Yes/No”);
answer = keyboard.next();
if (answer.equalsIgnoreCase(“Yes”) &&personalSize < SIZE-1)
{
personal[personalSize] =word;
++personalSize;
Arrays.sort(personal,0,personalSize);
}
} // word notfound in either dictionary
else // Thislogic looks odd, but it was done this way to avoid calling
// either search twice
{
System.out.println(“That word is spelledcorrectly”); // found in personal dictionary
}
} // end while
}
/** Read the dictionary file.
*/
public static String[] readDictionary(String fileName)throws FileNotFoundException
{
Scanner file = new Scanner(newFile(fileName));
// find the length of thedictionary by reading the file
int count = 0;
while (file.hasNextLine())
{
++count;
file.nextLine();
}
file.close();
// Now read the file and fill thearray
String[] result = newString[count];
file = new Scanner(newFile(fileName));
int index = 0;
while (index < count)
{
result[index] =file.nextLine();
++index;
}
file.close();
return result;
}
/** Read the dictionary into an array of fixedsize.
*/
public static int readDictionaryFixedSize(String[]array, String fileName) throws FileNotFoundException
{
Scanner file = new Scanner(newFile(fileName));
// Read in the file contentsuntil you run out of File or array
int index = 0;
while (index < SIZE &&file.hasNextLine())
{
array[index] =file.nextLine();
++index;
}
file.close();
return index;
}
/** Write the personal dictionary out to a file.
*/
public static void writePersonalDictionary(String[]personalDictionary, int size, String fileName)
throws FileNotFoundException
{
PrintWriter pw = newPrintWriter(new File(fileName));
for(int i=0; i<size; ++i)
{
pw.write(personalDictionary[i] + “n”);
}
pw.close();
}
}
Expert Answer
Answer to Intro to Java Description In this project you are going to rework a program someone else wrote using a new way of storin… . . .
OR

