Question: I need some guidance with this Java program. I cannot use any utilities and the headers listed have to be used. I have included the
I need some guidance with this Java program. I cannot use any utilities and the headers listed have to be used. I have included the requirement and then the code that I have so far. Any assistance as a guideline would be appreciated
public class StringArrayUtils { /** * Use the main method to test your other methods. Some sample arrays are given. The methods below * assume the array is full. */ public static void main(String[] args) { String[] a1 = {"aa", "bb", "cc"}; String[] a2 = {"aa", "dd", "bb", "ee", "cc"}; String[] a3 = {"cc", "bb", "aa"}; // add some test code here. printWords(a1); } /** * @param words - a full array of Strings to be printed displays the array to standard output * * This method is completed for you. Don't change it. */ public static void printWords(String[] words) { System.out.print("["); for (int i = 0; i < words.length - 1; i++) System.out.print(words[i] + ", "); System.out.println(words[words.length - 1] + "]"); } /** * @param words - a full array of Strings * @param key - a string search target * @return - the index of the string in the array or -1 if not found * * Hint - use the equals method of the String class to test String equality. E.g. * if(words[i].equals(key)){ ... } */ public static int indexOf(String[] words, String key) { return -1; } /** * @param words - a full array of Strings * @return - true if sorted in lexicographical order * * Hint - this is similar to the previous assignment */ public static boolean isSorted(String[] words) { return false; } /** * @param words - a full array to be sorted * * Hint - use the compareTo method of the String class to test order. * Use the insertion sort algorithm. You may not call any library sort function. */ public static void sortStandard(String[] words) { } /** * @param words - a full array to be sorted * * Hint - Sort the strings in increasing order of length. You can use any * sorting algorithm you like, but you must code it here. You may not call a library sort * function. Short strings should come first. */ public static void sortLength(String[] words) { } /** * @param words - a full array of Strings * @param key - a string search target * @return - the index of the string in the array or -1 if not found * * The method must ensure the list is sorted before searching. * Consider using your isSorted method to check. */ public static int binarySearch(String[] words, String key) { return -1; } /** * @param list1 - a full array of Strings * @param list2 - another full array of Strings * @return - true if both arrays contain the same words in the same orders. (i.e. copies of each other) * * Hint - use the equals method of the String class to test String equality. Be careful of * arrays of different lengths. */ public static boolean sameWords(String[] list1, String[] list2) { return false; } }
Here is the code I have so far:
public static void main(String[] args) { double[] a1 ={3.3, 2.2, 1.1 , 200, 33, 18, 97}; System.out.println("Min value at location: " + findMinIndex(a1)); } public static int findMinIndex(double [] nums){ int minLoc = 0; for(int i = 0; i< nums.length; i++){ if(nums[i] < nums[minLoc]) minLoc = i; } return minLoc; } public static int findIntWord(String[] words) { int minLoc = 0; for (int i = 0; i < words.length; i++) { // current word comes before the smallest word I've seen so far if (words[i].compareTo(words[minLoc]) < 0) minLoc = i; } return minLoc; } public static int findWord(String[] words, String target) { int loc = -1; for (int i = 0; i < words.length; i++) { if (words[i].equals(target)) loc = i; } return loc; } public static boolean isPal(String word){ for(int i =0, j=word.length()-1;i target) hi = mid - 1; else lo = mid + 1; }return -1; } public static void selectionSort(int[] list){ //cannot array dot sort. We are not sorting int we are sorting string for(int i =0; i < list.length - 1; i++){ int minIndex = i; //assume first element is smallest for(int j = i+1; j< list.length; j++){//change < to compare to if(list[j] < list[minIndex]) minIndex = j; } //swap items at i, minIndex int temp = list[i];// temp type is not an int its a string list[i] = list[minIndex]; list[minIndex] = temp; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
