Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete Java program called CalcWeightedAvgDropLowest according to the

ID: 3821611 • Letter: W

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, 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. This is what I was given to start the program. On the line: ArrayList inputLine = new ArrayList<>(); I was told to create an array list of doubles and return it. import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import java.util.ArrayList; public class CalcWeightedAvgDropLowest { public static void main(String{} args) throws FileNotFoundException { ArrayList input = readInputFile(); double weightedAverage = computeWeightedAverage(input); writeOutputFile(input, weightedAverage); } public static ArrayList readInputFile() throws FileNotFoundException { ArrayList inputLine = new ArrayList<>(); //This is where I should create an array list of doubles. Scanner console = new Scanner(System.in); System.out.println("Enter 'data.txt' (without the quotes) as the name of the input file: "); String inputFileName = console.next(); File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); while(in.hasNextDouble()) { inputLine.add(in.nextDouble()); } in.close(); return inputLine; } public static double computeWeightedAverage(ArrayList data) public static void writeOutputFile(ArrayList inputList, double weightedAvg) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.println("Enter the name of the output file: "); String outputFileName = console.next(); PrintWriter out = new PrintWriter(outputFileName); System.out.println("Your output is in the file " + outputFileName + "."); out.print("Hello); out.close(); }

Explanation / Answer

CalcWeightedAvgDropLowest.java


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class CalcWeightedAvgDropLowest {
   public static void main(String arg[]) throws IOException{
      
       ArrayList inputValues = getData();
       System.out.println("Input Valules :"+inputValues);
       double weightedAvg = calcWeightedAvg(inputValues);
       printResults(inputValues, weightedAvg);
      
   }
   public static ArrayList getData() throws IOException{
       Scanner console = new Scanner(System.in);
       System.out.println("Enter 'data.txt' (without the quotes) as the name of the input file: ");
       String inputFileName = console.next();
       File file = new File(inputFileName);
      
           ArrayList<Double> data = new ArrayList<Double>();
       if(file.exists()){
          
           BufferedReader br = new BufferedReader(new FileReader(file));
           String sCurrentLine;
           String values[] = null;
           while ((sCurrentLine = br.readLine()) != null) {
               values = sCurrentLine.split(" ");
           }
           for(int i=0; i<values.length; i++)
           data.add(Double.parseDouble(values[i]));
           }
       else{
           System.out.println("File does not exist");
       }
       return data;
   }
   public static double calcWeightedAvg(ArrayList list){
       double enteredWeight = Double.parseDouble(list.get(0).toString());
       int numberofLowetValuesDropped =(int) Double.parseDouble(list.get(1).toString());
       list.remove(1);
       list.remove(0);
       Collections.sort(list);
       double total = 0;
       for(int i=numberofLowetValuesDropped; i<list.size(); i++){
           total = total + Double.parseDouble(list.get(i).toString());
       }
       list.add(0, enteredWeight);
       list.add(1, numberofLowetValuesDropped);
       double avg = total/(list.size() - numberofLowetValuesDropped);
       return avg;
   }
   public static void printResults(ArrayList list, double weight) throws IOException{
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the output file:");
       String outputfile = scan.next();
       FileWriter fw = new FileWriter(new File(outputfile));
       String s = "The weighted average of the numbers is "+weight+", " +
       "when using the data "+list+", where "+list.get(0)+" is the weight used, " +
       "and the average is computed after dropping the lowest "+list.get(1)+" values.";
       System.out.println(s);
       fw.write(s);
       fw.flush();
       fw.close();
       System.out.println("Data has been written into output file");
      
   }
}

Output:

Enter 'data.txt' (without the quotes) as the name of the input file:
D:\data.txt
Input Valules :[0.5, 3.0, 10.0, 70.0, 90.0, 80.0, 20.0]
Enter the output file:
D:\output.txt
The weighted average of the numbers is 42.5, when using the data [0.5, 3, 10.0, 20.0, 70.0, 80.0, 90.0], where 0.5 is the weight used, and the average is computed after dropping the lowest 3 values.
Data has been written into output file

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote