Write a program that reads a stream of integers from the console and stores them
ID: 3761259 • Letter: W
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 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(). main() creates a scanner, as well as an array of 100 integers, and outputs a message to the screen asking for a sequence of numbers. readIntoArray() is then called to read values from the scanner and store them in the array. It must be passed two arguments: the scanner and the array. You should only store as many integers as the array can handle. Note, however, that there might be fewer than 100 values typed at the console – store whichever is fewer. This method must return how many integers, up to the length of the array, were read into the array. The hasNextInt() method of the scanner will be useful to determine if there are additional integers to read from the console. Additionally, when you are testing your code in Eclipse, and are done typing integers, press enter (i.e. to proceed to a new line) and then press CTRL+D to indicate to Eclipse that you are done typing (this is code for EOF, or end-of-file). Finally, printAboveAverage() should be called to read through the array, compute the average, and then print out all values in the array that are above the average. In particular, for each value above the average it should print the index in the array, as well as the value itself. printAboveAverage() should take two arguments: the array and the actual number of values in the array. Note that this second argument is not the total number of elements that the array can hold, but is instead the number of values that are valid (i.e. populated in the readIntoArray() method). For example, the array should be able to hold up to 100 values, but there might have only been 15 values typed at the console.
Explanation / Answer
/**
* The java program that prompts user to enter
* the integer values and press enter and then press
* Cntl+z to stop reading integer values.
* Then the program prints the numbers which are above
* average
* */
//ArrayProgram.java
import java.util.Scanner;
public class ArrayProgram
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//set size
final int size=100;
//an array of size 100
int[] elements=new int[size];
//calling readIntoArray(elements,scanner);
int count=readIntoArray(elements,scanner);
//call printAboveAverage method that prints the elements
//above average
printAboveAverage(elements,count);
}
/**The method printAboveAverage that takes an array and count
* of the values in the array that prints the integer values
* above average */
public static void printAboveAverage(int[] elements, int count)
{
//set sum to zero
double sum=0;
//add elements to sum
for (int i = 0; i < count; i++)
sum+=elements[i];
//calculate average value
double avg=sum/count;
//print above average numbers
System.out.println("Elements above average");
for (int i = 0; i < count; i++)
{
//check if value is above avg
if(elements[i]>avg)
System.out.println("element["+i+"]"+" = "+elements[i]);
}
}
/**The method readIntoArray takes array and scanner class object
* and read until user press enter and then cntl+z
* and returns the count of the elements*/
public static int readIntoArray(int[] elements, Scanner scanner)
{
int count=0;
String number="";
System.out.println("Enter values : ");
try
{
//read integer values from console
while(scanner.hasNextInt() || count<elements.length)
{
elements[count]=scanner.nextInt();
count++;
}
}
catch (Exception e) {
}
//return count vlaue
return count;
}
}
--------------------------------------------------------------------------------
Sample Output:
Enter values :
1 2 3 4 5
Elements above average
element[3] = 4
element[4] = 5