Question
You need to build a base class for all your searching algorithms. You should name this base class as MySearchAlg. Since we cannot implement the
You need to build a base class for all your searching algorithms. You should name this base class as MySearchAlg. Since we cannot implement the search method in base class, you should declare it as an abstract class.
---------------------------------------------------
public abstract class MySearchAlg
Methods
abstract int search(int[] array, int num);
---------------------------------------------------
1. Define aLinearSearchclass. This class should extend the MySearchAlg class. In this class, your search method should take a sequence of integer numbers and the number you want to find as input and return the index of that number. The index starts from 0, return -1 when it does not exist.
---------------------------------------------------
public class LinearSearch
Methods
public int search(int[] array, int num);
---------------------------------------------------
2. Define aBinarySearchclass.This class should extend the MySearchAlg class. In this class, your search method should take a sequence of integer numbers and the number you want to find as input and return the index of that number. The index starts from 0, return -1 when it does not exist.
---------------------------------------------------
public class BinarySearch
Methods
public int search(int[] array, int num);
---------------------------------------------------
In yourMainclass,build a test method to test your code.
The test method should take twointegers as input, the result is aninteger returned by one of yoursearching algorithms, and the ans is aninteger generated by yourself. Your test method should compare if these twointegers are the same, return true whenthey are the same and false when they not. You should generate 3 test cases and use these test cases to test allsearching algorithms above.
---------------------------------------------------
private static boolean test(int result, int ans);
---------------------------------------------------
Last, in your Main class, build a findMaxVal method.
This method takes an array of elements which is first increasing and then decreasing as input. And return the index value for the maximum number in this array. The same element will not appear in one array twice. For example,
---------------------------------------------------
private static int findMaxVal(int array[]);
Input: array = {1,5,8,12,9,7,-1} Output: 3 Input: array = {1,15,0} Output: 1
---------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Solution for the above question is MySeachAlgjava public abstract class MySearchAlg abstract int sea...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