Programming Assignment 13 – Rework Programming Assignment 12, but instead of getting the integer values from the user via System.in, get only input and output file names from the user. The input file should contain the weight and integer values as specified below. (See Horstmann, pp. 319, 320, and pp.350, 351). Also, include a method called printResults that prompts the user for the name of an output file and then prints the results to that user-designated output file.
1. Create, using NetBeans, a complete Java program called CalcWeightedAvg according to the following guidelines.
Your main program 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 a text file such as the following: 0.5 70 80 90 10
The output file should contain something like the following: “The weighted average of the numbers is 40, when using the data 0.5 70 80 90 10, where 0.5 is the weight, and the average is computed after dropping the lowest of the rest of the values.”
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 CalcWeightedAvg. 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 first number is a double (greater than 0.0 and less than or equal to 1.0) and the other numbers are the values you’ll work with. 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).
Thoughts:
*) It’s important that your input file is where NetBeans will look to find it. The above instructions should make sure that that happens.
*) Note that if you run this progam from the command line, the input file should be in the same directory as the .class file.