60 361
Assignments Done
98%
Successfully Done
In April 2018

Answer to Question #62004 in Java | JSP | JSF for Bill DeShields

Question #62004
Use a for loop to generate an array of ten random integers, all in the range from 100 to 200, inclusive. Use the Arrays class to both sort and display the entire array. Next, pass the array as the sole argument to a method that doubles each element of the array and then returns the array. Use a foreach loop to show all elements in the returned array on one line separated by a single space. This latter loop should also determine the sum of all elements in the returned array.
Expert's answer
import java.util.Arrays;
import java.util.Random;

public class Main {
public static void main(String[] args) {
int[] arr = new int[10];
Random r = new Random();

for (int i = 0; i < 10; i++) {
arr[i] = r.nextInt((200 - 100) + 1) + 100;
}

Arrays.sort(arr);
System.out.println(Arrays.toString(arr));

arr = doubleElements(arr);

int sum = 0;
for (int a : arr) {
System.out.print(a + " ");
sum += a;
}

System.out.println("\nSum is " + sum);
}

static int[] doubleElements(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] *= 2;
}

return arr;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions