Statistics are often calculated with varying amounts of data stored in a file. Write a program that takes any number of non-negative integers as input, and outputs the average, min and max. Prompt user for the input file name, open the file, read and process the numbers in the file.

Respuesta :

Following are the program explanation, program, and output to the given question:

Program Explanation:

  • Import package.
  • Defining a class Main.
  • Defining the main method.
  • Defining a string variable "str" and define a Scanner class object to input the"str" value.
  • After input, the string value of a Scanner class object is created that takes the "str"  value in the parameter.
  • In the next step, the "ArrayList" class object is declared that uses a while loop to add value to the list.
  • In the condition statement an if block is defined that check ArrayList value equal to 0, and print the message.
  • In the else block, a float variable "sum" and three integer variable "ctr, min, and max " is declared that check its value and prints its value with the message.

Program:

import java.io.*;//import package

import java.util.*;//import package    

public class Statistics //defining a class Statistics

{

  public static void main (String[] asx)//defining main method  

  {

       String str;//defining a string variable str

       Scanner sob= new Scanner(System.in);//defining Scanner class object to input value

       System.out.println("Enter value: ");//print message

       str= sob.nextLine();//input value

       Scanner s = new Scanner(str);//using Scanner class to take value str in parameter

       List<Integer> val = new ArrayList<Integer>();//defining an ArrayList class

       while(s.hasNextInt())//defining a while loop in add value

       {

           val.add(s.nextInt());//using add method to add value

       }

       if(val.size() == 0)//defining if block that check ArrayList value equal to 0

           System.out.println("The File is empty");//print message

       else//else block

       {

           float sum = 0;//defining float variable

           int ctr = 0, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;//defining integer variable that hold value

           for(Integer a:val)//defining a for loop that check ArrayList

           {

               ctr = ctr + 1;//adding value in ctr variable

               sum = sum + a;//adding value sum variable

           if(min > a)//use if to check min value greater than a

               min = a;//holding value in min

           if(max < a)//use if to check max value less than a

               max = a;//holding value in max

           }

               System.out.print("Average = ");//print message

               System.out.println(sum/ctr);//print average value

               System.out.print("Minimum value = ");//print message

               System.out.println(min);//print min value

               System.out.print("Maximum value = ");//print message

               System.out.println(max);//print max value

       }

  }

}

Output:

Please find the attached file.

Learn more:

brainly.com/question/15410214

Ver imagen codiepienagoya
ACCESS MORE
EDU ACCESS