In: Computer Science
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire list of entries on one line and then the minimum, maximum, and average. values on separate lines with nice readable labels. 5. Make sure your code runs without error and handles bad inputs and 0 and negative numbers are valid. 6. Followed instructions and include to get full credit file must include your name, date, class and Javadoc code comments for each and every method as well as logic comments.m
If you have any doubts, please give me comment...
import java.util.Scanner;
public class Numbers{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int numbers[] = new int[50];
System.out.print("Enter number(Q or q to exit): ");
String temp = scnr.next();
int n=0;
while(!temp.equalsIgnoreCase("Q")){
numbers[n] = Integer.parseInt(temp);
n++;
System.out.print("Enter number(Q or q to exit): ");
temp = scnr.next();
}
if(n==0)
System.out.println("No numbers are entered!");
else{
System.out.println("\nThe entered numbers are:");
for(int i=0; i<n; i++){
System.out.print(numbers[i]+" ");
}
System.out.println("\nMinimum: "+calcMinimum(numbers, n));
System.out.println("\nMaximum: "+calcMaximum(numbers, n));
System.out.println("\nAverage: "+calcAverage(numbers, n));
}
}
public static int calcMinimum(int nums[], int n){
int min = nums[0];
for(int i=1; i<n; i++){
if(min>nums[i])
min = nums[i];
}
return min;
}
public static int calcMaximum(int nums[], int n){
int max = nums[0];
for(int i=1; i<n; i++){
if(max<nums[i])
max = nums[i];
}
return max;
}
public static double calcAverage(int nums[], int n){
double avg = 0.0;
for(int i=0; i<n; i++){
avg += nums[i];
}
avg /= n;
return avg;
}
}