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

Create, using NetBeans, a complete Java program called CalcWeightedAvg according

ID: 3681111 • Letter: C

Question

Create, using NetBeans, a complete Java program called CalcWeightedAvg according to the following guidelines. The input values come from a single line in a text le such as the following: 0.5 70 80 90 10 0.30 80 90 0 100 0.1 50 60 50 70 The output le should look something like this: “When using the data 0.5 70 80 90 10 (where 0.5 is the weight, and the average is computed a er dropping the lowest of the rest of the values.) the weighted average is 40. When using the data 0.3 80 90 0 100 (where 0.3 is the weight, and the average is computed a er dropping the lowest of the rest of the values.) the weighted average is 30. When using the data 0.1 50 60 50 70 (where 0.1 is the weight, and the average is computed a er dropping the lowest of the rest of the values.) the weighted average is 6.” Thoughts: *) You'll need to modify your methods to handle three lines of data instead of just one.

Explanation / Answer

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class CalWeightAverageFile {
  
   public static int smallest(int[] list){
       int small = list[0];
       for(int x: list){
           if(small > x)
               small = x;
       }
       return small;
   }
  
   public static double calcWeightedAvg(int[] list, double weight){
      
       int sum = 0;
       int smallest = smallest(list); // getting smallest element
       boolean flag = false;
       for(int x : list){
           if(smallest != x || flag){ // ignoring smallest element
               sum +=x;
           }else{
               flag = true;
           }
       }
      
       double avg = ((double)sum)/(list.length-1);
      
       return avg*weight;
   }

   public static void main(String[] args) throws IOException {
      
       File f = new File("input.txt");
       FileReader fr = new FileReader(f);
       BufferedReader br = new BufferedReader(fr);
       String line;
      
       int []arr = new int[4]; // array of 4 integers
       double weight;
       while((line=br.readLine()) != null){
          
           String s[] = line.split("\s+");
           weight = Double.parseDouble(s[0]);
           for(int i=1; i<s.length; i++){// splitting by space
               int x = Integer.parseInt(s[i]);
               arr[i-1] = x;
           }
          
           // getting smallest element
           int smallest = smallest(arr);
           double avg = calcWeightedAvg(arr, weight);
           System.out.print("“When using the data ");
          
           System.out.print(weight+" ");
           for(int i=0; i<arr.length; i++)
               System.out.print(arr[i]+" ");
           System.out.println("where "+weight+" is the weight, and the "+
                       "average is computed after dropping the lowest "+smallest+" of the rest of the values.)"+
                       " the weighted average is "+avg+".");
           System.out.println();
       }
       br.close();
       fr.close();
   }

}

/*

Output:

“When using the data 0.5 70 80 90 10 where 0.5 is the weight, and the average is computed after dropping the lowest 10 of the rest of the values.) the weighted average is 40.0.

“When using the data 0.3 80 90 0 100 where 0.3 is the weight, and the average is computed after dropping the lowest 0 of the rest of the values.) the weighted average is 27.0.

“When using the data 0.1 50 60 50 70 where 0.1 is the weight, and the average is computed after dropping the lowest 50 of the rest of the values.) the weighted average is 6.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