This solution needs to be in one java file only This solutio
This solution needs to be in one java file only.
This solution needs to have the output show in the order the user entered them not in sorting order.
This solution also needs proper formatting and spacing.
This solution needs to use an Array List.
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
WeightedAvgDropSmallest.java
import java.util.ArrayList;
import java.util.Scanner;
public class WeightedAvgDropSmallest {
public static void main(String[] args) {
ArrayList<Double> list = getValuesByUser();
int lowest = getLowestValueByUser();
double average = removeLowestDoubleAndCalAvg(list, lowest);
double weight = getWeight();
printValues(list, lowest, average, weight);
}
public static ArrayList<Double> getValuesByUser(){
Scanner scan = new Scanner(System.in);
ArrayList<Double> list = new ArrayList<Double>();
System.out.println(\"Please enter 5 values : \");
for(int i=0; i<5; i++){
list.add(scan.nextDouble());
}
return list;
}
public static int getLowestValueByUser(){
Scanner scan = new Scanner(System.in);
System.out.println(\"Please enter lowest number : \");
int n = scan.nextInt();
return n;
}
public static double getWeight(){
Scanner scan = new Scanner(System.in);
System.out.println(\"Please enter weight : \");
double weight = scan.nextDouble();
return weight;
}
public static double removeLowestDoubleAndCalAvg(ArrayList<Double> list, int lowest){
double minOne = list.get(0);
double minTwo = list.get(0);
for(double n:list){
if(minOne > n){
minTwo = minOne;
minOne =n;
} else if(minTwo < n){
minTwo = n;
}
}
double avg = 0;
double total = 0;
for(int i=0; i<list.size(); i++){
if(list.get(i) != minOne && list.get(i) != minTwo){
total = total + list.get(i);
}
}
avg = total/(list.size()-lowest);
return avg;
}
public static void printValues(ArrayList<Double> list, int lowest, double avg, double weight){
System.out.print(\"Given the numbers \");
for(int i=0; i<list.size(); i++){
System.out.print(list.get(i) +\", \");
}
System.out.println();
System.out.println(\"and using a weight of \"+weight+\", The weighted average of all the numbers except the lowest \"+lowest+\" numbers is \"+avg*weight+\".\");
}
}
Output:
Please enter 5 values :
40 60 80 100 20
Please enter lowest number :
2
Please enter weight :
0.5
Given the numbers 40.0, 60.0, 80.0, 100.0, 20.0,
and using a weight of 0.5, The weighted average of all the numbers except the lowest 2 numbers is 40.0.


