My assignment:
Create a class with a method that accept a charge account number as its argument. The method 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 a binary search to locate the number passed as an argument. Use the QuickSort algorithm to sort the array before the binary search is performed. If the number is in the array, the method should return true, indicating the number is valid. If the number is not in the array, the method should return false, indicating the number is invalid. Write a program that tests the class by asking the user to enter a charge account number. The program should display a message indicating whether the number is valid or invalid.

Is my prgram correct...
i know this is a stupid post but i would just like to be certain befor turning it in. Thnak you so much

MY CODE:

   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};

         quickSort(values);


         // Display the array's contents.
         System.out.println("\nSorted order: ");
         for (int element : values)
            System.out.print(element + " ");

         System.out.println();
         do
        {         
         int input;
         int result;
        String yesORno;
        String yes = "yes";
            // Get a value to search for.
         System.out.print("Enter a value to search for: ");
         input = scn.nextInt();


            // Search for the value
         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);
         }


            // Does the user want to search again?
         System.out.print("Do you want to search again? (Y or N): ");
         yesORno = scn.nextLine();
         }while (yesORno.equalsIgnoreCase(yes));


      }
   }

Recommended Answers

All 3 Replies

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("\nSorted order: ");
         for (int element : values)
            System.out.print(element + " ");

      
      	
      }
   }

this is a revised code with no compile errors
why wont it adhere to the do while loop and continue to ask the user
if they would like to continue the search

use

yOn = scn.next();

instead of

yOn = scn.nextLine();

at 196

I cant believe the solution wa so simple. Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.