Question
JAVA HELP Create a project that uses the Binary Search algorithm to search for objects of type Circle, Fraction, FeetInches, Integer, Rectangle. In order to
JAVA HELP
Create a project that uses the Binary Search algorithm to search for objects of type Circle, Fraction, FeetInches, Integer, Rectangle. In order to do this you will need to add your Circle, Rectangle, FeetInches, and Fraction. Make sure they each have a compareTo.
In main you will need to create 4 arrays. An array of 12 rectangles, an array of 20 circles, an array of 24 Fractions, and an array of 16 FeetInches.
I have attached an input file that contains data from which you will be able to create these arrays.
First, the length and width of the 12 rectangles, then 20 reals for the radii of the 20 Circles, then 24 numerator/denominator pairs to create the 24 Fractions, then 16 feet/inches pairs for the FeetInches objects. Note that they are sorted, which is necessary for the Binary Search algorithm.
Print out the contents of your arrays (this will help you to debug)
Create FOUR binary search methods ALL WITH THE SAME NAME, but the parameters will be different in that they will be of different types).
Then ask your user for TWO length/width pairs. (For debugging purposes enter one which is in the array, one which is not. Since you printed out the contents of your array you can easily find which are in the array and the index). If in the array your binary search algorithm should return the index where it is, if not it should return -1.
Then ask your user for TWO radii. If in the array your binary search algorithm should return the index where it is, if not it should return -1.
Then ask your user for TWO numerator/denominator pairs. If in the array your binary search algorithm should return the index where it is, if not it should return -1.
Then ask your user for TWO feet/inches pairs. If in the array your binary search algorithm should return the index where it is, if not it should return -1.
Attached is an input file(BinarySearch.in) and also a project with the binary search algorithm with an array of ints (BS.java).
BS.java:
package bs;
public class BS {
public static void main(String[] args) { int[]A={3,5,7,8,10,13,15,20,35,32,50}; int pos=binarySearch(A, 20, 0, 10); //look for 20 System.out.println("20 is found at position " + pos); } public static int binarySearch(int numbers [], int key, int low, int high) { int mid = 0; // Midpoint of low and high values int pos=0; // Position where item found, -1 if not found int rangeSize = (high - low) + 1; // Remaining range of values to search for match mid = (high + low) / 2;
if (key==numbers[mid]) { // Base case 1: item found at midVal position pos = mid; } else if (rangeSize == 1) { // Base case 2: match not found pos = -1; } else { // Recursive case: search lower or upper half if (key
return pos; }
}
BinarySearch.in:
Binary Search in History Source 1.5 1.0 1.6 2.0 2.2 2.9 2.3 3.0 3.2 3.5 3.4 4.0 4.0 4.0 4.0 4.2 4.0 4. 4 5.0 5 2 5.9 6.0 6.2 7.0 1 2 1. 1 1.3 2.5 2.6 2.9 3.3 3.5 3.6 3.8 4.1 4.2 4.7 4.8 4.9 5.1 5.3 5.7 5.8 5.9 6.0 3 1 16 2 16 3 16 4 16 5 16 6 16 7 16 8 16 9 16 10 16 17 16 18 16 19 16 20 16 21 16 22 16 23 16 24 16 25 16 26 16 27 16 28 16 29 16 30 16 5 1 16 2 16 3 16 4 16 5 16 6 16 7 16 8 16 9 16 10 16 17 16 18 16 19 16 20 16 29 16 30 16
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started