Create using NetBeans a complete Java program called Weighte
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:
get the numbers whose average is to be calculated
get the number of numbers to drop before calculating the average
get the weight – a double from 0.0 to 1.0 – to use to calculate the weighted average
calculate the weighted average of the numbers (except the lowest n numbers) entered by the user; and
print the results.
The first method should take no arguments and return an array list of numbers (doubles) that the user entered.
The second method should take no arguments and return an integer (the number of the lowest numbers to be dropped 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 (a weight between 0.0 and 1.0 inclusive). 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 items to drop before calculating the average); a double (a weight between 0.0 and 1.0 inclusive); and a double (the weighted average returned from the fourth method above). This method should have no return value.
For example:
If the user input is:
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 to be used in calculated the weighted average, then
the program should give as output:
Given the numbers 40.00, 60.00, 80.00, 100.00, and 20.00, and using a weight of 0.5, the weighted average of all the numbers except the lowest 2 numbers is 40.00.
Solution
package weightedaverage;
import java.util.Scanner;
public class WeightedAverage {
double arr[] = new double[5];
double sarr[] = new double[5];
int nsmall=0,n;
Scanner sc = new Scanner(System.in);
public double[] readValues(){
System.out.println(\"\\t READING VALUES\");
int i;
System.out.print(\"Enter size of the elements:\\t\");
n = sc.nextInt();
for(i=0;i<n;i++){
arr[i] = Double.parseDouble(sc.next());
}
return arr;
}
public int small(){
int i;
System.out.print(\"Enter number of smalls to vomit = \");
nsmall = sc.nextInt();
for(i=0;i < sarr.length; i++){
sarr[i] = arr[i];
}
sort(sarr);
return nsmall; // some value
}
public void sort(double sarr[]){
int i,j;
for(i=0;i<sarr.length;i++){
for(j=0;j<sarr.length;j++){
if(sarr[i] <= sarr[j]){
double temp= sarr[i];
sarr[i] =sarr[j];
sarr[j] = temp;
}
}
}
}
public double weight(){
double w=0;
do{
System.out.print(\"Enter weight to find weighted average:\\t\");
w = sc.nextDouble();
}while(w <0.0 && w > 1.0);
return w;
}
public double weightedAverage(double values[],int count,double w){
double avg = 0.0;
int j;
for(j=2;j<sarr.length;j++){
avg += sarr[j];
}
System.out.println(\"\ avg = \" +avg/n);
return (avg/(n-count))*w;
}
public void printWeightedAverage(double values[],int count,double w,double avg){
int i;
System.out.print(\"given the values \\t\");
for(i=0;i<values.length;i++){
System.out.print(values[i]+\"\\t\");
}
System.out.print(\"\ and weight=\"+w+\"\ \");
System.out.print(\"The Weighted average by eliminating =\"+count+\" is =\"+avg+\"\ \");
}
public static void main(String[] args) {
int count=0;
double w =0.0,avg =0.0;
double values[] = new double[30];
WeightedAverage wa = new WeightedAverage();
values= wa.readValues();
count = wa.small();
w = wa.weight();
avg = wa.weightedAverage(values,count,w);
wa.printWeightedAverage(values,count,w,avg);
}
}


