Ask Question
4 July, 18:18

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 ().

+1
Answers (1)
  1. 4 July, 20:31
    0
    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
    sum = sum + intArray[i];

    }

    double ave = sum/intArray. length;

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

    for (int i = 0; i
    if (intArray[i]>ave) {

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

    }

    }

    }

    public static void main (String[] args) {

    readInttoArray ();

    printAboveAverage (readInttoArray ());

    }

    }

    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.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “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 ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers