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

I need help with this program for java. The program you are given to start with:

ID: 3790869 • Letter: I

Question

I need help with this program for java.

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

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.

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

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

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

static int indexOfFirstDupe( int[] arr, int count ); // returns ind of 1st occurrence of duplicate value

static int indexOfFirstDupe( String[] arr, int count ); // returns ind of 1st occurrence of duplicate value

Here is the starting file!

Explanation / Answer

HI, Please find my implementation of all required methods.

Please let me know in case of any issue.

/* Lab4.java Reads two files into two arrays then checks both arrays for dupes */

import java.io.*;

import java.util.*;

public class Lab4

{

   static final int INITIAL_CAPACITY = 10;

   static final int NOT_FOUND = -1; // indexOfFirstDupe returns this value if no dupes found

   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 Lab4 <numbers file> <words filename> "); // i.e. C:> java Lab4 10000ints.txt 172822words.txt

           System.exit(0);

       }

       String[] wordList = new String[INITIAL_CAPACITY];

       int[] intList = new int[INITIAL_CAPACITY];

       int wordCount = 0, intCount=0;

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

       BufferedReader wordFile = new BufferedReader( new FileReader(args[1]) );

       // P R O C E S S I N T F I L E

       while ( intFile.hasNextInt() ) // i.e. while there are more ints in the file

       { if ( intCount == intList.length )

           intList = upSizeArr( intList );

       intList[intCount++] = intFile.nextInt();

       } //END WHILE intFile

       intFile.close();

       System.out.format( "%s loaded into intList array. size=%d, count=%d ",args[0],intList.length,intCount );

       int dupeIndex = indexOfFirstDupe( intList, intCount );

       if ( dupeIndex == NOT_FOUND )

           System.out.format("No duplicate values found in intList ");

       else

           System.out.format("First duplicate value in intList found at index %d ",dupeIndex);

       // P R O C E S S S T R I N G F I L E

       while ( wordFile.ready() ) // i.e. while there is another line (word) in the file

       { if ( wordCount == wordList.length )

           wordList = upSizeArr( wordList );

       wordList[wordCount++] = wordFile.readLine();

       } //END WHILE wordFile

       wordFile.close();

       System.out.format( "%s loaded into word array. size=%d, count=%d ",args[1],wordList.length,wordCount );

       dupeIndex = indexOfFirstDupe( wordList, wordCount );

       if ( dupeIndex == NOT_FOUND )

           System.out.format("No duplicate values found in wordList ");

       else

           System.out.format("First duplicate value in wordList found at index %d ",dupeIndex);

   } // END MAIN

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

   // FYI. Methods that don't say private are by default, private.

   // copy/adapt your working code from lab3 || project3

   static String[] upSizeArr( String[] fullArr )

   {

       /* Y O U R C O D E H E R E */

       int currentSize = fullArr.length;

       int newSize = currentSize*2;

       // creating an array

       String[] newArr = new String[newSize];

       // copying valus from fullArr to newArr

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

           newArr[i] = fullArr[i];

       return newArr; // just to make it compile

   }

   // copy/adapt your working code from lab3 || project3

   static int[] upSizeArr( int[] fullArr )

   {

       /* Y O U R C O D E H E R E */

       int currentSize = fullArr.length;

       int newSize = currentSize*2;

       // creating an array

       int[] newArr = new int[newSize];

       // copying valus from fullArr to newArr

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

           newArr[i] = fullArr[i];

       return newArr; // just to make it compile

   }

   // use Arrays.sort() before scanning for dupe

   static int indexOfFirstDupe( int[] arr, int count )

   {

       /* Y O U R C O D E H E R E */

       for(int i=0; i<count-1; i++){

           for(int j=i+1; j<count; j++){

               if(arr[i] == arr[j])

                   return i;

           }

       }

       return NOT_FOUND; // just to make it compile

   }

   // use Array.sort() before scanning for dupe

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

   {

       /* Y O U R C O D E H E R E */

       for(int i=0; i<count-1; i++){

           for(int j=i+1; j<count; j++){

               if(arr[i].equals(arr[j]))

                   return i;

           }

       }

       return NOT_FOUND; // just to make it compile

   }

} // END CLASS LAB#4