**Complete skeleton**
import java.util.ArrayList;
public class MyGenerics{
//Declarations
//****************************************************************************
//No-argument constructor:
//****************************************************************************
public MyGenerics (){
}//end of constructor
//****************************************************************************
//max: Receives a generic one-dimensional array and returns the maximum
// value in the array.
//****************************************************************************
public > E max(E[] list){
return null;
}//end of max
//****************************************************************************
//max: Receives a generic two-dimensional array and returns the maximum
// value in the array.
//****************************************************************************
public > E max(E[][] list) {
return null;
}
//****************************************************************************
//largest: Receives a generic arrayList and returns the maximum
// value in the array.
//****************************************************************************
public > E largest(ArrayList list) {
return null;
}
//****************************************************************************
//binarySearch: Receives a generic one-dimensional array and a generic key
// and returns the location of the key (positive) or
// a negative location if not found.
//****************************************************************************
public > int binarySearch(E[] list, E key) {
int low = 0;
int high = list.length - 1;
return binarySearch (list, key, low, high);
}
public > int binarySearch(E[] list, E key, int low, int high) {
return -99; // update
}
//****************************************************************************
//sort: Receives a generic arrayList and returns nothing.
//****************************************************************************
public > void sort(ArrayList list) {
}
//****************************************************************************
//sort: Receives a generic one-dimensional array and returns nothing.
//****************************************************************************
public > void sort(E[] list) {
}
//****************************************************************************
//displayOneDList: Receives a generic one-dimensional array and displays its contents
//****************************************************************************
public void displayOneDList(E[] list, String listName){
}
//****************************************************************************
//displayTwoDList: Receives a generic two-dimensional array & a string name
// and displays its contents
//****************************************************************************
public void displayTwoDList(E[][] list, String listName){
}
//****************************************************************************
//displayArray.
Complete skeletonimport java.util.ArrayList; public class My.pdf
1. **Complete skeleton**
import java.util.ArrayList;
public class MyGenerics{
//Declarations
//****************************************************************************
//No-argument constructor:
//****************************************************************************
public MyGenerics (){
}//end of constructor
//****************************************************************************
//max: Receives a generic one-dimensional array and returns the maximum
// value in the array.
//****************************************************************************
public > E max(E[] list){
return null;
}//end of max
//****************************************************************************
//max: Receives a generic two-dimensional array and returns the maximum
// value in the array.
//****************************************************************************
public > E max(E[][] list) {
return null;
}
//****************************************************************************
//largest: Receives a generic arrayList and returns the maximum
// value in the array.
//****************************************************************************
public > E largest(ArrayList list) {
return null;
2. }
//****************************************************************************
//binarySearch: Receives a generic one-dimensional array and a generic key
// and returns the location of the key (positive) or
// a negative location if not found.
//****************************************************************************
public > int binarySearch(E[] list, E key) {
int low = 0;
int high = list.length - 1;
return binarySearch (list, key, low, high);
}
public > int binarySearch(E[] list, E key, int low, int high) {
return -99; // update
}
//****************************************************************************
//sort: Receives a generic arrayList and returns nothing.
//****************************************************************************
public > void sort(ArrayList list) {
}
//****************************************************************************
//sort: Receives a generic one-dimensional array and returns nothing.
//****************************************************************************
public > void sort(E[] list) {
}
//****************************************************************************
//displayOneDList: Receives a generic one-dimensional array and displays its contents
//****************************************************************************
public void displayOneDList(E[] list, String listName){
}
3. //****************************************************************************
//displayTwoDList: Receives a generic two-dimensional array & a string name
// and displays its contents
//****************************************************************************
public void displayTwoDList(E[][] list, String listName){
}
//****************************************************************************
//displayArrayList: Receives a generic arraylist & a string name
// and displays its contents
//****************************************************************************
public void displayArrayList(ArrayList list, String listName){
}
}//end of class
-------------------------end of skeleton--------------
1. Use recursion in implementing the binarySearch method
2. Use Generics in implementing different methods
3. Use the attached driver. Do not modify the driver and do not submit the driver
4. Make sure that your program is well documented, readable, and the output is well labeled and
aligned
Sample output:
List Name: Integer One D Array
4 7 2 20 30 22
List Name: Double One D Array
4.0 7.5 2.3 20.7 30.1 22.8
List Name: String One D Array
Tony Paige Denzel Travis Austin Thomas Demetrius
List Name: Character One D Array
W D A C I F B
List Name: Integer Two D Array
1 1 60 5
2 20 40 5
4. 3 100 300 15 27
List Name: string Two D Array
1 Quitman Valdosta Atlanta Macon
2 Gainesville Tallahassee Jacksonville
List Name: A String arraylist
Tony Paige Denzel
Largest value is one-d integer list is: 30
Largest value is one-d double list is: 30.1
Largest value is one-d string list is: Travis
Largest value is one-d character list is: W
Largest value is two-d integer list is: 300
Largest value is two-d string list is: Valdosta
Largest value is an arrayList is: Tony
List Name: Integer One D Array
2 4 7 20 22 30
List Name: Double One D Array
2.3 4.0 7.5 20.7 22.8 30.1
List Name: String One D Array
Austin Demetrius Denzel Paige Travis Thomas Tony
List Name: Character One D Array
A B C D F I W
List Name: A String arraylist
Damieona Denzel Paige Tony
The location of value 20 in intList is: 3
The location of value 77 in intList is: -7
The location of value 'C' in charList is: 2
The location of value "Austin" in stringList is: 0
-------------------------------Tester Code--------------------------------------------
import java.util.*;
public class MyGenerics_Tester{
//Declrations
public static void main (String [] args){
//Declarations
Integer [] intList = {4, 7, 2, 20, 30, 22};
Double [] doubleList = {4.0, 7.5, 2.3, 20.7, 30.1, 22.8};
5. String [] stringList = {"Tony","Paige","Denzel","Travis","Austin","Thomas",
"Demetrius"};
Character[] charList = {'W','D','A','C','I','F','B'};
Integer [][] intTwoDList = {{1, 60, 5},
{20, 40, 5},
{100, 300, 15, 27}};
String [][] stringTwoDList = {{"Quitman", "Valdosta","Atlanta", "Macon"},
{"Gainesville","Tallahassee","Jacksonville"}};
ArrayList aList = new ArrayList<>();
aList.add("Tony");
aList.add("Paige");
aList.add("Denzel");
//Create an object
MyGenerics object = new MyGenerics();
//Display different lists
object.displayOneDList(intList,"Integer One D Array");
object.displayOneDList(doubleList,"Double One D Array");
object.displayOneDList(stringList,"String One D Array");
object.displayOneDList(charList,"Character One D Array");
object.displayTwoDList(intTwoDList,"Integer Two D Array");
object.displayTwoDList(stringTwoDList,"string Two D Array");
object.displayArrayList(aList,"A String arraylist");
//display largest in list
System.out.println ("tLargest value is one-d integer list is: t" + object.max(intList));
System.out.println ("tLargest value is one-d double list is: t" + object.max(doubleList));
System.out.println ("tLargest value is one-d string list is: t" + object.max(stringList));
System.out.println ("tLargest value is one-d character list is: t" + object.max(charList));
System.out.println ("tLargest value is two-d integer list is: t" + object.max(intTwoDList));
System.out.println ("tLargest value is two-d string list is: t" + object.max(stringTwoDList));
System.out.println ("tLargest value is an arrayList is: t" + object.largest(aList));
//Sorting
6. object.sort(intList);
object.sort(doubleList);
object.sort(stringList);
object.sort(charList);
object.sort(aList);
//Dispaly sorted lists
object.displayOneDList(intList,"Integer One D Array");
object.displayOneDList(doubleList,"Double One D Array");
object.displayOneDList(stringList,"String One D Array");
object.displayOneDList(charList,"Character One D Array");
object.displayArrayList(aList,"A String arraylist");
//BinarySearch
System.out.println ("tThe location of value 20 in intList is: t" +
object.binarySearch(intList,20));
System.out.println ("tThe location of value 77 in intList is: t" +
object.binarySearch(intList,77));
System.out.println ("tThe location of value 'C' in charList is: t" +
object.binarySearch(charList,'C'));
System.out.println ("tThe location of value "Austin" in stringList is: t" +
object.binarySearch(stringList,"Austin"));
}
}
Solution
#include
#include
#define size 10
int binsearch(int[], int, int, int);
int main() {
int num, i, key, position;
int low, high, list[size];
printf(" Enter the total number of elements");
scanf("%d", &num);
7. printf(" Enter the elements of list :");
for (i = 0; i < num; i++) {
scanf("%d", &list[i]);
}
low = 0;
high = num - 1;
printf(" Enter element to be searched : ");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1) {
printf(" Number present at %d", (position + 1));
} else
printf(" The number is not present in the list");
return (0);
}
// Binary Search function
int binsearch(int a[], int x, int low, int high) {
int mid;
if (low > high)
return -1;
mid = (low + high) / 2;
if (x == a[mid]) {
return (mid);
} else if (x < a[mid]) {
binsearch(a, x, low, mid - 1);
} else {
binsearch(a, x, mid + 1, high);
}
}