Question
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,
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". For our program, you should create the data.txt file ahead of time 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. Your main method should include methods to get the data, a separate method to calculate the weighted average, and a separate method 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. 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. 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 for the name of the input file: " Also prompt the user for the name of the output file. The example for us to look at for guidance is: import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class Total { public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Input file: "); String inputFileName = console.next(); System.out.print("Output file: "); String outputFileName = console.next(); File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); PrintWriter out = new PrintWriter(outputFileName); double total = 0; while (in.hasNextDouble()) { double value = in.nextDouble(); out.printf("%15.2f ", value); total = total + value; } out.printf("Total: %8.2f ", total); in.close(); out.close(); } }
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