First, launch NetBeans and close any previous projects that may be open (at the
ID: 3917657 • Letter: F
Question
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "WeightedAvgDropSmallest" (without the quotation marks)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 used to calculate the average
Get the number of lowest numbers to drop before calculating the average
Get the weight, a double greater than 0 and less than or equal to 1
Calculate the weighted average of the numbers (except the lowest n numbers) entered by the user
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.
PreviousNext
Explanation / Answer
import java.util.Scanner;
public class WeightedAvgDropSmallest {
private double[] num = new double[10];
private int size;
private int n;
private double weight;
private double weightedAvg;
public void getNum() {
Scanner S = new Scanner(System.in);
System.out.println("Enter The Numbers.If want to exit enter -1");
int ptr = 0;
while (ptr < 10) {
Double number = S.nextDouble();
if (number == -1) {
break;
}
num[ptr] = number;
ptr++;
}
size = ptr - 1;
}
public void getN() {
Scanner S = new Scanner(System.in);
System.out.println("Enter how many lowest numbers to drop");
n = S.nextInt();
}
public void getWeight() {
Scanner S = new Scanner(System.in);
System.out.println("Enter weight(between 0 and 1)");
int sz = S.nextInt();
while (sz >= 0 && sz <= 1) {
System.out.println("Error.Enter between 0 and 1");
sz = S.nextInt();
}
weight = sz;
}
public double weightedAvg(double[] arr, int k, double wgt) {
double sum = 0;
sort(arr);
for (int i = 0 + k; i < size; i++) {
sum += (arr[i] * wgt);
}
return sum / (size - k);
}
public void print(double[] arr, int k, double wgt, double wgtAvg) {
for (int i = 0; i < size; i++)
System.out.print(arr[i]+" ");
System.out.println("The weight is "+ wgt);
System.out.println("The number of lowest numbers to be dropped is "+ k);
System.out.println("The weighted average is "+ wgtAvg );
}
void sort(double arr[]) {
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
double temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
//All the best
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.