Java Create a program called CalcWeightedAvgWithExceptions a

(Java) Create a program called CalcWeightedAvgWithExceptions, and use try-catch-finally blocks in your methods, as in the examples try-catch-finally examples from the class notes for this lesson, which are also shown in the file \'TryCatchFinally.txt\' available in this module.

The input file should be called \'data.txt\' and should be created according to the highlighted instructions below. The input file should contain (in order): the weight, the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values. See the example below. Also, your program should include methods for getting the input from the data.txt file, calculating the weighted average according to the data in the data.txt file, and printing the results to a user-designated output file.

Your main method should contain just three lines like these:

ArrayList<Double> inputValues = getData();

double weightedAvg = calcWeightedAvg(inputValues);

printResults(inputValues, weightedAvg);

The inputValues come from a single line in your data.txt file like the following:

0.5 3 10 70 90 80 20

The output file should contain something like the following:

“The weighted average of the numbers is 42.5, when using the data 10.0, 70.0, 90.0, 80.0, 20.0, where 0.5 is the weight used, and the average is computed after dropping the lowest 3 values.”

Creating the Input File

To create the input file, while in NetBeans with your project open, click to highlight the top-level folder of your project, which should be called CalcWeightedAvgWithExceptions. Then:

File-New File…

Keep the Project name at the top; keep Filter blank

Categories Other (at the bottom of the categories list)

   File Types Empty File (at the bottom of the files list)

Next>

FileName: data.txt

Folder: this should be blank; if it\'s not, delete whatever\'s there

Finish

In the empty file data.txt that you just created, add a single line of data like the example above, where the weight is a double (greater than 0.0 and less than or equal to 1.0) and the other numbers are the number, n, of lowest values to drop and then the numbers to be averaged after dropping the lowest n values. Also, instead of displaying the output to the console, let the user choose a file name (as in Horstmann\'s example), and write the output to a file with that user-supplied name (e.g., output.txt).

Solution

program

package com.gp.classes;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;

public class CalcWeightedAvgWithExceptions {

   /**
   * @param args
   *
   *
   *
   */
  
   public static List<Double> getData()
   {
       Scanner scanner=new Scanner(System.in);
       System.out.println(\"enter the file name with absolute path ex:D:\\\\information6.txt\");
       String filename=scanner.nextLine();
      
       List<Double> values=new ArrayList<Double>();
       List<String> value=new ArrayList<String>();
       File file=new File(filename);
       BufferedReader br = null;
      
      
       try {
           br=new BufferedReader(new FileReader(file));
           String line2;
       try {
               while((line2=br.readLine())!=null)
               {
                   value.add(line2);              
               }
              
               System.out.println(value);
           } catch (Exception e) {
               System.out.println(\"no data in the file\");
           }
      
       for(String line:value)
       {
           String[] nos=line.split(\"\\\\s+\");
          
           for(String no:nos)
           {
               values.add(Double.valueOf(no));
           }
                  
       }
          
          
       } catch (Exception e) {
           System.out.println(\"unable to perform to get the data try again\");
       }finally
       {
           if(br!=null){
               try {
                   br.close();
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   System.out.println(\"unable to close the buffer reader\");
               }
           }
       }
      
      
      
       return values;
      
   }
  
   private static double calcWeightedAvg(List<Double> inputValues) {
      
      
       int count=0;
       double sum=0;
       int count1=0;
      
       for(int i=0;i<inputValues.size();i++)
       {
           count++;
           if(count==3)
           {
               inputValues.remove(i);
           }
       }
      
      
       for(Double value:inputValues)
       {
           count1++;
       sum=sum+value;
       }
      
       double avg=sum/count1;
      
           return avg;
   }
   public static void printResults(double avg,List<Double> values)
   {
       System.out.println(\"weighted avg is\"+avg);
       System.out.println(\"the input values are\"+values);
   }

   public static void main(String[] args) {
       List<Double> inputValues=getData();
       double weightedAvg = calcWeightedAvg(inputValues);
       printResults(weightedAvg,inputValues);

   }
  
}

output

enter the file name with absolute path ex:D:\\information6.txt
D:\\\\information6.txt
[0.5 3 10 70 80 90 50]
weighted avg is48.916666666666664
the input values are[0.5, 3.0, 70.0, 80.0, 90.0, 50.0]

(Java) Create a program called CalcWeightedAvgWithExceptions, and use try-catch-finally blocks in your methods, as in the examples try-catch-finally examples fr
(Java) Create a program called CalcWeightedAvgWithExceptions, and use try-catch-finally blocks in your methods, as in the examples try-catch-finally examples fr
(Java) Create a program called CalcWeightedAvgWithExceptions, and use try-catch-finally blocks in your methods, as in the examples try-catch-finally examples fr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site