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

Write a java program: Create a method fillRandom() that accepts an array of int

ID: 3581296 • Letter: W

Question

Write a java program:

Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys)

Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10 columns line up - that is, specify a minimum field width of five.

In your main() method declare int arrays of size 1000, 100,000 and two arrays of 10,000,000. Call fillRandom() to populate the first three arrays. Populate the last array with ints from 50,000 to -49,999 in reverse order.

For each array:

call displayLastInts(), pause five seconds, call overloaded library method sort() on the array, call displayLastInts() on the sorted array.

Pause five seconds before proceeding to the next array.

Surround the calls to sort() with code that will record the system time before and after each sort.

Call library method binarySearch() three times on each sorted array. Search first for 0, then for 900, then for 2000.

Surround the calls to binarySearch() with code that will record the system time before and after each search.

Calculate the time elapsed for each operation and output a user friendly summary.

Explanation / Answer

import java.util.*;
public class RandomArrays{
    static void fillRandom(int[] arr){
        int size = arr.length;
        for (int i=0; i<size; i++){
            Random rand = new Random();
            int n = rand.nextInt(2000) - 999;
        }
        arr[0] = 0;
        arr[1] = 900;
    }

    static void displayLastInts(int[] arr){
        int size = arr.length;
        int n = 0;
        while(n<100){
            if (n%10==0 && n!=0){
                System.out.print(" ");
            }
            System.out.print(arr[size-100+n]+" ");
            n++;
        }
    }

    public static void main(String[] args) {
        int[] arr1 = new int[1000];
        int[] arr2 = new int[100000];
        int[] arr3 = new int[10000000];
        int[] arr4 = new int[100000];
        fillRandom(arr1);
        fillRandom(arr2);
        fillRandom(arr3);
        for (int i=0; i<arr4.length; i++){
            arr4[i] = 50000 - i;
        }
        try{
            displayLastInts(arr1);
            Thread.sleep(5000);
            Arrays.sort(arr1);
            displayLastInts(arr1);
            Thread.sleep(5000);
            displayLastInts(arr2);
            Thread.sleep(5000);
            Arrays.sort(arr2);
            displayLastInts(arr2);
            Thread.sleep(5000);
            displayLastInts(arr3);
            Thread.sleep(5000);
            Arrays.sort(arr3);
            displayLastInts(arr3);
            Thread.sleep(5000);
            displayLastInts(arr4);
            Thread.sleep(5000);
            Arrays.sort(arr4);
            displayLastInts(arr4);
            long startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr1, 0);
            long endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array1 searching for 0: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr1, 900);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array1 searching for 900: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr1, 2000);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array1 searching for 2000: "+ (endTime-startTime)/1000 + " seconds.");

            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr2, 0);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array2 searching for 0: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr2, 900);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array2 searching for 900: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr2, 2000);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array2 searching for 2000: "+ (endTime-startTime)/1000 + " seconds.");

            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr3, 0);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array3 searching for 0: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr3, 900);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array3 searching for 900: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr3, 2000);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array3 searching for 2000: "+ (endTime-startTime)/1000 + " seconds.");

            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr4, 0);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array4 searching for 0: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr4, 900);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array4 searching for 900: "+ (endTime-startTime)/1000 + " seconds.");
            startTime = System.currentTimeMillis();
            Arrays.binarySearch(arr4, 2000);
            endTime = System.currentTimeMillis();
            System.out.println("Binary Search for array4 searching for 2000: "+ (endTime-startTime)/1000 + " seconds.");
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote