Menu

[Solved] 2 Phonebookutils Task Public Final Class Phonebookutils Utility Class Means Intend Instant Q37283134

2. PhoneBookUtils Task:

public final class PhoneBookUtils : this is a utility class, whichmeans that we do not intend to instantiate it at all. Instead, wewill use the static helper methods it provides while writing otherclasses.

In our phone book application, the utility class implements thefollowing static methods:

  • public static <KeyType, ValueType> String mapToString(Map<KeyType, ValueType> map): a generic method which receives aMap and returns a string containing all of the the map’s “entries.”It should use the toString() method of each map entry, and place anewline after each entry so that there is one entry per line.
  • public static <T extends Comparable<T>> StringlistToSortedList(List <T> list) : a generic method whichreceives a List and returns a string containing the “entries” inthe list in sorted order, separated by newlines so thatthere is one entry per line. The toString() method of each entryshould be used to obtain a string representation. A side effect ofthe method is that the input list should become sorted. This methodmay make use of the static helper method Collections.sort.
  • public static <T extends Comparable<T>> voidinsertionSort(T [] arr, int i) : a generic method which receives anarray and sorts the array elements using the insertion sortalgorithm. To sort the array, the method should intially be calledwith an i parameter of 0 or 1.   This method must berecursive as follows: when it is called, it should beassumed that the first i elements of the array are already sorted,while the remaining elements may or may not be. The sorted portionof the array will always have at least one element, because any oneelement is always in sorted order in relation to itself. Therecursive step will always place one additional element into sortedorder by inserting it into the sorted section, in its proper sortedposition, thus increasing the size of the sorted section by oneelement. Then, it will make a recursive call to sort the remainingelements.     In otherwords, the recursivestep works by assuming that subset arr[0..i-1] of the array isalready sorted, and works on the arr[i..n] (whichis the unsortedsubset) by inserting first unsorted element (which is a[i]) intothe sorted region and incrementing the value of i for eachrecursive call.  The base case is reached once allelements have been placed into sorted order and there is nounsorted portion of the list remaining.
  • public static <T extends Comparable<T>> TbinarySearch(T arr[], int begin, int end, T key) : this genericmethod accepts an array and a key object returns the objectcontained in the array which matches the search key (usingcompareTo()) if found in the index range ≥ begin and < end, ornull if key is not found in that index range. You may assumethat the input array is already in sortedorder.  You may not call built-ins such asArrays.binarySearch() or Collections.binarySearch() as a part ofyour solution, and you must implement the binary search algorithmrecursively.

Expert Answer


Answer to 2. PhoneBookUtils Task: public final class PhoneBookUtils : this is a utility class, which means that we do not intend t… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *