Write a program that:

a. reads from the terminal a sequence of numbers (integers)
b. saves them to a file with the name given from the command line
c. calculates,then displays on the terminal, and also saves to that file
the maximum, minimum, and average.

Additional requirements:
Store the numbers in a LinkedList.
Define a class DataAnalyzer that

* has a constructor that stores the list of numbers:
public DataAnalyzer(LinkedList numList) {...}
* has a method each for computing min(), max() and average():
public int min() {...}, etc.

Define a class DataAnalyzerTester that reads the numbers from System.in, builds the number list,
creates the DataAnalyzer object, and displays the min, max, and average using the DataAnalyzer instance.
The DataAnalyzerTester class implements the main() method.

Your code needs to handle invalid input and I/O exceptions.
Write javadoc comments.
Include both java files in your solution document.

Respuesta :

Answer:

The Java code is given below with appropriate comments for better understanding

Explanation:

//DataAnalyzer.java

import java.util.*;

class DataAnalyzer

{

  private LinkedList<Integer> numbers;

  //Parameterized Constructor that stores the list of numbers

  public DataAnalyzer(LinkedList<Integer> numList)

  {

      //Storing numList to numbers

      numbers = numList;

  }

  //Implementation of min() method that returns minimum number

  public int min()

  {

      //consider starting number is the minimum number

      int minNum = numbers.get(0);

      //Iterate the numbers in the list

      for(int j=0; j < numbers.size(); j++)

      {

          //Comparing each number with minNum

          if( numbers.get(j) < minNum )

              //Update minimum number

              minNum = numbers.get(j);

      }

      //Return minimum number of the list

      return minNum;

  }

  //Implementation of max() method which returns maximum number

  public int max()

  {

      //consider starting number is the max number

      int maxNum = numbers.get(0);

      //Iterate the numbers in the list

      for(int j=0; j < numbers.size(); j++)

      {

          //Comparing each number with maxNum

          if( numbers.get(j) > maxNum )

              //Update maximum number

              maxNum = numbers.get(j);

      }

     

      //Return maximum number of the list

      return maxNum;

  }

  //Implementation of average() method returns average of all numbers

  public double average()

  {

       //Declare sum as type of integer and assign value as zero

      int sum = 0;

      //Declare avg as type of double type

      double avg;

     

      //Iterate the numbers in the list

      for(int j=0; j < numbers.size(); j++)

      {

          //add sum of numbers

          sum = sum + numbers.get(j);

      }

     

      avg = sum / (double)(numbers.size());

      //Return average value of the list

      return avg;

  }

}

DataAnalyzerTester.java

//DataAnalyzerTester.java

import java.util.*;

import java.io.*;

//Implementation of DataAnalyzetTester class

class DataAnalyzerTester

{

  //Main method

  public static void main(String args[])

  {

      try

      {

          //Declare numList as variable of type

      //Linked list that store user input

          LinkedList<Integer> numList = new LinkedList<Integer>();

          int num;

     

          //Declare Scanner class object

          Scanner input = new Scanner(System.in);

          String fileName;

          //Display statement

          System.out.print("\n Enter file name for storing numbers: ");

          fileName = input.nextLine();

         

          //Create a variable as file type

          File createfile = new File(fileName);

          //Creating file

          createfile.createNewFile();

          //Creating a FileWriter Object

          FileWriter writer = new FileWriter(createfile);

         

          System.out.println("\n Start entering numbers (0 to stop): \n\n");

     

          //Reading number

          num = input.nextInt();

     

          //Iterate the loop

          //until user enters 0

          while(num != 0)

          {

              //Writing numbers to the file

              writer.write(num + " \n ");

             

              //Add those numbers to list

              numList.add(num);

              //Get another number from user

              num = input.nextInt();

          }

          //Creating DataAnalyzer object

          DataAnalyzer analyzerObject = new DataAnalyzer(numList);

          //Display statement for min, max and average values

          System.out.println("\n\n Minimum Number: " + analyzerObject.min());

          writer.write("\n\n Minimum Number: " + analyzerObject.min());

          System.out.println("\n\n Maximum Number: " + analyzerObject.max());

          writer.write("\n\n Maximum Number: " + analyzerObject.max());

          System.out.println("\n\n Average: " + analyzerObject.average() + " \n\n");

          writer.write("\n\n Average: " + analyzerObject.average() + " \n\n");

         

          writer.flush();

         

          //Close the file object

          writer.close();

      }

     

      catch(Exception ex)

      {

      //display statement

          System.out.println(ex);

      }

  }

}