import java.io.*; import java.util.*; public class Apple { static final int INIT
ID: 3881245 • Letter: I
Question
import java.io.*; import java.util.*; public class Apple { static final int INITIAL_CAPACITY = 10; public static void main (String[] args) throws Exception { // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println(" usage: C:\> java Apple <input filename> "); // i.e. C:> java Apple dictionary.txt System.exit(0); } int[] histogram = new int[0]; // histogram[i] == # of words of length n /* array of String to store the words from the dictionary. We use BufferedReader (not Scanner). With each word read in, examine it's length and update word length frequency histogram accordingly. */ String[] wordList = new String[INITIAL_CAPACITY]; int wordCount = 0; BufferedReader infile = new BufferedReader( new FileReader(args[0]) ); while ( infile.ready() ) { String word = infile.readLine(); // # # # # # DO NOT WRITE/MODIFY ANYTHING ABOVE THIS LINE # # # # # // test to see if list is full. If needed do an up size // now you may safely append word onto list and incr count // 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 // now you can increment the counter in the histogram for this word's length // # # # # # DO NOT WRITE/MODIFY ANYTHING BELOW THIS LINE # # # # # } //END WHILE INFILE READY infile.close(); wordList = trimArr( wordList, wordCount ); System.out.println( "After final trim: wordList length: " + wordList.length + " wordCount: " + wordCount ); // PRINT WORD LENGTH FREQ HISTOGRAM for ( int i = 0; i < histogram.length ; i++ ) System.out.format("words of length %2d %d ", i,histogram[i] ); } // END main // YOU MUST CORRECTLY COPY THE STRING REFS FROM THE OLD ARR TO THE NEW ARR static String[] upSizeArr( String[] fullArr ) { return null; // just to make it complie you change as needed } static String[] trimArr( String[] oldArr, int count ) { return null; // just to make it complie you change as needed } // YOU MUST CORRECTLY COPY THE COUNTS FROM OLD HISTO TO NEW HISTO static int[] upSizeHisto( int[] oldArr, int newLength ) { return null; // just to make it complie you change as needed } }
Explanation / Answer
Below is your code: -
public class Apple {
static final int INITIAL_CAPACITY = 10;
public static void main(String[] args) throws Exception {
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE
// COMMAND LINE
if (args.length < 1) {
System.out.println(" usage: C:\> java Apple <input filename> "); // i.e.
// C:>
// java
// Apple
// dictionary.txt
System.exit(0);
}
int[] histogram = new int[0]; // histogram[i] == # of words of length n
/*
* array of String to store the words from the dictionary. We use
* BufferedReader (not Scanner). With each word read in, examine it's
* length and update word length frequency histogram accordingly.
*/
String[] wordList = new String[INITIAL_CAPACITY];
int wordCount = 0;
BufferedReader infile = new BufferedReader(new FileReader(args[0]));
while (infile.ready()) {
String word = infile.readLine();
// # # # # # DO NOT WRITE/MODIFY ANYTHING ABOVE THIS LINE # # # # #
// 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 newWord = infile.readLine();
wordList[wordCount++] = newWord;
// 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
// now you can increment the counter in the histogram for this
// word's length
int wordLength = word.length();
if (word.length() > histogram.length)
histogram = upSizeHisto(histogram, wordLength);
histogram[word.length()]++;
// # # # # # DO NOT WRITE/MODIFY ANYTHING BELOW THIS LINE # # # # #
} // END WHILE INFILE READY
infile.close();
wordList = trimArr(wordList, wordCount);
System.out.println("After final trim: wordList length: " + wordList.length + " wordCount: " + wordCount);
// PRINT WORD LENGTH FREQ HISTOGRAM
for (int i = 0; i < histogram.length; i++)
System.out.format("words of length %2d %d ", i, histogram[i]);
} // END main
// YOU MUST CORRECTLY COPY THE STRING REFS FROM THE OLD ARR TO THE NEW ARR
static String[] upSizeArr(String[] fullArr) {
String upSizeArr[] = new String[fullArr.length * 2];
// System.arrayCopy(sourceArray, indexToStart, destinationArray,
// indexToStart, numberOfElementsToCopy)
System.arraycopy(fullArr, 0, upSizeArr, 0, fullArr.length);
return upSizeArr; // just to make it complie you change as needed
}
static String[] trimArr(String[] oldArr, int count) {
// you know how many words were added because of the wordCount being
// passed in
String[] newArr = new String[count];
System.arraycopy(oldArr, 0, newArr, 0, count);
return newArr; // just to make it complie you change as needed
}
// YOU MUST CORRECTLY COPY THE COUNTS FROM OLD HISTO TO NEW HISTO
static int[] upSizeHisto(int[] oldArr, int newLength) {
int[] newArr = new int[newLength];
// create the appropriate size array
// System.arrayCopy(sourceArray, indexToStart, destinationArray,
// indexToStart, numberOfElementsToCopy)
System.arraycopy(oldArr, 0, newArr, 0, oldArr.length);
return newArr; // just to make it complie you change as needed
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.