Question: Previously, we have learned and used bubble sort, selection sort, iterative search and binary search to search and sort arrays of float. Now we will
Previously, we have learned and used bubble sort, selection sort, iterative search and binary search to search and sort arrays of float. Now we will use our knowledge of generics to reimplement those algorithms in a more generic way so that any number can be stored, sorted and searched. Review the slides for better understanding on how generics work in Java. Start by adding a member to our new GenericArrays class. This member will be an array of Object and will hold the values that we are searching and sorting. This is different from our previous labs where we had static method that we passed arrays into. create a constructor for this class. We will need to tell the constructor how much space to allocate for the array. Filling an array using setElement is possible but tedious. We will use another technique the copy constructor. A copy constructor is a constructor that takes existing data and uses it to fill the new object. In this case, we will be taking an array of the same type as the GenericArray and using it to populate the GenericArray. Next implement the accessor and mutator for a single element of the array. For the remainder of this lab, you may and should! make use of previously written code. You may need to modify it to make it work with generics. Implement sequential search on the array. You can assume that the array is sorted. Implement binarySearchRecursive. You can assume that the array is sorted. Implement bubbleSort and selectionSorts.
Helpful notes:
Object has a "compareTo" method that you can use for sorting and is equal"
You will need to use: @SuppressWarningsunchecked
public class GenericArray
public GenericArrayint arraySize
public GenericArray T existingArray
Gets the Nth element of the array.
@param n
the index of the item to return
@return the nth item in the array
public T getElementint n
Sets the Nth element of the array.
@param n
the index of the item to return
@param value
the value to set the array element to
public void setElementint n T value
Finds the location of the specified value the index at which the specified value is stored in the specified
array by examining each element until the value is found.
@param x
a value to search for
@return the location of the specified value the index at which the specified value is stored in the specified
array; @code if the specified value is not in the array
public int sequentialSearchT x
Finds the location of the specified value the index at which the specified value is stored in the specified
sorted array using the recursive binary search algorithm.
@param findMe
the value to search for
@return the location of the specified value the index at which the specified value is stored in the specified
array; @code if the specified value is not in the array
public int binarySearchRecursive T findMe
private int binarySearchRecursiveInternalint lower, int higher, T x
private boolean isGreaterT a T b
Sorts the array using the bubble sort algorithm
public void bubbleSort
Sorts the array using the selection sort algorithm
public void selectionSort
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
