Name your Java project LoopControl and the Class name as LoopControl Firs line o
ID: 3824734 • Letter: N
Question
Name your Java project LoopControl and the Class name as LoopControl
Firs line of program // LoopControl by your name
First line of output to console System.out.println(“LoopControl by your name”);
Use <for > loop and <if..else> branch to do your program
1.Fill an Integer array with random numbers (0-10)
Use Array definition int[] numbers= new double[10]; //define an array <numbers>
use numbers[i]=Math.random()*10 +1; //to Fill array with random numbers
// NOTE: Math.random() is a double but <numbers> is an integer
Output to console all the elements of <numbers> array, one element per line
2,Sum the values of all the elements of the <numbers> array and output this value to the console System.out.println(“Sum= “+ sum);
3,Find the average (double variable) of the array elements in <numbers> and output to console to nearest hundredth.
4,Find the Maximum value of element in this array and output value to the console, nearest tenth
Use <for> loop and <if…else> for your processing. Remember about casting.
You may use more than one <for> loop (one to set array[i] values, one to find the Max and average values of <numbers[i]> , however it can be accomplished with one loop
Explanation / Answer
LoopControl.java
public class LoopControl {
public static void main(String[] args) {
// LoopControl by your name
int sum=0;
double average=0.0;
System.out.println("LoopControl by your name");
int[] numbers= new int[10];
//generating random numbers and populating those values into an array
for(int i=0;i<numbers.length;i++)
{
numbers[i]=(int)(Math.random()*10 +1);
}
//Displaying the elements in the array
System.out.println("Displaying the elements in the array :");
for(int i=0;i<numbers.length;i++)
{
System.out.println(numbers[i]);
sum+=numbers[i];
}
//Displaying the sum
System.out.println("Sum= "+ sum);
//calculating the average
average=((double)sum)/numbers.length;
//Displaying average
System.out.printf("Average = %.2f ",average);
//Calling Method to Find maximum element
int max=findMax(numbers);
System.out.println("The Maximum Element is :"+max);
}
//Method to Find maximum element
private static int findMax(int[] numbers) {
int max=numbers[0];
for(int i=0;i<numbers.length;i++)
{
if(max<numbers[i])
{
max=numbers[i];
}
}
return max;
}
}
__________________
Output:
LoopControl by your name
Displaying the elements in the array :
5
6
2
7
9
10
8
2
6
4
Sum= 59
Average = 5.90
The Maximum Element is :10
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.