Question: Create, using NetBeans, a complete Java program called WeightedAvgDropSmallest according to the following guidelines. The program prompts the user for five to ten numbers all
Create, using NetBeans, 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 average 3) get the weight, a double greater than 0 and less than or equal to 1 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 (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 (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.00, 60.00, 80.00, 100.00, 20.00, where 0.5 is the weight used, and the average is computed after dropping the lowest 2 values.
I have the following provided:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public static void main (String[ ] args) }
ArrayList
int numberOfNumbersToDrop = 2;
//int numberOfNumbersToDrop = getNumberOfNumbersToDrop( ); //you need to write this method
double weight = 0.5;
//double weight = getWeight( ); //you need to write this method
double weightedAverage = calculateWeightedAverage(numbersFromUser, numberOfNumbersToDrop, weight);
printResults(numbersFromUser, numberOfNumbersToDrop, weight, weightedAverage);
}
public static ArrayList
Scanner in = new Scanner(System.in);
System.out.print("Enter 5 to 10 numbers all on one line, separated by spaces: ");
String lineOfNumbers = in.nextLine( );
Scanner numbers = new Scanner(lineOfNumbers);
ArrayList
while (numbers.hasNextDouble()) }
numberArrayList.add(numbers.nextDouble());
}
return numberArrayList;
}
public static double calculateWeightedAverage(ArrayList
double weightedAvg = 0.0;
ArrayList
Collections.sort(numbersForSorting);
double sum = 0.0;
for (int i = numberOfItemsToDrop; i < numbersForSorting.size( ); i++) {
sum += numbersForSorting.get(i);
}
if (numbersForSorting.size( ) -numberOfItemsToDrop) > 0) {
weightedAvg = weight* (sum/ (numbersForSorting.size( ) -numberOfItemsToDrop));
}
return weightedAvg;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
