Question

Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...

Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type.

Implement the following generic linear search method and write a client program to display results:

(Here is the header)

public static <E extends Comparable<E>> int search(E[] list, E key) {

            // add codes here

      }

Write an overloaded search method to process the collection type.

The following code is included for the problem:

//This program runs in finding the location (index) of a name and number.
//For this question, convert the arrays to an ArrayList and write another overloaded search method to do the same.


public class MainClient {
   public static void main(String[] args) {
       Integer[] list = {2, 13, 41, 17, -12, 43, -21, 99, 0};
       String[] names = {"Corey", "Zeal", "Hannah", "Ain", "Star", "Larry", "who"};
// use enhanced for to print the list and names
       for (int i =0; i < list.length; i++) {
           System.out.print(list[i] + ", ");
       }
      
       System.out.println();
       System.out.println("looking for 99 index = " + search(list, 99));
       System.out.println("looking for \"who \" index= " + search(names, "who"));
   }
  
   // use the method provided to create two ArrayList from the above array list and names
  
  
  

   public static <E extends Comparable<E>> int search(E[] list, E key) {
       for (int i = 0; i < list.length; i++)
           if (list[i].equals(key))
               return i;
       return -1;
   }
  
   // write a generic search method that takes the generic collection parameters instead of a generic array
   // inside this search method use an enhance for-loop to find the key


  
  
  
  
  
   public static <T> void fromArrayToCollection(T a[], Collection<T> c) {
       for (T x: a)
           c.add(x);
   }
}

The search(….) method returns the location (array index loc) for the <E> key when a match found,

and -1 if there is no match.

Result Sample: Search for 199, and “who” for the array search; -12 and “where” for the collection type search.

2 13 41 17 -12 43 -21 199 -12 0

Corey Zeal Hannah Ain Star Larry who where

----- using generic/array search -------------

looking for the index of 199 = 7

looking for the index of "who": 6

----- using generic/collection search -------------

[Corey, Zeal, Hannah, Ain, Star, Larry, who, where]

looking for the index of "who " index = 6

[2, 13, 41, 17, -12, 43, -21, 199, -12, 0]

looking for the index of -12 = 4

Homework Answers

Answer #1

PROGRAM :

package arraylist;

import java.util.*;
// Defines class A6Q5
public class A6Q5
{
// main method definition
public static void main(String []s)
{
// Creates an Integer type array
Integer [] list = {2, 13, 41, 17, -12, 43, -21, 99, 0};
// Creates an String type array
String [] names = {"Austin", "Max", "Spencer", "Mattew", "Blake",
"Dakota", "who", "where"};

// Creates an array list of type Integer
ArrayList<Integer> listInt = new ArrayList<Integer>();
// Creates an array list of type String
ArrayList<String> listNames = new ArrayList<String>();

// Displays integer array
System.out.print("\n Integer Array: ");
// Loops till end of the list
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + ", ");

// Displays integer array
System.out.print("\n String Array: ");
// Loops till end of the names
for(int i = 0; i < names.length; i++)
System.out.print(names[i] + ", ");

System.out.println("\n\n ------ Using generic / Array search ------");
// Calls the method to search 99 in list, and displays the returned result
System.out.println("Looking for 99 index = " + search(list, 99));
// Calls the method to search "who" in list, and displays the returned result
System.out.println("Looking for \"who\" index = " + search(names, "who"));

// Calls the method to convert integer array to array list of type integer
fromArrayToColection(list, listInt);
// Calls the method to convert string array to array list of type string
fromArrayToColection(names, listNames);

// Displays the integer type array list
System.out.println("\n Array list of Integer: " + listInt);
// Displays the string type array list
System.out.println("\n Array list of String: " + listNames);

System.out.println("\n ------ Using generic / Collection search ------");
// Calls the method to search -12 in array list of type integer
// and displays the returned result
System.out.println("Looking for -12 index = " + search(listInt, -12));
// Calls the method to search "where" in array list of type integer
// and displays the returned result
System.out.println("Looking for \"where\" index = " + search(listNames, "where"));
}// End of main method

// Generic method to search parameter key in the array
public static <E extends Comparable<E>> int search(E list[], E key)
{
// Loops till number of elements in the array
for(int i = 0; i < list.length; i++)
// Checks if current index position data is equals to parameter key
if (list[i].equals(key))
// Returns loop variable as found index
return i;
// Returns -1 for not found
return -1;
}// End of method

// Generic method to search parameter key in the array list
public static <M extends Comparable<M>> int search(ArrayList<M> list,
M key)
{
// Loops till number of elements in the array list
for(int i = 0; i < list.size(); i++)
// Checks if current index position data is equals to parameter key
if (list.get(i).equals(key))
// Returns loop variable as found index
return i;
// Returns -1 for not found
return -1;
}// End of method

// Generic method to convert first parameter array to second parameter array list
public static <T extends Comparable<T>> void fromArrayToColection(T a[], Collection<T> c)
{
// Enhanced for loop to extract each element from the array
for (T x : a)
// Adds the current element to array list
c.add(x);
}// End of method
}// End of class

Sample Output:


Integer Array: 2, 13, 41, 17, -12, 43, -21, 99, 0,
String Array: Austin, Max, Spencer, Mattew, Blake, Dakota, who, where,

------ Using generic / Array search ------
Looking for 99 index = 7
Looking for "who" index = 6

Array list of Integer: [2, 13, 41, 17, -12, 43, -21, 99, 0]

Array list of String: [Austin, Max, Spencer, Mattew, Blake, Dakota, who, where]

------ Using generic / Collection search ------
Looking for -12 index = 4
Looking for "where" index = 7

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Build two simple arrays (Integer and String) of 6 in length. Convert the two arrays into...
Build two simple arrays (Integer and String) of 6 in length. Convert the two arrays into a List type using an ArrayList type. A static method Array2Collection generic method is provided for that. From the List, display the contents of the two arrays in the forward or backward order: (without using an index for loop) //Code for converting Array to a Collection type static <T> void Array2Collection(T[] a, Collection<T> c) {     for (T x : a) {         c.add(x);...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999,...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime = System.currentTimeMillis(); long...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Do a trace on the binarySearch method below: variable key holds the value 17, and variable...
Do a trace on the binarySearch method below: variable key holds the value 17, and variable list is a reference to an array with these values {9, 20, 23, 28, 33, 38, 42, 48, 54, 61,73}. public static int binarySearch(int[] list, int key) {     int lowIndex = 0;     int highIndex = list.length - 1;     while (highIndex >= lowIndex) {       int midIndex = (lowIndex + highIndex) / 2;       if (key < list[midIndex]){            highIndex = midIndex...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...