Rework Programming Assignment 13, but with the following additions: instead of j
ID: 3693570 • Letter: R
Question
Rework Programming Assignment 13, but with the following additions: instead of just one line of input , use three lines of input data as specified below, and m odify the information you print to the output file accordingly. (See Horstmann, pp. 319, 320, and pp. 350,351). 1. Create, using NetBeans, a complete Java program called CalcWeightedAvg according to the following guidelines. The input values come from a single line in a text file such as the following: 0.5 70 80 90 10 0.30 80 90 0 100 0.1 50 60 50 70 The output file should look something like this: “When using the data 0.5 70 80 90 10 (where 0.5 is the weight, and the average is computed after dropping the lowest of the rest of the values.) the weighted average is 40. When using the data 0.3 80 90 0 100 (where 0.3 is the weight, and the average is computed after dropping the lowest of the rest of the values.) the weighted average is 30. When using the data 0.1 50 60 50 70 (where 0.1 is the weight, and the average is computed after dropping the lowest of the rest of the values.) the weighted average is 6.” Thoughts: *) You'll need to modify your methods to handle three lines of data instead of just one. package calcweightedavg; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class CalcWeightedAvg{ static BufferedReader br; static String line; public static ArrayList getData() { ArrayList values=new ArrayList<>(); if(line!=null) {String array[]=line.split(" "); values.addAll(Arrays.asList(array)); } return values; } static void printResults(ArrayList values,double avg) { System.out.println("Please provide Output file name"); Scanner w=new Scanner(System.in); String outputFile=w.next(); try{ try (BufferedWriter bw = new BufferedWriter(new FileWriter("data(2).txt"+outputFile))) { bw.write(String.valueOf("The weighted average of the numbers is "+avg+" when using the data "+line+" where "+values.get(0)+" is weight and the average is computed after dropping the lowest of the rest of the values.")); } } catch (Exception e) { System.out.println(e.toString()); } } static double CalcWeightedAvg(ArrayList values) { double weight=Double.parseDouble(values.get(0).toString()); double total=0; double min=Double.parseDouble(values.get(1).toString()); for(int i=1;iExplanation / Answer
/** Java program called CalcWeightedAvg **/
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CalcWeightedAvg {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
//System.out.println(System.getProperty("user.dir"));
ArrayList<Double> inputValues = getData(); // User entered integers.
double weightedAvg = calcWeightedAvg(inputValues); // User entered weight.
printResults(inputValues, weightedAvg); //Weighted average of integers.
}
public static ArrayList<Double> getData() throws FileNotFoundException {
// Get input file name.
Scanner console = new Scanner(System.in);
System.out.print("Input File: ");
String inputFileName = console.next();
File inputFile = new File(inputFileName);
//
Scanner in = new Scanner(inputFile);
String inputString = in.nextLine();
//
String[] strArray = inputString.split("\s+");
// Create arraylist with integers.
ArrayList<Double> doubleArrayList = new ArrayList<>();
for (String strElement : strArray) {
doubleArrayList.add(Double.parseDouble(strElement));
}
in.close();
return doubleArrayList;
}
public static double calcWeightedAvg(ArrayList<Double> inputValues){
//Get and remove weight.
Double weight = inputValues.get(0);
inputValues.remove(0);
//Sum and find min.
double min = Double.MAX_VALUE;
double sum = 0;
for (Double d : inputValues) {
if (d < min) min = d;
sum += d;
}
// Calculate weighted average.
return (sum-min)/(inputValues.size()-1) * weight;
}
public static void printResults(ArrayList<Double> inputValues, double weightedAvg) throws IOException {
Scanner console = new Scanner(System.in);
System.out.print("Output File: ");
String outputFileName = console.next();
PrintWriter out = new PrintWriter(outputFileName);
System.out.println("Your output is in the file " + outputFileName);
out.print("The weighted average of the numbers is " + weightedAvg + ", ");
out.print("when using the data ");
for (int i=0; i<inputValues.size(); i++) {
out.print(inputValues.get(i) + ", ");
}
out.print(" where " + inputValues.get(0) + " is the weight, ");
out.print("and the average is computed after dropping the lowest of the rest of the values. ");
out.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.