Write a JAVA Program that creates a onedimensional array and
Write a JAVA Program that creates a one-dimensional array and fills it with the following numbers in this order:
22, 40, 64, 92, 61, 42, 23, 98, 85, 13, 81, 32, 90, 54, 16
Pass the array to a method and find the following:
1. Sum of the numbers.
2. Average of the numbers to two decimal places.
3. Maximum value of the numbers.
4. Minimum value of the numbers.
5. Total Count of the numbers greater than or equal to 50.
You should first output the entire array and then output the answers to the above 5 questions. Make sure your output clearly identifies which answers are which.
Solution
Hi,
Please see below the class.Please comment for any queries/feedbacks.
thanks,
Anita
OneDimensionalArray.java
public class OneDimensionalArray {
public static void main(String [] args){
int [] intArr = {22, 40, 64, 92, 61, 42, 23, 98, 85, 13, 81, 32, 90, 54, 16};
//to output the entire array
printArray(intArr);
//calling method to calculate
processArray(intArr);
}
public static void printArray(int[] intArr){
System.out.println(\"Array is :\");
for(int i=0;i<intArr.length;i++){
System.out.print(\" \"+intArr[i]);
}
}
public static void processArray(int[] intArr){
int sum =0;
double average =0;
int maximumVal =0;
int minimumVal =intArr[0];
int totalCount =0 ;
for(int i=0;i<intArr.length;i++){
//to find sum
sum=sum+intArr[i];
//to find maximum value
if(maximumVal<intArr[i]){
maximumVal = intArr[i];
}
//to find minimum value
if(minimumVal>intArr[i]){
minimumVal = intArr[i];
}
//to find totalCount of the numbers greater than 50
if(intArr[i]>50){
totalCount = totalCount+1;
}
}
System.out.println(\"\ \ \");
//1.Sum of the numbers.
System.out.println(\"1. Sum of the numbers. : \"+sum);
//2. Average of the numbers to two decimal places.
average = sum/intArr.length;
average=Math.round(average*100.00)/100;
System.out.println(\"2. Average of the numbers : \"+average);
//3. Maximum value of the numbers.
System.out.println(\"3. Maximum value of the numbers : \"+maximumVal);
//4.Minimum value of the numbers.
System.out.println(\"4. Minimum value of the numbers : \"+minimumVal);
//5.Total Count of the numbers greater than or equal to 50
System.out.println(\"5.Total Count of the numbers greater than or equal to 50 : \"+totalCount );
}
}
Sample Output:
Array is :
22 40 64 92 61 42 23 98 85 13 81 32 90 54 16
1. Sum of the numbers. : 813
2. Average of the numbers : 54.0
3. Maximum value of the numbers : 98
4. Minimum value of the numbers : 13
5.Total Count of the numbers greater than or equal to 50 : 8

