This Solution needs to be one java file not multiples This S

This Solution needs to be one java file not multiples.

This Solution also needs proper java formatting.

Create, using NetBeans, a complete Java program called CalcAvg according to the following guidelines. (This program requires the use of arrays.)

The program prompts the user for five to ten numbers all on one line, separated by spaces, calculates the average of those numbers, and displays the numbers and their average to the user.

The program uses methods to:

get the numbers entered by the user

calculate the average of the numbers entered by the user

print the results.

The first method should take no arguments and return an array of doubles that the user entered.

The second method should take an array of doubles (the return value of the first method above) as its only argument and return a double (the average).

The third method should take an array of doubles and a (single) double value as arguments but have no return value.

For example:

If the user input is:

40 60 20 80 100

Then the program should give as output:

The average of the numbers 40.00, 60.00, 20.00, 80.00, and 100.00 is 60.00.

Solution

package org.students;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class CalAvgDropLowest {
   //Declaring static variables
   static Scanner sc=null;
  
   //Declaring constant
   public static int SIZE;
   public static void main(String[] args) {
      
       //Declaring variables
       int n;
       double average=0.0;
      
       Random r = new Random();
       SIZE= r.nextInt((10 - 5) + 1) + 5;
  
       //Creating an double array based on the constant SIZE
       double arr[]=new double[SIZE];
      
      
   //Scanner class object is sued to read the inputs entered by the user
   sc=new Scanner(System.in);
  
   //Calling the method which will get the numbers entered by the user
   arr=getTheNumbers();
  
   //calling the method which will get the number entered by the suer
   n=numOfLowestTermsTodrop();
  
   //Calling the method which will calculate the average
   average=calAvg(arr,n);
  
   //Calling the method which will display the results on the console
   displayResults(arr,n,average);
  
   }

   /* This method will display the results on the console
   * Params :double array,number of type integer,average of type double
   * Return :void
   */
   private static void displayResults(double[] arr, int n, double average) {
      
       System.out.print(\"\ Given the numbers \");
       for(int i=0;i<SIZE;i++)
       {
       System.out.print(arr[i]+\", \");  
       }
       System.out.printf(\"\ the average of all the numbers except the lowest %d numbers is %.2f\",n,average);
      
      
   }

   /* This method will calculate the average of the numbers by leaving the lowest number of numbers
   * Params :double array,number of type integer
   * Return :average of type double
   */
   private static double calAvg(double[] arr, int n) {
      
       //Sorting the double type array in ascending order
       Arrays.sort(arr);
      
       //Declaring local variables
       double sum=0.0,avg=0.0;
      
       //This for loop will calculate the sum of array elements by leaving the no of lower numbers
       for(int i=n;i<SIZE;i++)
       {
           //calculating the sum
           sum+=arr[i];
       }
      
       //calculating the average
       avg=sum/(SIZE-n);
      
       return avg;
   }

   /* This method will get how many lower elements have to leave from the user
   * Params :void
   * Return :number of type int
   */
   private static int numOfLowestTermsTodrop() {
      
       //Declaring the local variable
       int n;
      
       //Getting the number entered by the user
       System.out.print(\"\ Enter the number of lowest terms to drop :\");
       n=sc.nextInt();
       return n;
   }

   /* This number will get numbers entered by the user and populating them into an array
   * params:void
   * return:double ype array
   */
   private static double[] getTheNumbers() {
      
       //Creating an double type array based on the size
       double arr[]=new double[SIZE];
      
       //Getting the numbers entered by the user
       System.out.print(\"Enter \"+SIZE+\" numbers in one line :\");
       for(int i=0;i<SIZE;i++)
       {
           //Populating the numbers into an array
           arr[i]=sc.nextDouble();
       }
       return arr;
   }

}

__________________________________________

package org.students;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class CalAvgDropLowest {
   //Declaring static variables
   static Scanner sc=null;
  
   //Declaring constant
   public static int SIZE;
   public static void main(String[] args) {
      
       //Declaring variables
       int n;
       double average=0.0;
      
       Random r = new Random();
       SIZE= r.nextInt((10 - 5) + 1) + 5;
  
       //Creating an double array based on the constant SIZE
       double arr[]=new double[SIZE];
      
      
   //Scanner class object is sued to read the inputs entered by the user
   sc=new Scanner(System.in);
  
   //Calling the method which will get the numbers entered by the user
   arr=getTheNumbers();
  
   //calling the method which will get the number entered by the suer
   n=numOfLowestTermsTodrop();
  
   //Calling the method which will calculate the average
   average=calAvg(arr,n);
  
   //Calling the method which will display the results on the console
   displayResults(arr,n,average);
  
   }

   /* This method will display the results on the console
   * Params :double array,number of type integer,average of type double
   * Return :void
   */
   private static void displayResults(double[] arr, int n, double average) {
      
       System.out.print(\"\ Given the numbers \");
       for(int i=0;i<SIZE;i++)
       {
       System.out.print(arr[i]+\", \");  
       }
       System.out.printf(\"\ the average of all the numbers except the lowest %d numbers is %.2f\",n,average);
      
      
   }

   /* This method will calculate the average of the numbers by leaving the lowest number of numbers
   * Params :double array,number of type integer
   * Return :average of type double
   */
   private static double calAvg(double[] arr, int n) {
      
       //Sorting the double type array in ascending order
       Arrays.sort(arr);
      
       //Declaring local variables
       double sum=0.0,avg=0.0;
      
       //This for loop will calculate the sum of array elements by leaving the no of lower numbers
       for(int i=n;i<SIZE;i++)
       {
           //calculating the sum
           sum+=arr[i];
       }
      
       //calculating the average
       avg=sum/(SIZE-n);
      
       return avg;
   }

   /* This method will get how many lower elements have to leave from the user
   * Params :void
   * Return :number of type int
   */
   private static int numOfLowestTermsTodrop() {
      
       //Declaring the local variable
       int n;
      
       //Getting the number entered by the user
       System.out.print(\"\ Enter the number of lowest terms to drop :\");
       n=sc.nextInt();
       return n;
   }

   /* This number will get numbers entered by the user and populating them into an array
   * params:void
   * return:double ype array
   */
   private static double[] getTheNumbers() {
      
       //Creating an double type array based on the size
       double arr[]=new double[SIZE];
      
       //Getting the numbers entered by the user
       System.out.print(\"Enter \"+SIZE+\" numbers in one line :\");
       for(int i=0;i<SIZE;i++)
       {
           //Populating the numbers into an array
           arr[i]=sc.nextDouble();
       }
       return arr;
   }

}

_________________________Thank You

This Solution needs to be one java file not multiples. This Solution also needs proper java formatting. Create, using NetBeans, a complete Java program called C
This Solution needs to be one java file not multiples. This Solution also needs proper java formatting. Create, using NetBeans, a complete Java program called C
This Solution needs to be one java file not multiples. This Solution also needs proper java formatting. Create, using NetBeans, a complete Java program called C
This Solution needs to be one java file not multiples. This Solution also needs proper java formatting. Create, using NetBeans, a complete Java program called C
This Solution needs to be one java file not multiples. This Solution also needs proper java formatting. Create, using NetBeans, a complete Java program called C

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site