Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Written in java!! here\'s what i have and it is not working for me. please help!

ID: 3710023 • Letter: W

Question

Written in java!!

here's what i have and it is not working for me. please help!!

Make? ?a? ?method? ?to? ?print? ?a? ?String? ?array? ?that? ?accepts? ?the? ?array? ?as? ?a? ?parameter. 3. Create? ?a? ?method? ?which? ?accepts? ?a? ?String? ?array,? ?and? ?a? ?string? ?to? ?search? ?for? ?as? ?parameters, which? ?returns? ?the? ?index? ?of? ?the? ?first? ?occurrence? ?of? ?the? ?given? ?string? ?as? ?an? ?integer. 4. Create? ?a? ?boolean? ?method? ?which? ?accepts? ?a? ?sorted? ?string? ?array? ?in? ?alphabetical? ?order.? ?The method? ?either? ?confirms? ?that? ?the? ?array? ?is? ?sorted? ?and? ?returns? ?true,? ?or? ?returns? ?false? ?if? ?the array? ?wasn’t? ?sorted. 5. Create? ?a? ?binarySearch? ?method? ?which? ?accepts? ?a? ?sorted? ?string? ?array? ?and? ?a? ?string? ?to? ?search for? ?as? ?parameters,? ?confirms? ?that? ?the? ?array? ?is? ?sorted,? ?and? ?returns? ?the? ?index? ?of? ?ANY occurrence? ?of? ?the? ?given? ?element? ?as? ?an? ?integer.? ?A? ?binarySearch? ?algorithm? ?splits? ?the? ?search area? ?of? ?the? ?array? ?depending? ?on? ?how? ?the? ?search? ?string? ?compares? ?to? ?the? ?element? ?in? ?the middle? ?(array[length/2].)? ?You? ?only? ?need? ?to? ?search? ?one? ?half? ?of? ?the? ?split? ?array,? ?the? ?left? ?half if? ?the? ?search? ?string? ?comes? ?alphabetically? ?before? ?the? ?middle? ?element? ?and? ?vice? ?versa? ?if? ?it’s alphabetically? ?after.? ?If? ?the? ?array? ?wasn’t? ?sorted,? ?have? ?it? ?return? ?-1. 6. Please? ?copy? ?and? ?paste? ?your? ?Lab11.java? ?file? ?to? ?the? ?Lab? ?11? ?assignment? ?submission? ?link under? ?the? ?Labs? ?tab? ?on? ?Blackboard.? ?Make? ?sure? ?you? ?talk? ?to? ?your? ?lab? ?TA? ?before? ?you? ?leave? ?to ensure? ?they? ?marked? ?you? ?as? ?present.

package Lab12; import java.util.Arrays; public class Lab12 { static void printArray(String[] array) { for(int i = 0; i < array.length; ++i) { System.out.print(array[i] + " "); System.out.println(); } } int i = 0; static int findOccurence(String[] array, String key) { for(int i = 0; i < array.length; ++i) { if(array[i].equals(key)) { return i; } return -1; } } static boolean isSorted(String[] array) { for(int i = 0; i < array.length-1; ++i) { if(array[i].compareTo(array[i+1]) > 0) { System.out.println("Array is not sorted" ); } return false; } return true; } public static int binarySearch(String[] array, String key) { int l = 0; int h = array.length - 1; if(!isSorted(array)) return -1; while (l <= h) { int mid = (l + h) / 2; if(array[mid].equals(key)) return mid; if (key.compareTo(array[mid]) > 0 ) { l = mid + 1; } else { h = mid - 1; } } return -1; } public static void main(String[] args) { String[] array = {"john", "ahmed", "ghaith", "tyler", "elizabeth",}; System.out.println("Original Array print: "); printArray(array); System.out.println("Check sorted" + isSorted(array) ); System.out.println("Find occurence" + findOccurence(array, "tyler")); Arrays.sort(array); System.out.println("Sorted array print: "); printArray(array); System.out.println("Binary Search: " + binarySearch(array, "tyler")); } }

Explanation / Answer

import java.util.Arrays;

class Lab12
{
static void printArray(String[] array)
{
for(int i = 0; i < array.length; ++i)
{
System.out.print(array[i] + " ");
System.out.println();

}

}

static int findOccurence(String[] array, String key)
{
int occur = -1;
for(int i = 0; i < array.length; ++i)
{
  if(array[i].equals(key))
   occur = i;

  
}
return occur;
}
static boolean isSorted(String[] array)
{
boolean sort = true;
for(int i = 0; i < array.length-1; ++i)
{
if(array[i].compareTo(array[i+1]) > 0)
sort = false;

}
return sort;
}
public static int binarySearch(String[] array, String key)
{ int l = 0;
int h = array.length - 1;
if(!isSorted(array))
return -1;

while (l <= h)
{ int mid = (l + h) / 2;
if(array[mid].equals(key))
return mid;
if (key.compareTo(array[mid]) > 0 )
{ l = mid + 1; }
else { h = mid - 1; } }
return -1;
}

public static void main(String[] args)
{
String[] array = {"john", "ahmed", "ghaith", "tyler", "elizabeth",};
System.out.println("Original Array print: ");
printArray(array);
System.out.println("Check sorted " + isSorted(array) );
System.out.println("Find occurence " + findOccurence(array, "tyler"));
Arrays.sort(array); System.out.println("Sorted array print: ");
printArray(array);
System.out.println("Binary Search: " + binarySearch(array, "tyler"));

}

}

Output:

Original Array print:
john
ahmed
ghaith
tyler
elizabeth
Check sorted false
Find occurence3
Sorted array print:
ahmed
elizabeth
ghaith
john
tyler
Binary Search: 4

Do ask if any doubt. Please upvote.