105k views
4 votes
Write a program that reads a stream of integers from the console and stores them in an array. The array is then analyzed to compute the average of all the values in the array and finally all of the values that are above the average should be printed out to the screen. Specifically, you must write three methods: main(), readIntoArray(), and printAboveAverage().

User Minas
by
4.4k points

1 Answer

4 votes

Answer:

import java.util.Arrays;

import java.util.Scanner;

public class num4 {

static int[] readInttoArray() {

Scanner in = new Scanner(System.in);

int [] intArray = new int [10];

System.out.println("Enter Array values");

intArray[0]= in.nextInt();

for(int i = 1; i < intArray.length; i++){

System.out.println("enter the next value ");

intArray[i] = in.nextInt();

}

return intArray;

}

static void printAboveAverage(int [] intArray){

System.out.println("The array entered is:"+ Arrays.toString(intArray));

int sum =0;

for(int i =0; i<intArray.length; i++){

sum = sum +intArray[i];

}

double ave = sum/intArray.length;

System.out.println("Average "+ave);

for(int i = 0; i<intArray.length; i++){

if(intArray[i]>ave){

System.out.println(intArray[i]+" Is above average");

}

}

}

public static void main(String[] args) {

readInttoArray();

printAboveAverage(readInttoArray());

}

}

Step-by-step explanation:

  • Using Java Programming Language
  • The Three Methods are Created
  • readIntoArray() Uses Java's Scanner Class to prompt user to enter values to an array that is created with a for loop
  • The values entered are stored in the array array and returned whenever the method is called
  • printAboveAverage() Accepts an array as a parameter when called
  • Calculate the sum and average values in the array using a for loop
  • Uses an if statement to check for elements greater than the averaage and prints them out.
  • In the Main Method, Both methods readIntoArray() and printAboveAverage() are called.

User Patrick F
by
4.7k points