Assignment Write a complete Java program called CalcWeightedAvgDropLowest accord
ID: 3821547 • Letter: A
Question
Assignment
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 the name of the input file - which you should hard-code to "data.txt".
See Horstmann's program Total.java in Section 7.1, pp. 319, 320, on which this assignment is based.
For our program, you should create the data.txt file ahead of time (see below) and then prompt the user for the name of the input file, telling the user that the name of the input file should be "data.txt".
Note that the methods for getting the input from a file and writing the output to a file will need to handle exceptions, whereas in Horstmann's Total.java example, only the main method needs to handle exceptions.
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 include methods to get the data, to calculate the weighted average, and to print the results. In the method that prints the results, you should prompt the user for the name of an output file and print the results 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
Ans:;
code:
// class Calc_weighted_Avg_Drop_lowest
public class CalcWeightedAvgDropLowest {
// main and filenot found exception
public static void main(String[] args) throws FileNotFoundException {
// ArrayList _double
ArrayList<Double> inputValues = getData();
// WeightedAvg__double
double weightedAvg = calcWeightedAvg(inputValues);
// prints the result
printResults(inputValues, weightedAvg);
}
// Arraylist method
public static ArrayList<Double> getData() throws FileNotFoundException {
// this promts for input file_name
// scanner
Scanner in = new Scanner(new File("data.txt"));
// input values to arraylist
ArrayList<Double> inputValues = new ArrayList<Double>();
// next double-- while
while (in.hasNextDouble())
{
// input values
inputValues.add(in.nextDouble());
}
// close
in.close();
// return input values
return inputValues;
}
/// CalcWeightedAvg
public static double calcWeightedAvg(ArrayList<Double> inputValues) throws FileNotFoundException {
// calculate weigted av
double sum = 0;
// avg
double average = 0;
// int i
int i = 0;
// weightavg
double weightavg = 0;
// this Calcuate the avg of array_list with lowest_numbers_dropped
// the avg CALC is '42.5'
// for loop
for (i = 0; i < inputValues.size(); i++)
{
// get
if (inputValues.get(i) > inputValues.get(1))
{
// cond
List<Double> view = inputValues.subList(2, inputValues.size());
}
}
// avg
average = sum /inputValues.size();
// wightavg
weightavg = average * inputValues.get(0);
// return
return weightavg;
}
// print results arraylist weighted avg nd exceptoin
public static void printResults(ArrayList<Double> inputValues, double weightedAvg) throws FileNotFoundException {
// scanner
Scanner scnr = new Scanner(System.in);
// sop output file
System.out.print("Output File: ");
// string op filename
String outputFileName = scnr.next();
// print writer out
PrintWriter out = new PrintWriter(outputFileName);
// prints the op weighted avg
out.print("The weigted_avg of the numbers is " + weightedAvg + ", when using the data " + inputValues + ", where " +inputValues.get(0)+ " is the weight_used, andd the average is computed after dropping the lowest " +inputValues.get(1)+ " values.");
// close
out.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.