RunTime Class You will write the RunTime.java class which will implement the Run
ID: 3876026 • Letter: R
Question
RunTime Class You will write the RunTime.java class which will implement the Runtime Interface, The interface may be downloaded from Runtimelnterface.java LinearSearch Class You will write the LinearSearch.java class which will inherit from RunTime.java and implement the Search Interface using a linear search algorithm. The interface may be downloaded from Searchlnterface.java Please note that your search method must measure the run time and add it to the RunTime class by using the addRunTime() method BinarySearch Class You will Write the BinarySearch.java class which will inherit from RunTime.java and implement the Search Interface using a binary search algorithm. The interface may be downloaded from SearchInterface java Please note that your search method must measure the run time and add it to the RunTime class by using the addRunTime() method. Driver Class You will write the Driver.java class to test your implementations of: RunTime.java LinearSearch.java BinarySearch.java The Driver.java will implement the Driver Interface. The interface may be downloaded from Driverlnrerface.java Please note that, in addition to implementing the DriverInterface, you are also required to write your own public static main(String[] args) method in Driver.java that tests all of your classes and methods before submitting your assignment on mimir.Explanation / Answer
package com;
public class RunTime implements RunTimeInterface {
}
package com;
public interface RunTimeInterface {
}
package com;
public interface SearchInterface {
public int Search(int [] array,int KeyValue);
}
package com;
public class LinearSearch extends RunTime implements SearchInterface {
@Override
public int Search(int[] array, int KeyValue) {
int size = array.length;
for(int i=0;i<size;i++){
if(array[i] == KeyValue){
return i;
}
}
return -1;
}
}
package com;
public class BinarySearch extends RunTime implements SearchInterface {
@Override
public int Search(int [] array,int keyValue) {
int startIndex = 0;
int end = array.length - 1;
while (startIndex <= end) {
int mid = (startIndex + end) / 2;
if (keyValue == array[mid]) {
return mid;
}
if (keyValue < array[mid]) {
end = mid - 1;
} else {
startIndex = mid + 1;
}
}
return -1;
}
}
package com;
import java.util.Scanner;
public class Driver implements DriverInterface {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter number of elements");
int number=s.nextInt();
int array[]=new int[number];
System.out.println("enter Array elements");
for(int i=0;i<number;i++){
array[i]=s.nextInt();
}
Scanner s1=new Scanner(System.in);
System.out.println("enter Search Elemets");
int key=s1.nextInt();
LinearSearch linearSearch =new LinearSearch();
BinarySearch binarySearch=new BinarySearch();
int linearElements=linearSearch.Search(array, key);
int binarElements=binarySearch.Search(array, key);
System.out.println("Find by Linear Search :" +linearElements);
System.out.println("Find by Binary Search :" +binarElements);
}
}
package com;
public interface DriverInterface {
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.