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...


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 require-
ments are different from the textbook code.
•Both search methods must use the Comparable 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.
Review the sample output given below to get an idea of what the program output should look like.



Enter the elements (search pool):<br>1 12 18 22 31 34 40 46 59 67 85<br>Target:<br>40<br># of comparisons for Linear Search: 4<br># of comparisons for Binary Search: 2<br>Enter the elements (search pool):<br>1 12 18 22 31 34 40 46 59 67 85<br>Target:<br>100<br>Not found!<br># of comparisons for Linear Search: 8<br>Not found!<br># of comparisons for Binary Search:<br>5<br>

Extracted text: Enter the elements (search pool): 1 12 18 22 31 34 40 46 59 67 85 Target: 40 # of comparisons for Linear Search: 4 # of comparisons for Binary Search: 2 Enter the elements (search pool): 1 12 18 22 31 34 40 46 59 67 85 Target: 100 Not found! # of comparisons for Linear Search: 8 Not found! # of comparisons for Binary Search: 5

Jun 04, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here