Programming Assignment 14 – Rework Programming Assignment 13, but with the follo
ID: 3690366 • Letter: P
Question
Programming Assignment 14 – 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.
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CalcWeightedAvg{
public static void main(String args[]){
double weight;
int a, b, c, d;
Scanner in = null;
try {
in = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e){
System.out.println("Unable to open file");
}
while(in.hasNext()){
weight = in.nextDouble();
a = in.nextInt();
int lowest = a;
b = in.nextInt();
if(b < lowest) lowest = b;
c = in.nextInt();
if(c < lowest) lowest = c;
d = in.nextInt();
if(d < lowest) lowest = d;
double weightedMean = weight * (a + b + c + d - lowest) / 3;
System.out.println("When using the data " + weight + " " + a + " " + b + " " + c + " " + d + "(where " + weight + " is the weight, and the average is computed after dropping the lowest of the rest of the values.) the weighted average is " + weightedMean);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.