I need help with a java program. The job is to write the definition of these two
ID: 3914787 • Letter: I
Question
I need help with a java program. The job is 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. Please 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 downsized 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. Please do not write any output statements.
Here is the starter file!
Explanation / Answer
Instructions: Just copy the above methods and put at the respective place into the program and you will get the desired results. Comment if you have any doubts.
Method upSize() static String[] upSize( String[] fullArray ){
int len = fullArray.length;
String[] newArray = new String[2*len]; // assigning the size as double of len
for(int i = 0; i < len; i++)
{
newArray[i] = fullArray[i]; // copying the elements
}
return newArray;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.