Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete Java program called CalcWeightedAvgDropLowest according to the

ID: 3822436 • Letter: W

Question

Write a complete Java program called CalcWeightedAvgDropLowest according to the following guidelines.

For this program, instead of getting the data from the user via System.in, get only input and output file names from the user. See Horstmann's program Total.java in Section 7.1, pp. 319, 320, on which this assignment is based.

Also note that the methods for getting input and writing output will need to handle exceptions, whereas in Horstmann's Total.java example, only the main needs to handle exceptions. The simplest (and recommended) way to handle exceptions in this program is to have the two methods that read and write data each throw a FileNotFoundException. We'll see in a later program how to handle exceptions differently.

Input File

The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions below.

The input file should contain (in order): the weight (greater than 0 and less than or equal to 1), the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values.

For example, you might have these values in your input file (data.txt):

0.5 3 10 70 90 80 20

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 in the projects tab at the upper left. Then starting with the NetBeans File menu, do

In the empty file data.txt that you just created, add a single line of data like the one in 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. Then save this data.txt input file.

It's important that your input file is where NetBeans will look to find it. The above instructions should make sure that that happens.

Main method

Your main method should contain just three lines like these:

ArrayList inputValues = getData();

double weightedAvg = calcWeightedAvg(inputValues);

printResults(inputValues, weightedAvg);

where the printResults method should prompt the user for the name of the output file and print to that output file.

Output

Given the sample values above in the data.txt input file, the output file should contain something very much 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.

Prompting for names of input file and output file

Make sure you prompt the user for the name of the input file (even though we know what it is). You can use a prompt like: "Enter "data.txt" (no quotes) for the name of the input file: "

Also prompt the user for the name of the output file. If you use code like that in Horstmann's Total.java program to create the output file, the output file should be located at the same place as the input file. Check the NetBeans Files tab (next to the Project tab in the upper left) to see both the input and output files.

Explanation / Answer

CalcWeightedAvgDropLowest.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class CalcWeightedAvgDropLowest {
   public static void main(String[] args) throws FileNotFoundException {
       ArrayList input = getData();
       double weightedAverage = calcWeightedAvg(input);
       printResults(input, weightedAverage);
   }

   public static ArrayList getData() throws FileNotFoundException {
       ArrayList inputLine = new ArrayList();
       //This is where I should create an array list of doubles.
       Scanner console = new Scanner(System.in);
       System.out.println("Enter 'data.txt' (without the quotes) as the name of the input file: ");
       String inputFileName = console.next();
       File inputFile = new File(inputFileName);
       Scanner in = new Scanner(inputFile);
       while(in.hasNextDouble()) {
           inputLine.add(in.nextDouble());
           }
       in.close();
       return inputLine;
       }

   public static double calcWeightedAvg(ArrayList data) {
       double enteredWeight = Double.parseDouble(data.get(0).toString());
       int numberofLowetValuesDropped =(int) Double.parseDouble(data.get(1).toString());
       data.remove(1);
       data.remove(0);
       Collections.sort(data);
       double total = 0;
       for(int i=numberofLowetValuesDropped; i<data.size(); i++){
           total = total + Double.parseDouble(data.get(i).toString());
       }
       data.add(0, enteredWeight);
       data.add(1, numberofLowetValuesDropped);
       double avg = total/(data.size() - numberofLowetValuesDropped);
       return avg;
   }

   public static void printResults(ArrayList inputList, double weightedAvg) throws FileNotFoundException {
       Scanner console = new Scanner(System.in);
       System.out.println("Enter the name of the output file: ");
       String outputFileName = console.next();
       PrintWriter out = new PrintWriter(outputFileName);
       String s = "The weighted average of the numbers is "+weightedAvg+", " +
       "when using the data "+inputList+", where "+inputList.get(0)+" is the weight used, " +
       "and the average is computed after dropping the lowest "+inputList.get(1)+" values.";
       System.out.println(s);
       out.write(s);
       out.flush();
       out.close();
       System.out.println("Your output is in the file " + outputFileName + ".");
          
   }
}

Output:

Enter 'data.txt' (without the quotes) as the name of the input file:
D:\data.txt
Enter the name of the output file:
D:\output.txt
The weighted average of the numbers is 42.5, when using the data [0.5, 3, 10.0, 20.0, 70.0, 80.0, 90.0], where 0.5 is the weight used, and the average is computed after dropping the lowest 3 values.
Your output is in the file D:\output.txt.

output.txt

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote