SHARE
TWEET

Untitled

a guest Nov 14th, 2017 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5. import java.util.Arrays;
  6.  
  7. public class Lab9
  8. {
  9.     public static void main(String[] args) throws IOException
  10.     {
  11.         final int MAX_COUNT = 30;
  12.  
  13.         if (args.length  != 2)
  14.         {
  15.             System.out.println("Usage:  java Lab8 <filename> integer-search-value");
  16.             System.exit(0);
  17.         }
  18.  
  19.         Scanner infile = new Scanner(new File(args[0]));
  20.  
  21.         int[] array = new int[MAX_COUNT];
  22.         int count = 0;
  23.         while (infile.hasNextInt())
  24.         {
  25.             array[count] = infile.nextInt();
  26.             ++count;
  27.         }
  28.         showArray(array, count);
  29.         System.out.println("Array Capacity: " + array.length);
  30.         System.out.println("Array In-Use Count: " + count);
  31.  
  32.     System.out.println("Original Array: Is array sorted? " + isSorted(array));
  33.  
  34.         Arrays.sort(array);
  35.     System.out.println("Modified Array: Is array sorted? " + isSorted(array));
  36.         // TODO: 4. Extract the search value from args....
  37.     int searchValue = args[2];
  38.         // TODO: 5. Add in the call to search the array using the Java Arrays.binarySearch
  39.     int index = Arrays.binarySearch(array, searchValue);
  40.     if(index > 0 && index < array.length )
  41.     {
  42.         System.out.println("The element " + searchValue + " was found at index " + index + ".");
  43.     }
  44.     else
  45.     {
  46.         System.out.println("Error. The element " + searchValue + "was not found.");
  47.     }
  48.        
  49.         //          Print out a message indicating if the element was found or not found.
  50.     }
  51.  
  52.     public static boolean isSorted(int[]arr)
  53.     {
  54.         if(arr == null)
  55.         {
  56.             return false;
  57.         }
  58.         else if(arr.length == 0)
  59.         {
  60.             return true;
  61.         }
  62.         for(int i = 0; i < arr.length-1; i++)
  63.         {
  64.             if (arr[i] > arr[i+1])
  65.             {
  66.                 return false;
  67.             }
  68.         }
  69.         return true;
  70.     }
  71.     public static void showArray(int[] arr, int count)
  72.     {
  73.         System.out.println("Array has  " + count + " in use elements");
  74.         System.out.print("Elements: ");
  75.         for (int ii = 0; ii < count; ++ii)
  76.             System.out.print(arr[ii] + " ");
  77.  
  78.         System.out.println();
  79.     }
  80. }
RAW Paste Data
Top