You will use the P10Class here:

public class P10Class

public class P10Class
{
  public static int SEQ_NUM;
  public static int BIN_NUM;

  public static int seq_search(int[] data, int key)
  {
    boolean found = false;
    int i = 0;

    SEQ_NUM = 0;

    while ( !found && (i < data.length) )
    {
      if ( data[i] == key )
        found = true;
      else
        i++;

      SEQ_NUM++;
    }

    if ( found )
      return i;
    else
      return -1;
  }

  public static int bin_search(int[] data, int key)
  {
    boolean found = false;
    int midpoint = 0, first = 0, last;

    BIN_NUM = 0;
    last = data.length - 1;

    while ( ( first <= last ) && !found )
    {
      midpoint = (first + last) / 2;
      if ( data[midpoint] == key )
        found = true;
      else if ( data[midpoint] > key )
        last = midpoint - 1;
      else
        first = midpoint + 1;

      BIN_NUM++;
    }

    if ( found )
      return midpoint;
    else
      return -1;
  }
}

This class is designed to test the efficiency of the two search models.

You will simultaneously populate both arrays with the same random numbers. This will guarantee that both arrays have the exact same content. Sort only ONE array using Arrays.sort(). This sorted array will be used with the bin_search() method and the unsorted array will be used with seq_search().

Write code to read integers from System.in until EOF. With each integer search each array using seq_search() and bin_search(). Report the number of elements examined for each search method, whether or not the item was found and where in the array you found it.

The number of array elements examined is stored in static instance variables. These variables for sequential and binary search are SEQ_NUM and BIN_NUM respectively. The appropriate variable is set when the corresponding search method is called.

Your output should resemble the following:

1000 random numbers from 0 to 999 have been generated

Enter an integer to search the arrays (EOF to stop): 467
sequential search found the value 467 in element 500 with 501 items scanned.

Enter an integer to search the arrays (EOF to stop): 999
sequential search found the value 999 in element 475 with 476 items scanned.

binary search found the value 999 in element 998 with 9 items scanned. 

Enter an integer to search the arrays (EOF to stop): 34
sequential search did not find the value with 1000 items scanned.

binary search did not find the value with 10 items scanned. 

Enter an integer to search the arrays (EOF to stop): 145
sequential search found the value 145 in element 499 with 500 items scanned.

binary search found the value 145 in element 141 with 9 items scanned.

Enter an integer to search the arrays (EOF to stop): CTRL-D

Here is my code....what am I doing incorrectly?? Thanks for any help.

import java.util.*;

public class p10 { 

 public static void printArray(int[] a) {
         for (int i = 0; i < a.length; i++) {
                 System.out.println(a[i]);
         }
         System.out.println();
         System.out.println();
}
 static Scanner kb = new Scanner(System.in);
public static void main(String []args)
{
 int[] bArray = new int[1000];
 int[] sArray = new int[1000];
 int num ;
 int integer = 0;

  for (num = 0; num < bArray.length;num++)
  {
  int randNumber = (int) (Math.random() * 1000 +1);
  bArray[num] = randNumber;
  sArray[num] = randNumber;
  }
Arrays.sort(bArray);


System.out.println("1000 random numbers 0 to 999 have been generated"
     +"\n\t (Very grudgingly might I add!)"
     +"\n");
System.out.print("Enter an integer to search the arrays (EOF to stop):");

while ( kb.hasNext() )
 {
do{
   try{
       valid = true;
      integer = kb.nextInt();
      }
   catch(Exception e){
       valid = false;

      integer = kb.nextInt();
     }
 } while (!valid);
}
int bIndex = bin_search(bArray, integer);

if(bIndex < 0){
   System.out.println("Binary search did not find the value with " + BIN_NUM + " items scanned.");
}
else {
  System.out.println("Binary search found the value "+ integer +" in element " + bIndex + " with " + BIN_NUM + 
                     " items scanned.");
}
int sIndex = seq_search(sArray, integer);

if (sIndex < 0){
 System.out.println("Sequntial search did not find the value with " + SEQ_NUM + " items scanned.");
}
else {
 System.out.println("sequential search found the value "+ integer +" in element " + sIndex + " with " + SEQ_NUM + 
                    " items scanned.");
}
System.out.print("\nEnter an interger to search the arrays (EOF to stop):");
}

}

Why do you think you are doing anything wrong? What hapens when you run it?

I'm geeting errors on lines 32, 36, 39, 41, 43, 46, 49, 51, and 54. I've been racking my brain. And I can't figure out why. I just want my output as....

1000 random numbers from 0 to 999 have been generated

Enter an integer to search the arrays (EOF to stop): 467
sequential search found the value 467 in element 500 with 501 items scanned.
binary search found the value 467 in element 461 with 9 items scanned.

Enter an integer to search the arrays (EOF to stop): 999
sequential search found the value 999 in element 475 with 476 items scanned.
binary search found the value 999 in element 998 with 9 items scanned.

Enter an integer to search the arrays (EOF to stop): 34
sequential search did not find the value with 1000 items scanned.
binary search did not find the value with 10 items scanned.

Enter an integer to search the arrays (EOF to stop): 145
sequential search found the value 145 in element 499 with 500 items scanned.
binary search found the value 145 in element 141 with 9 items scanned.

Enter an integer to search the arrays (EOF to stop): CTRL-D

Any help would be great. Thanks again.

"errors"... what errors exactly?

9 errors found:
File: C:\Desktop\Dr Java\p10.java [line: 32]
Error: valid cannot be resolved to a variable
File: C:\Desktop\Dr Java\p10.java [line: 36]
Error: valid cannot be resolved to a variable
File: C:\Desktop\Dr Java\p10.java [line: 39]
Error: valid cannot be resolved to a variable
File: C:\Desktop\Dr Java\p10.java [line: 41]
Error: The method bin_search(int[], int) is undefined for the type p10
File: C:\Desktop\Dr Java\p10.java [line: 43]
Error: BIN_NUM cannot be resolved to a variable
File: C:\Desktop\Dr Java\p10.java [line: 46]
Error: BIN_NUM cannot be resolved to a variable
File: C:\Desktop\Dr Java\p10.java [line: 49]
Error: The method seq_search(int[], int) is undefined for the type p10
File: C:\Desktop\Dr Java\p10.java [line: 51]
Error: SEQ_NUM cannot be resolved to a variable
File: C:\Desktop\Dr Java\p10.java [line: 54]
Error: SEQ_NUM cannot be resolved to a variable

Thank you.

Every one of those refers to a variable or method name that the compiler does not recognise - maybe because you forgot to declare it, or because it's in a different class and you tried to use it as if it was in your class

I suck, my brain is fried.

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.