Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

IN JAVA: Create a project that uses the Binary Search algorithm to search for objects of type Circle, Fraction, FeetInches, Integer, Rectangle. 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.

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

BinarySearch.in:

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

What I have so far :

// method to read file
public static void file() {
//read file BinarySearch.in
File inFile = new File("BinarySearch.in");

Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
}
  
}

//binarysearch
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 < numbers[mid]) { // Search lower half, recursive call
pos = binarySearch(numbers, key, low, mid);
}
else { // Search upper half, recursive call
pos = binarySearch(numbers, key, mid + 1, high);
}
}

return pos;
  
}
  
public static void main(String[] args) {
//scanner
Scanner scnr= new Scanner(System.in);
  
//rectangle array 12
Rectangle[] rectangles=new Rectangle[12];
//circle array 20
Circle[] circles=new Circle[20];
//fraction array 24
Fraction[] fractions=new Fraction[24];
//feetinches array16
FeetInches[] ftInch=new FeetInches[16];

//print out rectangle array
System.out.println(Arrays.toString(rectangles));
//print out circles array
System.out.println(Arrays.toString(circles));
//print out fractions array
System.out.println(Arrays.toString(fractions));
//print out feetInch array
System.out.println(Arrays.toString(ftInch));
  

}
}

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.