Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project#6 will give you experience loading a large file of Strings into an array

ID: 3914039 • Letter: P

Question

Project#6 will give you experience loading a large file of Strings into an array of Strings. You job will be to write the definition of these two methods:

1. static String[] upSize( String[] fullArray ); // incoming array is FULL. It needs it's capacity doubled

2. static String[] downSize( String[] array, int count ); // incoming array most likely NOT full and needs down-sized to be just big enough to hold only the initialized elements. Like shrink wrapping the array to eliminate those wasted empty elements. Makes .length == count.

The program you are given to start with: Project6.java reads a file of Strings into an array but it has a problem. The array is dimensioned with an initial capacity of only ten elements. Inside the while loop is an if test that compares the current wordCount to the current capacity (.length) of the array. If the array is full, a method named upSize( String[] fullArr) (which you must write) is called that performs the following:

1. allocates a new array that is twice the capacity of the one passed in that got full

2. copies all the values from the full array into the new (bigger) array

3. returns the address (reference) of the new (bigger) array

Each time this upSize(...) method returns, the capacity of the new (bigger) array will be printed out. The file you are given will do all the output for you. DO NOT WRITE ANY OUTPUT STATEMENTS. After the loop terminates the program will call a similar method called downSize( String[] arr,int count) (which you must write) that performs the following:

1. allocates a new array that is just big enough to hold all the data from the arrays passed in.

2. copies all the values from the array passed in, into the new array that is just big enough to hold them all

3. return the address (reference) of the new (shrink wrapped) array

After the array is downSize'ed to shrink-wrap fit the data, the new .length (capacity) and wordCount will be reported. The file you are given will do all the output for you. DO NOT WRITE ANY OUPUT STATMENTS.

Code:

/* Project6.java Resizes an array every time it fills up while reading a large file. */

import java.io.*;

import java.util.*;

public class Project6

{

static final int INITIAL_CAPACITY = 5;

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 Project6 dictionary.txt ");

System.exit(0);

}

String[] wordList = new String[INITIAL_CAPACITY];

int wordCount = 0;

BufferedReader infile = new BufferedReader( new FileReader(args[0]) );

int numOfUpsizes=0;

while ( infile.ready() ) // i.e. while there are more lines of text in the file

{

String word = infile.readLine();

if ( wordCount == wordList.length ) // an array's .length() is its CAPACITY, not count

{

wordList = upSize( wordList );

System.out.format( "after resize#%d length=%d, count=%d ",++numOfUpsizes,wordList.length,wordCount );

}

wordList[wordCount++] = word; // Now wordList has enough capacity

} //END WHILE INFILE READY

infile.close();

System.out.format( "after file closed: length=%d, count=%d ",wordList.length,wordCount );

wordList = downSize( wordList, wordCount );

System.out.format( "after final down size: length=%d, count=%d ",wordList.length,wordCount );

printFirstTenWords( wordList ); // IF THERE ARE LESS THAN 10 IN THE ARRAY THEN JUST PRINT THOSE

printLastTenWords( wordList ); // IF THERE ARE LESS THAN 10 IN THE ARRAY THEN JUST PRINT THOSE

} // END MAIN

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

// DO NOT MODIFY THESE TWO METHODS

static void printFirstTenWords( String[] wordList )

{ final int MAX_WORDS_PER_LINE = 10;

int count = wordList.length<MAX_WORDS_PER_LINE ? wordList.length : MAX_WORDS_PER_LINE;

for (int i=0 ; i<count ; ++i )

System.out.print( wordList[i] + " ");

System.out.println();

}

static void printLastTenWords( String[] wordList )

{ final int MAX_WORDS_PER_LINE = 10;

int count = wordList.length<MAX_WORDS_PER_LINE ? wordList.length : MAX_WORDS_PER_LINE;

for (int i=0 ; i<count ; ++i )

System.out.print( wordList[wordList.length-count+i] + " ");

System.out.println();

}

// YOU FILL IN THESE METHODS BELOW

static String[] upSize( String[] fullArr )

{

return null; // REPLACE WITH YOUR CODE

}

static String[] downSize( String[] arr, int count )

{

return null; // REPLACE WITH YOUR CODE

}

} // END CLASS Project6

INPUT FILE : BufferedReader infile String[] words - new String [5] int count ; new BufferedReader ( new FileReader( args[0] ) ); nwae es 1??? rpeoims 2 3 4 ettniner 5 6 while infile. ready ( words [count++] -infile.readLine // ie. while it has another line // read line (word) ahicryrhe dica dobol nyegtr cukklen warllc after the fifth word is read in, the array is FULL 8 9 [words]-> -I 11 addej 12 baltoc 13 14 dufil "nwae" "rpeoims""ettniner" ahicryrhe" "dica' WE MUST RESIZE THE ARRAY AS FOLLOWS mycall preko yaldde // STEP #1 : DEFINE A NEW ARRAY TWICE AS LONG AS THE FULL ONE // STEP #3: COPY THE REF TO BIGGER ARRAY INTO REF TO WORDS ARRAY String biggerArr - new String[ words.length * 2 16 dupled words = bigge rArr: 0 1 2 3 4 5 6 7 8 9 18 19 nekel 20 21 lappor lasia [biggerArr]- ] "nwae" "rpeoims""ettniner" "ahicryrhe" "dica" laurib // STEP 2: WRITE A LOOP THAT COPIES ALL THE STRINGS REFERENCES couph FROM THE FULL ARRAY TO THE BIGGER ARR 7 8 9 [words] [words] -> "nwae" "rpeoims""ettniner""ahicryrhe" "dica" 0 1 2 3 >?][ ] [ /] [ [ 7 8 9 [biggerArr] ][ ][ ][ ][ ]

Explanation / Answer

static String[] upSize( String[] fullArr )

{

    // create an array double the size of fullArr

    String[] arr = new String[ 2 * fullArr.length ];

   

    int i;

   

    // copy the contents of fullArr to arr

    for( i = 0 ; i < fullArr.length ; i++ )

        arr[i] = fullArr[i];

   

    return fullArr;

}

static String[] downSize( String[] arr, int count )

{

    // create an array of size count

    String[] new_arr = new String[ count ];

   

    int i;

   

    // copy the contents of arr to new_arr

    for( i = 0 ; i < count ; i++ )

        new_arr[i] = arr[i];

   

    return new_arr;

}