Instead of getting the data from the user via System.in, get only input and outp
ID: 3820528 • Letter: I
Question
Instead of getting the data from the user via System.in, get only input and output file names from the user.
(See Horstmann, pp. 319, 320). 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, 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 CalcWeightedAvgDropLowest according to the following guidelines. Your main program should contain just three lines like these:
ArrayList 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 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 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 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).
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 program from the command line, the input file should be in the same directory as the .class file.
REQUIREMENTS:
Can only use these imports only!
import java.util.Collections;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
java.lang.Double parseDouble;
java.lang.Integer parseInt;
java.io.IOException;
Cannot declare global variables.
Must get input and output file names from user.
Explanation / Answer
/**
The java program that read an input file "data.txt" and finds the weighted average and then
prompts user to enter file name and then write the weighted averate to output file .
*/
//CalcWeightedAvgDropLowest.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class CalcWeightedAvgDropLowest {
public static void main(String[] args) throws FileNotFoundException
{
//calling getData
ArrayList<Double> inputValues = getData();
//calling calcWeightedAvg method
double weightedAvg = calcWeightedAvg(inputValues);
//calling printResults method
printResults(inputValues, weightedAvg);
}
/*
* Method that reads data.txt file into the arralist
* */
public static ArrayList<Double> getData() throws FileNotFoundException {
// Open an input file data.txt
Scanner in = new Scanner(new File("data.txt"));
ArrayList<Double> inputValues = new ArrayList<Double>();
while (in.hasNextDouble())
{
//read data intp inputValues
inputValues.add(in.nextDouble());
}
in.close();
return inputValues;
}
/**
* Method that calculates the weighed average value
* */
public static double calcWeightedAvg(ArrayList<Double> inputValues) throws FileNotFoundException {
// calc weighted av
double sum = 0;
double average = 0;
int i = 0;
double weightavg = 0;
double weight=inputValues.get(0);
double removeCount=inputValues.get(1);
ArrayList<Double>tempList=new ArrayList<>(inputValues);
for (i = 2; i < tempList.size(); i++)
{
sum+=tempList.get(i);
}
//remove starting two values
tempList.remove(0);
tempList.remove(0);
for (int j = 0; j <removeCount ; j++)
{
double value=getLowest(tempList);
sum-=value;
tempList.remove(value);
}
average = sum /tempList.size();
//caluclate weighted average
weightavg = average * weight;
return weightavg;
}
//Helper method to find lowest value
private static double getLowest(ArrayList<Double> inputValues) {
double low=inputValues.get(0);
for (int i = 1; i < inputValues.size(); i++)
{
if(inputValues.get(i)<low)
{
low=inputValues.get(i);
}
}
return low;
}
/*
*
* Method to write output to the user given file name*/
public static void printResults(ArrayList<Double> inputValues,
double weightedAvg) throws FileNotFoundException {
Scanner scnr = new Scanner(System.in);
System.out.print("Enter output File: ");
//prompt for output file name
String outputFileName = scnr.next();
//open printwriter object
PrintWriter out = new PrintWriter(outputFileName);
//write to file
out.print("The weighted average of the numbers is "
+ weightedAvg + ", when using the data " +
inputValues + ", where " +inputValues.get(0)+
" is the weight used, and the average is "
+ "computed after dropping the lowest "
+inputValues.get(1)+" values.");
//close the output file
out.close();
}
}
-------------------------------------------------------------------------
Input file
data.txt
0.5 3 10 70 90 80 20
--------------------------------------------------------------------------
Enter output File: average.txt
average.txt data
The weighted average of the numbers is 42.5, when using the data [0.5, 3.0, 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.0 values.