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

Write a method that takes an array (A) of integers number as parameter, splits A

ID: 3781719 • Letter: W

Question

Write a method that takes an array (A) of integers number as parameter, splits A into two arrays B and C as follow, then prints B and C. Example:

A 4 65 23 94 12 56 33 89 34 23

B 4 65 23 94 12

C 56 33 89 34 23

Test this method in the main by creating an array of size entered by the user (the size should be even number). The array A is filled randomly with numbers between 1 and 100. Display the original array as well as the resulting arrays. Hint: you can write and use the method void displayArray(int [] T). Sample Run1: Enter the size of the array: 7 7 is not an even number Enter the size of the array: 8 The first array: 4 65 23 94 12 56 33 89 34 23 The second array:4 65 23 94 12 The third array: 56 33 89 34 23

Explanation / Answer

Java Program:

package project;
import java.util.*;
public class test {
public static void main(String[] args) {
  
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of elements for an array");
int n=s.nextInt();
//checking whether number of elements are odd or even
if(n%2 != 0) {
System.out.println("Please enter an even number");
}
else {
   //creating an array with user defined value for number of elements in an array
int arr[]=new int[n];
Random rand = new Random();
//define the maximum limit for the random values
int max = 100;
// Generating random values
for(int i = 0; i < n; i++){//for reading array
arr[i]=rand.nextInt(max);
}
// Displaying randomly generated values
System.out.println("Original Array: ");
for(int i: arr){
System.out.print(i+" ");
}
System.out.println(" ");
// Splitting the array into 2 halves
int arr1[] = Arrays.copyOfRange(arr, 0, n/2);
int arr2[] = Arrays.copyOfRange(arr, (n/2), n);
// Displaying both parts
System.out.println("First half: " + Arrays.toString(arr1));
System.out.println("Second half: " + Arrays.toString(arr2));
}   
}
}

Output:

Enter the number of elements for an array
10
Original Array:
30 61 46 15 49 50 7 59 26 85
First half: [30, 61, 46, 15, 49]
Second half: [50, 7, 59, 26, 85]

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