Question: I have this java code and I cannot seem to get it to work properly. I'll add what is going wrong with the code in

I have this java code and I cannot seem to get it to work properly. I'll add what is going wrong with the code in here as well.

/* Project4.java InsertInOrder with bSearch optimization to compute insertion index */

import java.util.*;

import java.io.*;

public class Project4

{

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

{

System.out.println(" usage: C:\\> java Project4 "); // i.e. C:\> java Project4 P4input.txt

System.exit(0);

}

// LOAD FILE INTO ARR USING INSERINORDER

Scanner infile = new Scanner( new File( args[0] ) );

int[] arr = new int[INITIAL_CAPACITY];

int count= 0;

while (infile.hasNextInt())

{

if ( count==arr.length )

arr = upSizeArr(arr);

insertInOrder( arr, count++, infile.nextInt() );

}

infile.close();

arr=trimArr(arr,count); // Now count == .length

System.out.println( "Sorted array of " + arr.length + " ints from " + args[0] + " after insertInOrder:" );

printArray( arr ); // we trimmed it thus count == length so we don't bother to pass in count

} // END MAIN

// ############################################################################################################

// USE AS IS - DO NOT MODIFY

static void printArray( int[] arr )

{

for( int i=0 ; i

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

System.out.println();

}

// USE AS IS - DO NOT MODIFY

static int[] upSizeArr( int[] fullArr )

{

int[] upSizedArr = new int[ fullArr.length * 2 ];

for ( int i=0; i

upSizedArr[i] = fullArr[i];

return upSizedArr;

}

// USE AS IS - DO NOT MODIFY

static int[] trimArr( int[] oldArr, int count )

{

int[] trimmedArr = new int[ count ];

for ( int i=0; i

trimmedArr[i] = oldArr[i];

return trimmedArr;

}

// ############################################################################################################

static void insertInOrder( int[] arr, int count, int key )

{

int index=bSearch( arr, count, key );

// LEAVE THIS HERE

// Y O U R C O D E I N H E R E

if(index

index = index + 1;

}

for(int i = arr.length;i>0;i--){

arr[i] = arr[i-1];

}

arr[index]=key; // LEAVE THIS HERE

}

static int bSearch(int[] a, int count, int key)

{

// Y O U R C O D E I N H E R E

int low =0,high = a.length-1,mid = 0;

while(low

mid = (low + high)/2;

if(a[mid] == key){

count = mid;

return count;

}

else if(a[mid]

low = mid + 1;

}

else{

high = mid - 1;

}

}

return -(mid+1);

}

} // END PROJECT4

and this is the error message I am getting for this code:

Project4 produced RUNTIME ERRORS! Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Project4.insertInOrder(Project4.java:127) at Project4.main(Project4.java:45)

Update:

Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt

Be sure to put the ints filename before the words filename. The starter file will be expecting them in that order.

Lab#4's main method is completely written. Do not modify main. Just fill in the methods. Main will load a large arrays of int, and then load a large array of Strings. As usual the read loops for each file will be calling a resize method as needed. Once the arrays are loaded, each array will be tested for the presence of duplicates via calls to indexOfFirstDuplicate(). Do not do any trim operation.

Here are the rules: In your methods that look for the first occurrence of a duplicate in each array, you must sort the arrays before looking for the dupe. Do not search the array for the dupe until it is sorted. We require this because an unsorted array would require a quadratic algorithm (N squared via nested loops). If you sort you array first you will only incur an O(n*Log2n) cost for the sort with an additional O(n) for the search. This is the best that can be done without using a technology such as hashing which you will use for your next lab.

Inside your method to find the dupe you must sort the array first. Do not define/use any new type or data structure that we have not covered yet. Your traversal of the array looking for the dupe should require no more than one pass. Be as efficient as you can without breaking rule #2.

In your methods that look for the first occurrence of a duplicate in each array, you must sort the arrays before looking for the dupe. Do not search the array for the dupe until it is sorted. We require this because an unsorted array would require a quadratic algorithm (N squared via nested loops). If you sort you array first you will only incur an O(n*Log2n) cost for the sort with an additional O(n) for the search. This is the best that can be done without using a technology such as hashing which you will use for your next lab.

Assuming the array below, the 1st occurrence of a duplicate is at index 4, not 3, since the element at [4] is the first index position where that value was seen for a second time. Be sure you understand this. Do not report the index of the first occurrence of a value as being the index where the duplicate occurred.

Your job will be to fill in the definitions of these methods below main

1. static int[] upSizeArr( int[] fullArray ); // incoming array is FULL. It needs it's capacity doubled 2. static String[] upSizeArr( String[] fullArray ); // incoming array is FULL. It needs it's capacity doubled 3. static int indexOfFirstDupe( int[] arr, int count ); // returns ind of 1st occurrence of duplicate value 4. static int indexOfFirstDupe( String[] arr, int count ); // returns ind of 1st occurrence of duplicate value

The program you are given to start with: Lab4.java The input file of ints: 10,000ints.txt The input file of words: 172,822words.txt

ORIGINAL C13U 110] [90] [17][81] E71] [12] [11][81] 113] SORTED: [10] [11] [12] [13] [13][17] [71] [81] [81] [90] 1st occurance of any duplicate is at index 4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!