1 Use a for loop to generate an array of ten random integers

1. 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. Display the sum with a \"thousands\" comma as in the sample output.
SAMPLE OUTPUT

[106, 114, 118, 126, 130, 132, 169, 183, 192, 195]
212 228 236 252 260 264 338 366 384 390
The total is 2,930

2. Write a method named sumInts that can take a variable number of int arguments and return the sum of these arguments. The ints to be summed up must be entered as command line arguments. Command line arguments can be simulated in Eclipse. Watch the video. In the main method, display the ints that were entered on the command line. Then execute sumInts and display the sum it returns.
TWO SAMPLE OUTPUTS

Passing [1, 2, 3, 4, 5]
Sum is 15

Passing [10, 20, 30]
Sum is 60

Here is the array 12.50 3.50 4.20 8.20 3.40 1.80 6.80 4.90 2.10

Solution

2)

public class Commandlinesum

{

public static void main(String[] args) {

System.out.println(\"# of command line args: \" + args.length);

System.out.print(\"Command line args in order: \");

for (int i = 0; i < args.length; i++) {
System.out.print(args[i] + \" \");

}

System.out.println(\"\ The sum is: \" + sumInts(args));

}

public static int sumInts(String[] args) {
int sum = 0;

for (int i = 0; i < args.length; i++) {
sum += Integer.parseInt(args[i]);
}
return sum;

}
}

3)

}

}

Thank You :)

1. 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 en
1. 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 en

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site