I have the following classes
SearchMain.java
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
*
* DO NOT CHANGE THIS CODE
*
*/
public class SearchMain {
public static void main(String[] args) {
ArraySearcher arraySearch = new
ArraySearchImp();
SecureRandom secureRandom = new
SecureRandom();
int[] sortedArray =
generateRandomSortedIntArray(10);
System.out.println(Arrays.toString(sortedArray));
Set<Integer> intSet = new
HashSet<Integer>();
for(int i = 0; i < 5; i++)
{
int
randomInt;
do {
randomInt =
sortedArray[secureRandom.nextInt(sortedArray.length-1)+1];
}
while(intSet.contains(randomInt));
intSet.add(randomInt);
testSearchMethods(arraySearch, sortedArray, randomInt);
}
testSearchMethods(arraySearch,
sortedArray, sortedArray[0]);
testSearchMethods(arraySearch,
sortedArray, sortedArray[sortedArray.length-1]);
testSearchMethods(arraySearch,
sortedArray, sortedArray[sortedArray.length-1] + 1);
testSearchMethods(arraySearch,
sortedArray, sortedArray[0] -1);
testSearchMethods(arraySearch,
sortedArray, -10);
}
private static void testSearchMethods(ArraySearcher
arraySearch, int[] sortedArray, int valueToSearchFor) {
int result =
Arrays.binarySearch(sortedArray, valueToSearchFor);
result = result < 0 ? -1 :
result;
System.out.println("\nValue to
search for: "+ valueToSearchFor + "; expected result: " +
result);
System.out.print("Linear
search...");
int linearSearchResult
=
arraySearch.linearSearch(sortedArray, valueToSearchFor);
if(result == linearSearchResult)
{
System.out.println("[OK]");
} else {
System.out.println("[ X ] - index returned: " +
linearSearchResult);
}
System.out.print("Binary
search...");
int binarySearchResult
=
arraySearch.binarySearch(sortedArray, valueToSearchFor);
if(result == binarySearchResult)
{
System.out.println("[OK]");
} else {
System.out.println("[ X ] - index returned: " +
binarySearchResult);
}
System.out.print("Binary
search...");
int binarySearchRecursiveResult
= arraySearch.binarySearchRecursive(sortedArray,
valueToSearchFor);
if(result ==
binarySearchRecursiveResult) {
System.out.println("[OK]");
} else {
System.out.println("[ X ] - index returned: " +
binarySearchRecursiveResult);
}
}
private static int[] generateRandomSortedIntArray(int
arraySize) {
SecureRandom secureRandom = new
SecureRandom();
int[] array = new
int[arraySize];
int previousInt = 0;
for(int i = 0; i < array.length;
i++) {
int randomInt =
secureRandom.nextInt(10) + 1;
array[i] =
previousInt + randomInt;
previousInt =
array[i];
}
return array;
}
}
----------------------------------------------
ArraySearcher.java
/**
*
* DO NOT CHANGE THIS CODE
*
*/
public interface ArraySearcher {
/**
*
* Also known as sequential search
*
* @param array
- an int[] array
* @param valueToSearchFor - the value to
search for
* @return index of <valueToSearchFor> if found
in <sortedArray>, -1 otherwise
*/
public int linearSearch(int[] array, int
valueToSearchFor);
/**
*
* The non-recursive version of binarySearch
*
* @param sortedArray -
the array must be sorted
* @param valueToSearchFor - the value to
search for
* @return index of <valueToSearchFor> if found
in <sortedArray>, -1 otherwise
*/
public int binarySearch(int[] sortedArray, int
valueToSearchFor);
/**
*
* The recursive version of binarySearch
*
* @param sortedArray -
the array must be sorted
* @param valueToSearchFor - the value to
search for
* @return index of <valueToSearchFor> if found
in <sortedArray>, -1 otherwise
*/
public int binarySearchRecursive(int[] sortedArray,
int valueToSearchFor);
}
---------------------------------------
I need to create
ArraySearchImp.java
to output the following
----------------------------------
------------- A sample run of SearchMain.java which shows correct implementation ---------
[4, 12, 20, 23, 31, 33, 43, 44, 45, 48]
Value to search for: 33; expected result: 5 Linear search.................. [OK]
Binary search.. [OK]
Binary search.. [OK]
Value to search for: 45; expected result: 8 Linear search.................. [OK]
Binary search.. [OK]
Binary search.. [OK]
Value to search for: 23; expected result: 3 Linear search.................. [OK]
Binary search.. [OK]
Binary search.. [OK]
Value to search for: 44; expected result: 7 Linear search.................. [OK]
Binary search.. [OK]
Binary search.. [OK]
Value to search for: 48; expected result: 9 Linear search.................. [OK]
Binary search.. [OK]
Binary search.. [OK]
Value to search for: 4; expected result: 0 Linear search...[OK]
Binary search...[OK] Binary search...[OK]
Value to search for: 48; expected result: 9 Linear search...[OK]
Binary search...[OK] Binary search...[OK]
Value to search for: 49; expected result: -1 Linear search...[OK]
Binary search...[OK] Binary search...[OK]
Value to search for: 3; expected result: -1 Linear search...[OK]
Binary search...[OK] Binary search...[OK]
Value to search for: -10; expected result: -1 Linear search...[OK]
Binary search...[OK] Binary search...[OK]
-----------------------------------------------------------------
Below is the
code in JAVA for the above question:
(ArraySearchImp.java)
//class ArraySearchImp that implements ArraySearcher.
public class ArraySearchImp implements ArraySearcher {
//method linear Search.
@Override
public int linearSearch(int[] array, int
valueToSearchFor) {
// TODO Auto-generated method
stub
//length of array.
int n = array.length;
//searching value using linear
search
for (int i = 0; i < n; i++)
{
//if value at i
in array is equal to valueToSearchFor
if (array[i] ==
valueToSearchFor)
//returning index if found.
return i;
}
//returning -1 if not found.
return -1;
}
//iterative binary search method.
@Override
public int binarySearch(int[] sortedArray, int
valueToSearchFor) {
// TODO Auto-generated method
stub
int left = 0, right =
sortedArray.length - 1;
//iterating till left is less than
or equal to right.
while (left <= right) {
//finding mid
index of sortedArray
int mid = (left
+ right) / 2;
//if element at
index mid is equal to valueToSearchFor
if
(sortedArray[mid] == valueToSearchFor)
return mid;
//if element at
index mid is less than valueToSearchFor
if
(sortedArray[mid] < valueToSearchFor)
left = mid + 1;
else
right = mid - 1;
}
//returning -1 if not found.
return -1;
}
@Override
public int binarySearchRecursive(int[] sortedArray,
int valueToSearchFor) {
// TODO Auto-generated method
stub
//finding index using helper method
of binarySearchRecursive.
int find =
binarySearchRecursiveHelper(sortedArray, valueToSearchFor, 0,
sortedArray.length - 1);
//returning index of the element
which is stored in variable find.
return find;
}
//helper function to find element using binary search
recursively.
public int binarySearchRecursiveHelper(int[]
sortedArray, int valueToSearchFor, int left, int right) {
if (right >= left) {
int mid = (left
+ right) / 2;
//if value at
index mid is equal to valueToSearchFor
if
(sortedArray[mid] == valueToSearchFor)
return mid;
//if value at
index mid is greater than valueToSearchFor
if
(sortedArray[mid] > valueToSearchFor)
return binarySearchRecursiveHelper(sortedArray,
valueToSearchFor, left, mid - 1);
//if value at
index mid is less than valueToSearchFor
if
(sortedArray[mid] < valueToSearchFor)
return binarySearchRecursiveHelper(sortedArray,
valueToSearchFor, mid + 1, right);
}
//returns -1 if not found.
return -1;
}
}
Refer to the
screenshot attached below to better understand the code and
indentation:
(ArraySearchImp.java)
Output:
If this answer
helps you then please upvote,
for further queries comment below.
Thank you.
I have the following classes SearchMain.java import java.security.SecureRandom; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** *...
Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform a linear search of the array. The method should receive the search key and starting index as arguments. If the search key is found, return its index in the array; otherwise, return –1. Each call to the recursive method should check one index in the array. Figure 19.2 import java.security.SecureRandom; import java.util.Arrays; import java.util.Scanner; public class LinearSearchTest{ public static int linearSearch(int data[], int searchKey)...
I am currently using eclipse to write in java.
A snapshot of the output would be greatly appreciated to verify
that the program is indeed working. Thanks in advance for both your
time and effort.
Here is the previous exercise code:
/////////////////////////////////////////////////////Main
/*******************************************
* Week 5 lab - exercise 1 and exercise 2: *
* ArrayList class with search algorithms *
********************************************/
import java.util.*;
/**
* Class to test sequential search, sorted search, and binary search
algorithms
* implemented in...
Need help with this Java project implementing an interpolation search, bolded the missing parts: /** A class that implements an interpolation search and a binary search of a sorted array of integers. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public class SearchComparer { private int[] a; private int interpolationSearchCounter; private int binarySearchCounter; private static final int CAPACITY = 50; public SearchComparer(int[] array, int n) {...
import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of this class in the * MasterMind program. * * @author TODO add your name here */ public class MasterMind { /** * Prompts the user for a value by displaying prompt. * Note: This method should not add a new line to the output of prompt. * * After prompting the user, the method will consume an entire * line of input while reading...
JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet { /** * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList * containing all the indexes of Strings in arr that match String s. For this method, two Strings, * p and q, match when p.equals(q) returns true or if both of the compared references are null. * * @param s The string...
I need help asking the user to half the value of the displayed random number as well as storing each number generated by the program into another array list and displayed after the game is over at the end java.util.*; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); ArrayList<Integer> data = new ArrayList<Integer>(); int count = 0; while (!choice.equals("No")) { int randomInt =...
23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a second oversize array with size2 elements, write a method that returns the first array with the elements of the second appended to the end. If the capacity of the oversize array is not large enough to append all of the elements, append as many as will fit. Hint: Do not construct a new array. Instead, modify the contents of the oversize array inside the...
please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1 colum like the one bellow, first box should have the class name, second box is for class attributes, and third box should have the class operations/methods and please put a (+) for public and a (-) for private example: class +-attributes +-Operations Also make a JAVADOCs, but not generated by a compiler, to explain what is going on in the code import java.util.Random; public...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...
How can I split this program into two classes? import java.io.*; public class Quiz { static LineNumberReader cin = new LineNumberReader(new InputStreamReader(System.in)); public static void main(String[] args) { int score = 0; final int NumberofQuestions = 5; int num; System.out.println("\nWelcome to CSC 111 Java Quiz\n"); String[][] QandA = { {"All information is stored in the computer using binary numbers: ","true"}, {"The word \"Public\" is a reserved word: ","false"}, {"Variable names may begin with a number: ","false"}, {"Will the...