Question
Create a new Java application called WeightedAvgDataAnalyzer (without the quotation marks), that modifies the DataAnalyzer.java code below according to the specifications at the end of
Create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java code below according to the specifications at the end of the code.
1 import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.io.IOException;
4 import java.util.Scanner;
5 import java.util.NoSuchElementException;
6
7 /**
8 This program processes a file containing a count followed by data values.
9 If the file doesnt exist or the format is incorrect, you can specify another file.
10 */
11 public class DataAnalyzer
12 {
13 public static void main(String[] args)
14 {
15 Scanner in = new Scanner(System.in);
16
17 // Keep trying until there are no more exceptions
18
19 boolean done = false;
20 while (!done)
21 {
22 try
23 {
24 System.out.print("Please enter the file name: ");
25 String filename = in.next();
26
27 double[] data = readFile(filename);
28
29 // As an example for processing the data, we compute the sum
30
31 double sum = 0;
32 for (double d : data) { sum = sum + d; }
33 System.out.println("The sum is " + sum);
34
35 done = true;
36 }
37 catch (FileNotFoundException exception)
38 {
39 System.out.println("File not found.");
}
41 catch (NoSuchElementException exception)
42 {
43 System.out.println("File contents invalid.");
44 }
45 catch (IOException exception)
46 {
47 exception.printStackTrace();
48 }
49 }
50 }
51
52 /**
53 Opens a file and reads a data set.
54 @param filename the name of the file holding the data
55 @return the data in the file
56 */
57 public static double[] readFile(String filename) throws IOException
58 {
59 File inFile = new File(filename);
60 Scanner in = new Scanner(inFile);
61 try
62 {
63 return readData(in);
64 }
65 finally
66 {
67 in.close();
68 }
69 }
70
71 /**
72 Reads a data set.
73 @param in the scanner that scans the data
74 @return the data set
75 */
76 public static double[] readData(Scanner in) throws IOException
77 {
78 int numberOfValues = in.nextInt(); // May throw NoSuchElementException
79 double[] data = new double[numberOfValues];
80
81 for (int i = 0; i < numberOfValues; i++)
82 {
83 data[i] = in.nextDouble(); // May throw NoSuchElementException
84 }
85
86 if (in.hasNext())
87 {
88 throw new IOException("End of file expected");
89 }
90 return data;
91 }
92 }
Create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java code above 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 (Links to an external site.)Links to an external site. 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 Horstmanns 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:
File->New File Keep the Project name at the top; keep Filter blank
Categories choose Other (at the bottom of the categories list) File Types choose 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 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started