import java.io.*; import java.util.*; public class Program5 { private static fin
ID: 3651744 • Letter: I
Question
import java.io.*;
import java.util.*;
public class Program5
{
private static final int NOT_FOUND = -1;
public static void main( String[] args ) throws Exception
{
if (args.length < 2 )
{
System.out.println("CMD LINE INPUT ERROR: Must enter two filenames on command line ");
System.exit(0);
}
Scanner infile1 = new Scanner( new File(args[0]) ); // fill an array from this file using insertInOrder
Scanner infile2 = new Scanner( new File(args[1]) ); // fill an array from this file using selectionSort
int[] array = new int[20]; // Array can hold 20 ints - your input files will not fill the array
int arrCnt = 0; // num of values put into the array so far.
// READ THE NUMBERS FROM FILE1 INTO THE ARRAY USING INSERT IN ORDER
System.out.print("ORIGINAL ORDER " + args[0] + " " );
while ( infile1.hasNextInt() )
{
int n = infile1.nextInt();
System.out.print( n + " " ); // echo the numbers as read from the file
insertInOrder( array, arrCnt, n ); // insert number into the array at the position it belongs
arrCnt++; // update counter
}
infile1.close();
System.out.println();
System.out.print("INSERT IN ORDER " + args[0] + " " );
printArray( array, arrCnt ); // must come out in sorted order
// READ THE NUMBERS FROM FILE2 INTO THE ARRAY. THEN SORT USING BUBBLE SORT
arrCnt = 0; // very important to reset the count since we are re-using the same array space
while ( infile2.hasNextInt() )
array[ arrCnt++ ] = infile2.nextInt();
infile2.close();
System.out.print("ORIGINAL ORDER " + args[1] + " " );
printArray( array, arrCnt ); // must come out in original order as in file2
// now sort array after loading it
bubbleSort( array, arrCnt );
System.out.print("BUBBLE SORT " + args[1] + " " );
printArray( array, arrCnt ); // must come out in sorted order
} // END MAIN
// ########################################################################################
// USE THIS METHOD AS GIVEN: DO NOT CHANGE
private static void printArray( int[] arr, int cnt )
{
for( int i=0 ; i<cnt ;++i )
System.out.printf("%d ", arr[i] );
System.out.println();
}
// YOU WRITE THIS METHOD
private static void insertInOrder( int[] arr, int cnt, int n )
{
// YOUR CODE HERE
}
// YOU WRITE THIS METHOD
private static void bubbleSort( int[] arr, int cnt )
{
// YOUR CODE HERE
}
} // END PROGRAM5
please it's due tomorrow
Explanation / Answer
import java.io.*; import java.util.*; public class Program5 { private static final int NOT_FOUND = -1; public static void main( String[] args ) throws Exception { if (args.length < 2 ) { System.out.println(" CMD LINE INPUT ERROR: Must enter two filenames on command line "); System.exit(0); } Scanner infile1 = new Scanner( new File(args[0]) ); // fill an array from this file using insertInOrder Scanner infile2 = new Scanner( new File(args[1]) ); // fill an array from this file using selectionSort int[] array = new int[20]; // Array can hold 20 ints - your input files will not fill the array int arrCnt = 0; // num of values put into the array so far. // READ THE NUMBERS FROM FILE1 INTO THE ARRAY USING INSERT IN ORDER System.out.print(" ORIGINAL ORDER " + args[0] + " " ); while ( infile1.hasNextInt() ) { int n = infile1.nextInt(); System.out.print( n + " " ); // echo the numbers as read from the file insertInOrder( array, arrCnt, n ); // insert number into the array at the position it belongs arrCnt++; // update counter } infile1.close(); System.out.println(); System.out.print("INSERT IN ORDER " + args[0] + " " ); printArray( array, arrCnt ); // must come out in sorted order // READ THE NUMBERS FROM FILE2 INTO THE ARRAY. THEN SORT USING BUBBLE SORT arrCnt = 0; // very important to reset the count since we are re-using the same array space while ( infile2.hasNextInt() ) array[ arrCnt++ ] = infile2.nextInt(); infile2.close(); System.out.print("ORIGINAL ORDER " + args[1] + " " ); printArray( array, arrCnt ); // must come out in original order as in file2 // now sort array after loading it bubbleSort( array, arrCnt ); System.out.print("BUBBLE SORT " + args[1] + " " ); printArray( array, arrCnt ); // must come out in sorted order } // END MAIN // ######################################################################################## // USE THIS METHOD AS GIVEN: DO NOT CHANGE private static void printArray( int[] arr, int cnt ) { for( int i=0 ; i 0) && (arr[cnt-1] > n)) { arr[cnt] = arr[cnt-1]; cnt--; } arr[cnt] = n; } // YOU WRITE THIS METHOD private static void bubbleSort( int[] arr, int cnt ) { int i, j,t=0; for(i = 0; i arr[j]) { t = arr[j-1]; arr[j-1]=arr[j]; arr[j]=t; } } } } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.