Question

Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints...

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.

Given 10,000ints.txt & 172,822words.txt

Program given to start with
/* 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("\nusage: C:\\> java Lab4 <numbers file> <words filename>\n\n"); // 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\n",args[0],intList.length,intCount );
                int dupeIndex = indexOfFirstDupe( intList, intCount );
                if ( dupeIndex == NOT_FOUND )
                        System.out.format("No duplicate values found in intList\n");
                else
                        System.out.format("First duplicate value in intList found at index %d\n",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\n",args[1],wordList.length,wordCount );
                dupeIndex = indexOfFirstDupe( wordList, wordCount );
                if ( dupeIndex == NOT_FOUND )
                        System.out.format("No duplicate values found in wordList\n");
                else
                        System.out.format("First duplicate value in wordList found at index %d\n",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 */
                return null; // 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 */
                return null; // 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 */
                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 */
                return NOT_FOUND; // just to make it compile
        }

} // END CLASS LAB#4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

output:

intput Files:

1).10000ints.txt :

8
2
3
3
4
5
1
6
9
3
5
14

2). 172822words.txt​

ashd
sfdhjf
fnjgf
yreuir
cnjcnxc
ndjnhh
vfjfjvnjc
njchbdnhc
ashj
dbhfgyur
hjdjkgfj
jhruyj
fbdhbcnnc
mjfdjfjd
jfjhgru
zzhmfdh
fjkdf
dhsjs
fng

Program:

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("\nusage: C:\\> java Lab4 <numbers file> <words filename>\n\n"); // 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 &&intCount>0 )
intList = upSizeArr( intList );
intList[intCount++] = intFile.nextInt();
} //END WHILE intFile
intFile.close();
System.out.format( "%s loaded into intList array. size=%d, count=%d\n",args[0],intList.length,intCount );
int dupeIndex = indexOfFirstDupe( intList, intCount );
if ( dupeIndex == NOT_FOUND )
System.out.format("No duplicate values found in intList\n");
else
System.out.format("First duplicate value in intList found at index %d\n",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 &&wordCount > 0 )
wordList = upSizeArr( wordList );
wordList[wordCount++] = wordFile.readLine();
} //END WHILE wordFile
wordFile.close();
System.out.format( "%s loaded into word array. size=%d, count=%d\n",args[1],wordList.length,wordCount );
dupeIndex = indexOfFirstDupe( wordList, wordCount );
if ( dupeIndex == NOT_FOUND )
System.out.format("No duplicate values found in wordList\n");
else
System.out.format("First duplicate value in wordList found at index %d\n",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 */
String arr[]=new String[fullArr.length+5];
for(int i=0;i<fullArr.length;i++)
arr[i]=fullArr[i];
return arr; // 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 arr[]=new int[fullArr.length+5];
for(int i=0;i<fullArr.length;i++)
arr[i]=fullArr[i];
return arr; // just to make it compile
}

// use Arrays.sort() before scanning for dupe
static int indexOfFirstDupe( int[] arr, int count )
{   
Arrays.sort(arr);              
int t;
/* Y O U R C O D E H E R E */
for(int i=0;i<count;i++)
{
t=arr[i];
for(int j=i+1;j<count;j++)
{
if(t==arr[j])
return j;
}
}
return NOT_FOUND; // just to make it compile
}

// use Array.sort() before scanning for dupe
static int indexOfFirstDupe( String[] arr, int count )
{   
//Arrays.sort(arr);// it is Arrays. sort() not for strings
  
String t;
/* Y O U R C O D E H E R E */
for(int i=0;i<count;i++)
{
t=arr[i];
for(int j=i+1;j<count;j++)
{
if(t.equals(arr[j]))
return j;
}
}
return NOT_FOUND; // just to make it compile
}

} // END CLASS LAB#4

Add a comment
Know the answer?
Add Answer to:
Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[]...

    Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[] args) { // sort doubleArray into ascending order char [] A = {'g', ', 'y', 'a', 'e','d' }; Arrays.sort( A); for (char value: A) System.out.printf("%2C", value); System.out.printf("\n"); char [] A Copy = new char[ 3 ]; System.arraycopy( A, 2, A Copy, 0, A Copy.length); for(char value: A Copy) System.out.printf("%2C", value); System.out.printf("\n"); int location = Arrays.binary Search(A Copy, 'y'); if(location >=0) System.out.printf("y Found at element...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT