Good morning, everyone! I am having a lot of trouble understanding this Java pro
ID: 3572639 • Letter: G
Question
Good morning, everyone! I am having a lot of trouble understanding this Java programming assignment and would greatly appreciate some help with it! The assignment is as follows:
Write a Java 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.
You have been supplied JUnit tests for the two methods, as well as the output for several example input sequences.
ALSO, the following format is provided below:
package edu.wit.cs.comp1000;
import java.util.Scanner;
//TODO: document this class
public class PA8a {
/**
* Reads all values from the provided scanner
* into the supplied array (up to its size)
* and returns the number of integers read
*
* @param input input source
* @param nums destination for read integers
* @return number of integers read
*/
public static int readIntoArray(Scanner input, int[] nums) {
return 0; // TODO: replace with your code
}
/**
* Prints to the screen the average of the supplied array,
* up to the supplied size, and all integers in the array,
* again up to the supplied size, that are larger than the average
*
* @param nums array of numbers
* @param size number of valid elements
*/
public static void printAboveAverage(int[] nums, int size) {
// TODO: write your code here
}
/**
* Program execution point:
* input a sequence of integers (up to 100),
* output average of integers and those over the average
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
// TODO: write your code here
}
}
Thank you so much in advance everyone! I highly appreciate it!
Explanation / Answer
package edu.wit.cs.comp1000;
import java.util.Scanner;
public class PA8a {
/**
* Reads all values from the provided scanner
* into the supplied array (up to its size)
* and returns the number of integers read
*
* @param input input source
* @param nums destination for read integers
* @return number of integers read
*/
public static int readIntoArray(Scanner input, int[] nums) {
System.out.println("Enter Integer Values upto 100 ");
int i=0;
while(input.hasNextInt())
{
nums[i++]=input.nextInt();
}
return i;
}
/**
* Prints to the screen the average of the supplied array,
* up to the supplied size, and all integers in the array,
* again up to the supplied size, that are larger than the average
*
* @param nums array of numbers
* @param size number of valid elements
*/
public static void printAboveAverage(int[] nums, int size) {
int sum=0,i=0;
float average=0;
for(i=0;i<size;i++)
System.out.print(" "+nums[i]);
System.out.println();
for(i=0;i<size;i++)
{
sum +=nums[i];
}
System.out.println("Sum Of The Array is:"+sum);
System.out.println();
average = sum /size;
System.out.println("Average Of The Array is:"+average);
for(i=0;i<size;i++)
{
if(nums[i]>average)
{
System.out.println("Number "+nums[i]+"is greater than average and it's index value is:"+ i);
}
}
}
/**
* Program execution point:
* input a sequence of integers (up to 100),
* output average of integers and those over the average
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int data [] = new int[100];
int len=PA8a.readIntoArray(scanner,data);
System.out.println("The Lenght Of Array is:"+len);
PA8a.printAboveAverage(data,len);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.