//Need help fixing some issues with a project. PLEASE read below Write a Java program that implements both Linear Search
Posted: Sun May 15, 2022 1:55 pm
//Need help fixing some issues with a project. PLEASE read
below
Write a Java program that implements both Linear Search and
Binary Search. The program will take a collection of objects
(generic type data) as input and print the number of comparisons
needed to find a target element within that collection. You will
create the following two Java classes:
1. SearchCombo.java : Code for both linearSearch and binarySearch
will be in this class. You may take help from the textbook Chapter
9, Section 9.1. However, note that the design requirements are
different from the textbook code.
•Both search methods must use the Comparable<T> interface and
the compareTo() method.
•Your program must be able to handle different data types, i.e.,
use generics.
•For binarySearch, if you decide to use a midpoint computation
formula that is different from the textbook, explain that formula
briefly as a comment within your code.
2. Tester.java : This class will contain the main() method. The
user will be asked to enter the collection of elements (comma or
space can be used to separate the elements). The output will be the
number of comparisons the two searching techniques require.
//my code
import java.util.Scanner;
public class SearchCombo{
public static int binarySearch(int array[], int element, int
low, int high){
int step = 0;
while (low <= high){
step++;
int mid = low +(high - low)/2;
if (array[mid] == element){
System.out.println("# of comparisons
for binary search: " + step);
return mid;
}
if (array[mid] < element)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static int linearSearch(int array[], int
element){
int flag = 0, step = 0;
for (int i = 0; i < array.length; i++){
step++;
if (array == element){
flag = 1;
System.out.println("\n# of comparisons
for linear search: " + step);
}
}
if (flag == 1){
return 1;
} else
return -1;
}
public static void main(String args[]){
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int flag = 1;
System.out.print("Enter the number of element of the
array: ");
int n = input.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements (search pool):
");
for (int i = 0; i < n; i++)
array = input.nextInt();
while (flag == 1) {
System.out.print("Enter target: ");
int element = input.nextInt();
int result = binarySearch(array, element, 0, n
- 1);
int result1 = linearSearch(array,
element);
if (result == -1 && result1 ==
-1)
System.out.println("\nUnable to find
the element in the array
");
}
}
}
//need help getting the number of comparisons corrected.
below
Write a Java program that implements both Linear Search and
Binary Search. The program will take a collection of objects
(generic type data) as input and print the number of comparisons
needed to find a target element within that collection. You will
create the following two Java classes:
1. SearchCombo.java : Code for both linearSearch and binarySearch
will be in this class. You may take help from the textbook Chapter
9, Section 9.1. However, note that the design requirements are
different from the textbook code.
•Both search methods must use the Comparable<T> interface and
the compareTo() method.
•Your program must be able to handle different data types, i.e.,
use generics.
•For binarySearch, if you decide to use a midpoint computation
formula that is different from the textbook, explain that formula
briefly as a comment within your code.
2. Tester.java : This class will contain the main() method. The
user will be asked to enter the collection of elements (comma or
space can be used to separate the elements). The output will be the
number of comparisons the two searching techniques require.
//my code
import java.util.Scanner;
public class SearchCombo{
public static int binarySearch(int array[], int element, int
low, int high){
int step = 0;
while (low <= high){
step++;
int mid = low +(high - low)/2;
if (array[mid] == element){
System.out.println("# of comparisons
for binary search: " + step);
return mid;
}
if (array[mid] < element)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static int linearSearch(int array[], int
element){
int flag = 0, step = 0;
for (int i = 0; i < array.length; i++){
step++;
if (array == element){
flag = 1;
System.out.println("\n# of comparisons
for linear search: " + step);
}
}
if (flag == 1){
return 1;
} else
return -1;
}
public static void main(String args[]){
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int flag = 1;
System.out.print("Enter the number of element of the
array: ");
int n = input.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements (search pool):
");
for (int i = 0; i < n; i++)
array = input.nextInt();
while (flag == 1) {
System.out.print("Enter target: ");
int element = input.nextInt();
int result = binarySearch(array, element, 0, n
- 1);
int result1 = linearSearch(array,
element);
if (result == -1 && result1 ==
-1)
System.out.println("\nUnable to find
the element in the array
}
}
}
//need help getting the number of comparisons corrected.