Assignment Create a program called CalcWeightedAvgWithExceptions, according to t
ID: 3825016 • Letter: A
Question
Assignment
Create a program called CalcWeightedAvgWithExceptions, according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files.
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 contain just three lines like these:
ArrayList inputValues = getData();
double weightedAvg = calcWeightedAvg(inputValues);
printResults(inputValues, weightedAvg);
where the printResults method should prompt the user for the name of the output file and print 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
The below code will satisfy the required functionalities which are discussed in the question and also comments are added for better understanding.
package chegg;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CalcWeightedAvgWithExceptions {
public static void main(String[] args)
{
List<String> inputValues = new ArrayList<String>();
inputValues.add(getData());
double weightedAvg = calcWeightedAvg(inputValues);
printResults(inputValues, weightedAvg);
}
public String getData()
{
Sanner sc=new Scanner(System.in);
System.out.print("enter the input file name");
String fileName=sc.next();
try
{
Scanner input = new Scanner(new File(fileName)).useDelimiter(" ");; // read data from file using space as delimiter
}
catch(FileNotFoundException s)
{
System.out.println("File does Not Exist Please Try Again: ");
}
while (input.hasNext()) {
// find next line
token1 = input.next();
return token1; // return the tokens to add in to array
}
}
public double calcWeightedAvg()
{
int n, sum = 0;
float avg
for(int i = 0; i < n ; i++) // to find the weighted average and stored in to avg
{
a[i] = s.nextInt();
sum = sum + a[i];
}
avg = (float)sum / n;
return avg;
}
public printResults(String a,double avg){
// creates the file
file.createNewFile();
Sanner sc=new Scanner(System.in);
System.out.print("enter the output file name to create");
String outfileName=sc.next();
// creates a FileWriter Object
FileWriter Output = new FileWriter(outfileName);
// Writes the content to the file
Output.write("The weighted average of the numbers is "+avg+);
Output.flush();
Output.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.