First, launch NetBeans and close any previous projects that may be open (at the
ID: 3715246 • Letter: F
Question
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below.
The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file.
The input file should contain (in order): the weight (a number greater than zero 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.
You should also prompt the user for the name of the output file, and then print your results to an output file with the name that the user specified.
Your program should allow the user to re-enter the input file name if one or more of the exceptions in the catch clauses are caught.
Your methods for getting data and printing results should each throw a FileNotFoundException which should be caught in the main method.
Use try-with-resources statements (?? ???? ?????.)?? ???? ?????. in your methods for getting and printing the data, and so avoid the need to explicitly close certain resources.
In your readData method, use hasNextDouble to check ahead of time whether there's a double in the data. That way when you try to get the nextDouble, your code won't throw a NoSuchElementException.
You can use a writeFile method that does all the work (i.e., does not call a writeData method the way that Horstmann’s readFile method calls a readData method). Use a try-with-resources statement in your writeFile method when creating a new PrintWriter.
The inputValues come from a single line in a text file (data.txt) such as the following:
0.5 3 10 70 90 80 20
The output in the output file must give the weighted average, the data and weight that were used to calculate the weighted average, and the number of values dropped before the weighted average was calculated.
Your output should look 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."
Write the output to a file with the filename that the user chose to name the output file (e.g., output.txt). Don't hard-code the output file name in your program.
Creating the Input File
To create the input file, while in NetBeans with your project open, first click to highlight the top-level folder of your project, which should be called WeightedAvgDataAnalyzer.
Then from the File menu do this:
In the empty file data.txt that you just created, add a single line of data like that shown 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.
Explanation / Answer
WeightedAvgDataAnalyzer.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class WeightedAvgDataAnalyzer {
public static void main(String[] args) {
ArrayList<Double> input = getData();
System.out.println(input);
double weightedAverage = calcWeightedAvg(input);
printResults(input, weightedAverage);
}
public static ArrayList<Double> getData() {
ArrayList<Double> inputLine = new ArrayList<Double>();
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 = null;
try {
in = new Scanner(inputFile);
while (in.hasNextDouble()) {
inputLine.add(in.nextDouble());
}
} catch (FileNotFoundException fne) {
String errorString = fne.getMessage();
System.out.println("There was an error when trying to read from file "
+ inputFileName + ": " + errorString);
} finally {
if (in != null) {
in.close();
}
}
return inputLine;
}
public static double calcWeightedAvg(ArrayList<Double> 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, Double.valueOf(numberofLowetValuesDropped));
double avg = total/(data.size() - numberofLowetValuesDropped);
return avg;
}
public static void printResults(ArrayList<Double> inputList, double weightedAvg) {
Scanner console = new Scanner(System.in);
System.out.println("Enter the name of the output file: ");
String outputFileName = console.next();
PrintWriter out = null;
if (inputList.size() > 0) { // we have a non-empty inputList
try {
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.";
out.print(s);
System.out.println("Your output is in the file " + outputFileName + ".");
} catch (FileNotFoundException fne) {
String errorString = fne.getMessage();
System.out.println("There was an error when trying to write to the output file "
+ errorString);
} finally {
if (out != null) {
out.close();
}
}
} else {
System.out.println("Problems reading data from input file; no output written to " + outputFileName);
}
}
}
Output:
Enter 'data.txt' (without the quotes) as the name of the input file:
D:\data.txt
[0.5, 3.0, 10.0, 70.0, 90.0, 80.0, 20.0]
Enter the name of the output file:
D:\output.txt
Your output is in the file D:\output.txt.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.