Write a complete Java program called WeightedAvgDropSmallest according to the fo
ID: 3812788 • Letter: W
Question
Write a complete Java program called WeightedAvgDropSmallest according to the following guidelines.
The program prompts the user for five to ten numbers all on one line, separated by spaces, calculates the weighted average of all those numbers except the lowest n numbers, where n and the weight are also given by the user, and displays all the numbers, the weight, the number of lowest numbers dropped, and the calculated average to the user.
The program uses methods to:
1. get the numbers used to calculate the average,
2. get the number of lowest numbers to drop before calculating the
3. get the weight, a double greater than 0 and less than or equal to 1,
4. calculate the weighted average of the numbers (except the lowest n numbers) entered by the user, and
5. print the results.
The first method should take no arguments and return an array list of doubles.
The second method should take no arguments and return a single integer, the number of the lowest numbers to drop before calculating the average.
The third method should take no arguments and return a double (the weight)
The fourth method should take three arguments: an array list of numbers (the return value of the first method above); an integer (the number of smallest items to drop before calculating the average); and a double (the weight). This method should return a double (the weighted average of all the numbers except the lowest n values).
The fifth method should take four arguments: an array list of numbers (the return value of the first method above); an integer (the number of smallest numbers to drop before calculating the average); a double (the weight); and a double (the weighted average returned from the fourth method above). This method should have no return value.
For example:
If the user gives these numbers for calculating the average:
40 60 80 100 20
and the user gives the number 2 to indicate how many of the lowest values should be dropped before calculating the average, and gives a weight of 0.5, then the program should give as output:
The weighted average of the numbers is 40.0, when using the data 40.0, 60.0, 80.0, 100.0, 20.0, where 0.5 is the weight used, and the average is computed after dropping the lowest 2 values.
Explanation / Answer
JAVA PROGRAM :
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static ArrayList<Double> getDoubles() throws Exception{
System.out.println("Enter the double values");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = br.readLine().trim().split(" ");
ArrayList<Double> al = new ArrayList();
for(int i=0;i<str.length;i++){
al.add(Double.parseDouble(str[i]));
}
return al;
}
static int getNumberOfLowest(){
System.out.println("Enter the number of lowest values to be dropped");
return new Scanner(System.in).nextInt();
}
static double getWeight(){
System.out.println("Enter the weight");
return new Scanner(System.in).nextDouble();
}
static double getWeightedAvg(ArrayList<Double> al, int l, double w){
double sum = 0;
Collections.sort(al);
for(int i=l;i<al.size();i++){
sum = sum + al.get(i);
}
return (sum*w)/(al.size()-l);
}
static void doNothing(ArrayList<Double> al, int l, double w, double avg){
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
ArrayList<Double> list = getDoubles();
int l = getNumberOfLowest();
double w = getWeight();
double avg = getWeightedAvg(list,l,w);
System.out.println("Weighted avg is "+avg);
}
}
OUTPUT :
D:>java Ideone
Enter the double values
40 60 80 100 20
Enter the number of lowest values to be dropped
2
Enter the weight
0.5
Weighted avg is 40.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.