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

\"Design a program that asks the user to enter a series of 20 numbers. The progr

ID: 3624047 • Letter: #

Question

"Design a program that asks the user to enter a series of 20 numbers. The program should determine whether the number is valid by comparing it to the following list of valid charge account numbers:
5658845 4520125 7895122 8777541 8451277 1302850
8080152 4562555 5552012 5050552 7825877 1250255
1005231 6545231 3852085 7576651 7881200 4581002

These numbers should be stored in an array. Use the sequential search algortithm to locate the number entered by the user. If the number is in the array, the program should display a message indicating the number is valid. If the number is not in the array, the program should display a message indicating the number is invalid.
This assignment gives you a chance to use an array to check the input validity of an account number.
The required components for the assignment:
Software requirements
Algorithm
Hierarchy Chart
Pseudocode

Explanation / Answer

   import java.util.*;

    public class ProgramHW
   {
            /**
      The quickSort method calls the doQuickSort method
      to sort an int array.
      @param array The array to sort.
      */
     
       public static void quickSort(int array[])
      {
         doQuickSort(array, 0, array.length - 1);
      }
     
      /**
      The doQuickSort method uses the QuickSort algorithm
      to sort an int array.
      @param array The array to sort.
      @param start The starting subscript of the list to sort
      @param end The ending subscript of the list to sort
      */
     
       private static void doQuickSort(int array[], int start, int end)
      {
         int pivotPoint;
        
         if (start < end)
         {
            // Get the pivot point.
            pivotPoint = partition(array, start, end);
           
            // Sort the first sub list.
            doQuickSort(array, start, pivotPoint - 1);
           
            // Sort the second sub list.
            doQuickSort(array, pivotPoint + 1, end);
         }
      }
     
      /**
      The partiton method selects a pivot value in an array
      and arranges the array into two sub lists. All the
      values less than the pivot will be stored in the left
      sub list and all the values greater than or equal to
      the pivot will be stored in the right sub list.
      @param array The array to partition.
      @param start The starting subscript of the area to partition.
      @param end The ending subscript of the area to partition.
      @return The subscript of the pivot value.
      */
     
       private static int partition(int array[], int start, int end)
      {
         int pivotValue;    // To hold the pivot value
         int endOfLeftList; // Last element in the left sub list.
         int mid;           // To hold the mid-point subscript
        
         // Find the subscript of the middle element.
         // This will be our pivot value.
         mid = (start + end) / 2;
        
         // Swap the middle element with the first element.
         // This moves the pivot value to the start of
         // the list.
         swap(array, start, mid);
        
         // Save the pivot value for comparisons.
         pivotValue = array[start];
        
         // For now, the end of the left sub list is
         // the first element.
         endOfLeftList = start;
        
         // Scan the entire list and move any values that
         // are less than the pivot value to the left
         // sub list.
         for (int scan = start + 1; scan <= end; scan++)
         {
            if (array[scan] < pivotValue)
            {
               endOfLeftList++;
               swap(array, endOfLeftList, scan);
            }
         }
        
         // Move the pivot value to end of the
         // left sub list.
         swap(array, start, endOfLeftList);
        
         // Return the subscript of the pivot value.
         return endOfLeftList;
      }
     
      /**
      The swap method swaps the contents of two elements
      in an int array.
      @param The array containing the two elements.
      @param a The subscript of the first element.
      @param b The subscript of the second element.
      */
     
       private static void swap(int[] array, int a, int b)
      {
         int temp;
        
         temp = array[a];
         array[a] = array[b];
         array[b] = temp;
      }
    
  
  
  
     
       public static int search(int[] array, int value)
      {
         return binarySearch(array, 0, array.length - 1, value);
      }
     
      /**
      The binarySearch method performs a recursive binary
      search on an integer array.
      @param array The array to search.
      @param first The first element in the search range.
      @param last The last element in the search range.
      @param value The value to search for.
      @return The subscript of the value if found, otherwise -1.
      */
     
       private static int binarySearch(int[] array, int first,
                                     int last, int value)
      {
         int middle;     // Mid point of search
        
         // Test for the base case where the
         // value is not found.
         if (first > last)
            return -1;
        
         // Calculate the middle position.
         middle = (first + last) / 2;
        
         // Search for the value.
         if (array[middle] == value)
            return middle;
         else if (array[middle] < value)
            return binarySearch(array, middle + 1,
                             last, value);
         else
            return binarySearch(array, first,
                             middle - 1, value);
      }
     
     
  
       public static void main(String [] args)    
      {
         Scanner scn = new Scanner(System.in);        
         // Create an int array with test values.
         int[] values = { 5658845,4520125,7895122,8777541,8451277,1302850,
                              8080152,4562555,5552012,5050552,7825877,1250255,
                              1005231,6545231,3852085,7576651,7881200,4581002};
         int input;
         int result;
         String yOn;
         String y = "y";
         quickSort(values);
               
        
                
         System.out.println();
        
         do
         {        
            // Get a value to search for.
            System.out.print("Enter a value to search for: ");
            input = scn.nextInt();
           
           
            // Search for the value
            result = search(values, input);
           
            // Display the results.
            if (result == -1)
               System.out.println(input + " was not found.");
            else
            {
               System.out.println(input + " was found at " +
                              "element " + result+1);
            }
           
          
            // Does the user want to search again?
            System.out.print("Do you want to search again? (Y or N): ");
            yOn = scn.nextLine();
         }while (yOn.equalsIgnoreCase(y));
        
          // Display the array's contents.
         System.out.println(" Sorted order: ");
         for (int element : values)
            System.out.print(element + " ");

     
         
      }
   }