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

For practice in writing and using methods that take an array parameter, copy Ini

ID: 3602005 • Letter: F

Question

For practice in writing and using methods that take an array parameter, copy Initializers.java to Initializers2.java and modify it as follows – first write two sets of new, overloaded display() and fill() methods ahead of main that take array parameters:

Define two overloaded versions of a static void display() method:

static void display(String header, int[] a) – if the header is not the empty String, print it followed by a space, using System.out.print(); in any case, print all the elements of array a, separated by single spaces, across one line, then print a blank line. Hint: copy the second version of the printing for loop from Initializers.

static void display(int[] a) – call display("", a); // no header

Define three overloaded versions of a static void fill() method:

static void fill(int[] a, int start, int diff) – fill up array a with values starting with start and adding diff for each element

static void fill(int[] a, int start)call fill(a, start, 2); // diff 2

static void fill(int[] a) – call fill(a, 2, 2); // start at 2, diff is 2

Now modify main (parts carried over from Initializers are in blue):

Declare and instantiate an int array named array1 having size 10 using the Java array initializer syntax with { } to fill array1 with the even integers 2 through 20, then use the first version of the display() method, defined above, to print out the values of array1, preceded by the String "array1:".

Now ask the user how long to make the array, and assign a new array of that length to reference variable array1, then fill it with even integers starting with 2; use the third version of the fill() method, defined above; finally, print array1 using display() as in #3 above. Using fill() and display() makes main much simpler.

And here is my code below: (I'm not sure if this is right..)

import java.util.Arrays;

public class Initializers

{

public static void main(String[] args)

{

int[] numbers = {12, 193, 56, 74, 9, 34, 165, 102, 154, 63, 19, 49};

System.out.print("UNSORTED Array is ");

for (int array : numbers)

{

System.out.print(array+" ");

}

Arrays.sort(numbers);

System.out.println(" SORTED Array is ");

for (int array : numbers)

{

System.out.print(array+" ");

}

}

}

Explanation / Answer

sample output :-

array1: 2 4 6 8 10 12 14 16 18 20
how long to make the array : 13
array1: 2 4 6 8 10 12 14 16 18 20 22 24 26