In: Computer Science
JAVA CODE
// Write a method called example that will do several things.
// It has two integer array parameters of unknown varying lengths.
// It returns an integer array that contains each of the first array values
// divided by two and each of the second array values added to the number 100.
// It prints each value of the first array and then prints that value
// divided by two on a separate line. It then prints each value of the second
// array and that value added to 100 on a separate line. It then adds the
// values of the first array and prints the sum with an appropriate label and
// adds the values of the second array and prints the sum. It then returns the
// combined array.
import java.util.Arrays;
public class MyClass {
public static int[] example(int[] first, int[] second)
{
int[] result = new int[first.length+second.length];
int index = 0;
for(int x:first)
result[index++] = x/2;
for(int x:second)
result[index++] = x+100;
System.out.println("Values of the first array: ");
for(int x:first)
System.out.print(x+ " ");
System.out.println("\n\nValues of the first array/2: ");
for(int i=0; i<first.length; i++)
System.out.print(result[i]+ " ");
System.out.println("\n\nValues of the second array: ");
for(int x:first)
System.out.print(x+ " ");
System.out.println("\n\nValues of the second array+100: ");
for(int i=first.length; i<result.length; i++)
System.out.print(result[i]+ " ");
int sumFirst = 0;
for(int x:first)
sumFirst += x;
System.out.println("\n\nSum of the first array = " +
sumFirst);
int sumSecond = 0;
for(int x:second)
sumSecond += x;
System.out.println("Sum of the second array = " + sumSecond);
return result;
}
public static void main(String args[]) {
System.out.println("\nArray returned is" +
Arrays.toString(example(new int[]{10, 20, 30, 40}, new int[]{5, 6,
7, 8, 9})));
}
}
// Hit the thumbs up if you are fine with the answer. Happy Learning!