Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete Java program called WeightedAvgDropSmallest according to the fo

ID: 3810583 • 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: get the numbers used to calculate the average, get the number of lowest numbers to drop before calculating the 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, and 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.00 60.00 80.00 100.00 20.00 ...and the user gives the number 2 to indicate how many of the lowest values should be dropped before calculating the average, then the program should give as output: Given the numbers 40.00, 60.00, 80.00, 100.00, and 20.00, the average of all the numbers except the lowest 2 numbers is 80.00.

Explanation / Answer

WeightedAvgDropSmallest.java


import java.util.Scanner;
public class WeightedAvgDropSmallest{


      
       public static void main(String[] args) {
               double d[] = getValuesByUser();
               int lowest = getLowestValueByUser();
               double average = removeLowestDoubleAndCalAvg(d, lowest);
               printValues(d, lowest, average);
       }
       public static double[] getValuesByUser(){
           Scanner scan = new Scanner(System.in);
           double values[] = new double[5];
           System.out.println("Please enter 5 values : ");
           for(int i=0; i<values.length; i++){
               values[i] = scan.nextDouble();
           }
           return values;
       }
       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 removeLowestDoubleAndCalAvg(double[] d, int lowest){
           double minOne = d[0];
           double minTwo = d[0];
   for(double n:d){
   if(minOne > n){
   minTwo = minOne;
   minOne =n;
   } else if(minTwo < n){
   minTwo = n;
   }
   }
   double avg = 0;
   double total = 0;
   for(int i=0; i<d.length; i++){
       if(d[i] != minOne && d[i] != minTwo){
           total = total + d[i];
       }
   }
   avg = total/(d.length-lowest);
   return avg;   
       }
       public static void printValues(double[] d, int lowest, double avg){
           System.out.print("Given the numbers ");
           for(int i=0; i<d.length; i++){
               System.out.print(d[i] +", ");
           }
           System.out.println();
           System.out.println("The average of all the numbers except the lowest "+lowest+" numbers is "+avg+".");
       }
   }

Output:

Please enter 5 values :
40.00 60.00 80.00 100.00 20.00
Please enter lowest number :
2
Given the numbers 40.0, 60.0, 80.0, 100.0, 20.0,
The average of all the numbers except the lowest 2 numbers is 80.0.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote