I need help writing this code for java class Starter file Pr
I need help writing this code for java class.
Starter file: Project3.java and input file: dictionary.txt
Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following: at index [1], the number words of length 1. At index [2] then number of words of length 2 . . . at histogram[n] the number of words of length n. Since there will be no words of length zero, there will be a zero in histogram[0].
The histogram is just a plain array of int that is initialized to length zero. Recall that Java allows us to define a new array with a length of zero. It happens every time you run a Java program with no commnd line arguments. Doing so initializes the args array to have length zero. Every time you read a word from the dictionary you cannot just execute an increment statment such as
This is the correct statement to execute, but you must first make sure the histogram is long enough to have a cell at that index. If your current word has length of 7, you must first make sure that your histogram array has a .length of at least 8 (not 7) because the [7] cell of the array is actually the eighth cell.
If you do need to upsize your histogram then only upsize it to be just big enough for that particular word. At the end of the program it is possible that you could have gaps in your histogram if (depending on the input file) there were no words of length twenty six. In this case there would still be a zero at the [26] cell in the array that was put there when the Java compiler initialized all the cells to zero.
Here is the starting code.
If there is anything else you might need to write this, let me know and I will be sure to post it.
Solution
/* Project3.java Dynamic histogram */
import java.io.*;
import java.util.*;
public class Project3
{
static final int INITIAL_CAPACITY = 10;
public static void main(String[] args) throws Exception
{
if (args.length < 1 )
{
System.out.println(\"\ usage: C:\\\\> java Project3 \ \ \"); // i.e. C:\\> java Project3 dictionary.txt
System.exit(0);
}
int[] histogram = new int[0];
String[] wordList = new String[INITIAL_CAPACITY];
int wordCount = 0;
BufferedReader infile = new BufferedReader(new FileReader(args[0]));
while (infile.ready())
{
String word = infile.readLine();
// test to see if list is full. If needed do an up size
if (wordCount == wordList.length)
wordList = upSizeArr(wordList);
//now you may safely append word onto list and incr count
String nWord = infile.readLine();
wordList[wordCount++] = nWord;
// look at the word length and see if the histogram length is at least
// word length + 1. If not, you must upsize histogram to be exactly word length + 1
int wordLength = word.length();
if (word.length() > histogram.length)
histogram = upSizeHisto(histogram, wordLength);
// now you can increment the counter in the histogram for this word\'s length
histogram[word.length()]++;
}
infile.close();
wordList = trimArr(wordList, wordCount);
System.out.println(\"After trim, wordList length: \" + wordList.length);
for (int i = 0; i < histogram.length; i++)
System.out.println(\"words of length \" + i + \": \" + histogram[i]);
}
private static String[] upSizeArr(String[] fullArr)
{
int l = (fullArr.length)*2;
String[] array = new String[l];
for (int i = 0; i < fullArr.length; i++)
{
array[i] = fullArr[i];
}
return array;
}
private static String[] trimArr(String[] oldArr, int count) {
String[] array = new String[count];
for (int i = 0; i < array.length; i++)
{
array[i] = oldArr[i];
}
return array;
}
private static int[] upSizeHisto(int[] oldArr, int newLength) {
int array[] = new int[newLength];
for (int i = 0; i < oldArr.length; i++)
{
array[i] = oldArr[i];
}
return array;
}
}

