turt2live

Untitled

Jun 26th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1.  
  2. import java.util.*;
  3. import java.util.Scanner;
  4.  
  5. public class BinarySearch {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner menuScan = new Scanner(System.in);
  10.         int index;
  11.  
  12.  
  13.  
  14.         int[] intArray = new int[12];
  15.  
  16.         intArray[0] = 12;
  17.         intArray[1] = 13;
  18.         intArray[2] = 32;
  19.         intArray[3] = 35;
  20.         intArray[4] = 46;
  21.         intArray[5] = 68;
  22.         intArray[6] = 71;
  23.         intArray[7] = 73;
  24.         intArray[8] = 88;
  25.         intArray[9] = 94;
  26.         intArray[10] = 99;
  27.         intArray[11] = 100;
  28.  
  29.         String[] critterArray = new String[10];
  30.  
  31.         critterArray[0] = "Chimaera";
  32.         critterArray[1] = "Clownfish";
  33.         critterArray[2] = "Dugong";
  34.         critterArray[3] = "Eel";
  35.         critterArray[4] = "Fangtooth";
  36.         critterArray[5] = "Grenadier"; //not the soldier, the fish!
  37.         critterArray[6] = "Isopod";
  38.         critterArray[7] = "Lobster";
  39.         critterArray[8] = "Viperfish";
  40.         critterArray[9] = "Whale";
  41.  
  42.         String[] critterTypeArray = new String[10];
  43.  
  44.         critterTypeArray[0] = "Chimaera, fish";
  45.         critterTypeArray[1] = "Clownfish, fish";
  46.         critterTypeArray[2] = "Dugong, Pokemon";
  47.         critterTypeArray[3] = "Eel, fish";
  48.         critterTypeArray[4] = "Fangtooth, fish";
  49.         critterTypeArray[5] = "Grenadier, fish";
  50.         critterTypeArray[6] = "Isopod, crustacean";
  51.         critterTypeArray[7] = "Lobster, crustacean";
  52.         critterTypeArray[8] = "Viperfish, fish";
  53.         critterTypeArray[9] = "Whale, mammal";
  54.  
  55.         System.out.println("Please enter the designated number of the method you want to access:"
  56.                 + "/n Enter 1 for a boolean integer check" //foundInt method
  57.                 + "/n Enter 2 to search the index number of an integer" //binarySearch method
  58.                 + "/n Enter 3 for a boolean String check on a list of sea creatures" //foundString method
  59.                 + "/n Enter 4 to search the index number of a sea creature");//binarySearch method, vString      
  60.  
  61.         int choice = menuScan.nextInt();
  62.  
  63.         if (choice == 1) {
  64.  
  65.             System.out.println("Please enter the number you wish to search:");
  66.             int searchTerm = menuScan.nextInt();
  67.  
  68.  
  69.                 if(foundInt(intArray, searchTerm)){
  70.                
  71.                
  72.                 }
  73.  
  74.  
  75.         }
  76.         if (choice == 2) {
  77.  
  78.             System.out.println("Please enter the number you wish to search:");
  79.             int searchTerm = menuScan.nextInt();
  80.  
  81.             index = binarySearch(intArray, searchTerm);
  82.             if (index != -1) {
  83.                 System.out.println("I found it at index number: " + index);
  84.             } else {
  85.                 System.out.println("I am sorry, this number is not in our list. Please try again later");
  86.             }
  87.  
  88.         }
  89.         if (choice == 3) {
  90.  
  91.             System.out.println("Please enter the name of the sea creature you wish to search:");
  92.             String searchTerm = menuScan.nextLine();
  93.  
  94. //        }
  95. //        if (choice == 4) {
  96. //
  97. //            System.out.println("Please enter the name of the sea creature you wish to search:");
  98. //            String searchTerm = menuScan.nextLine();
  99. //
  100. //            index = binarySearch(critterTypeArray, searchTerm);
  101. //            if (index != -1) {
  102. //                System.out.println("I found it at index number: " + index);
  103. //            } else {
  104. //                System.out.println("I am sorry, this critter is not in our list. Please try again later");
  105. //            }
  106.  
  107.         } else {
  108.  
  109.             System.out.println("Invalid entry; Please try again later.");
  110.  
  111.         }
  112.  
  113.     }//end main
  114.  
  115.     public static boolean foundInt(int intArray[], int searchTerm) {
  116.  
  117.         int start, end, midPt;
  118.         start = 0;
  119.         end = intArray.length - 1;
  120.         while (start <= end) {
  121.             midPt = (start + end) / 2;
  122.             if (intArray[midPt] == searchTerm) {
  123.                 return true;
  124.             } else if (intArray[midPt] < searchTerm) {
  125.                 start = midPt + 1;
  126.             } else {
  127.                 end = midPt - 1;
  128.             }
  129.         }
  130.         return false;
  131.  
  132.     }//end foundInt
  133.  
  134.     static int binarySearch(int intArray[], int searchTerm) {
  135.  
  136.         int start, end, midPt;
  137.         start = 0;
  138.         end = intArray.length - 1;
  139.         while (start <= end) {
  140.             midPt = (start + end) / 2;
  141.             if (intArray[midPt] == searchTerm) {
  142.                 return midPt;
  143.             } else if (intArray[midPt] < searchTerm) {
  144.                 start = midPt + 1;
  145.             } else {
  146.                 end = midPt - 1;
  147.             }
  148.         }
  149.         return -1;
  150.  
  151.     }//end binarySearch
  152.  
  153.     public boolean foundString(String critterArray[], String searchTerm) {
  154.  
  155.         int start, end, midPt;
  156.         start = 0;
  157.         end = critterArray.length - 1;
  158.         while (start <= end) {
  159.             midPt = (start + end) / 2;
  160.             System.out.println("Testing the results of compareTo: "+critterArray[midPt].compareTo(searchTerm));
  161.             if (critterArray[midPt] == searchTerm) {
  162.                 return true;
  163.             } else if (critterArray[midPt].compareTo(searchTerm) < 0) {
  164.                 start = midPt + 1;
  165.             } else {
  166.                 end = midPt - 1;
  167.             }
  168.         }
  169.         return false;
  170.  
  171.     }//end foundString
  172. //    
  173. //    public int binarySearch (String critterTypeArray[], int searchTerm){
  174. //  
  175. //      **Due to numerous and bizarre errors this method has been scrapped.**
  176. //  
  177. //    }
  178. }//end class
Advertisement
Add Comment
Please, Sign In to add comment