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.
JAVA CODE:(Numbers.java)
// Your name here
// date of assignment
// class
import java.util.*;
public class Numbers
{
/** main method gets user input and
calls various methods
*/
public static void main(String
args[])
{
//scanner object
to get user input
Scanner ob=new
Scanner(System.in);
// variable to
store the user input
String
input="";
//list to store
the user input integers
ArrayList<Integer> list = new
ArrayList<Integer>();
//infinite
loop
while(true)
{
//user input
System.out.print("Enter a integer: ");
input=ob.next();
// checking for breaking the loop
if (input.equalsIgnoreCase("Q"))
break;
try
{
//converting string into int
and stored in the list
list.add(Integer.valueOf(input));
}
catch(NumberFormatException e)
{
//error message when non
integer is input from the user
System.out.println("\nError:
Please enter a valid integer\n");
}
}
//displaying the
list of integers
System.out.println("\nIntegers in the list: ");
for(Integer i:
list)
{
System.out.println(i);
}
//calling the
method to find the largest integer
max(list);
//calling the
method to find the minimum integer
min(list);
//calling the
method to find the average
average(list);
}
/** method to find the largest
number in the list
@param list a
list of integers
*/
static void
max(ArrayList<Integer> list)
{
//variable to
store the maximum integer
int
max=Integer.MIN_VALUE;
//iterating over
a list of integers
for(Integer i:
list)
{
//checking for maximum integer
if (i> max)
//storing the maximum integer
max=i;
}
//printing the
maximum integer
System.out.println("\nLargest integer in the list: "+max);
}
/** method to find the smallest
number in the list
@param list a
list of integers
*/
static void
min(ArrayList<Integer> list)
{
//variable to
store the minimum integer
int
min=Integer.MAX_VALUE;
//iterating over
a list of integers
for(Integer i:
list)
{
//checking for minimum integer
if (i< min)
//storing the minimum integer
min=i;
}
//printing the
maximum integer
System.out.println("\nSmallest integer in the list: "+min);
}
/** method to find the average of
integers in the list
@param list a
list of integers
*/
static void
average(ArrayList<Integer> list)
{
//variable to
store total and counter
int
total=0;
int
count=0;
//looping over a
list
for(Integer i:
list)
{
//calculating the total
total=total+i;
//incrementing the counter
count++;
}
//calculating
the average
double
average=(double)total/(double)count;
//printing the
average
System.out.println("\nAverage: "+String.format("%.2f",
average));
}
}
SCREENSHOT FOR OUTPUT: