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

Write a JAVA program that includes two methods named calcvaerage () and variance

ID: 3841788 • Letter: W

Question

Write a JAVA program that includes two methods named calcvaerage() and variance(). The clacaverage() method should calculate and return the average of the values stored in the array named testvals. This array should be declared in the main() and should hold double type 15 values. These values are input by the user when the program is run. The variance() method should calculate and return the variance of the data stored in the testvals array. The variance is obtained by subtracting the average from each value in testvals, squaring these differences, adding them, and dividing this sum by number of elements in testvals. The values obtained from calcaverage() and variance() should be displayed using println statements within the main() function.

Explanation / Answer

PROGRAM CODE:

import java.util.*;
import java.lang.*;
import java.io.*;


public class MathCalculation
{
   //average calculation
   public static double calcaverage(double testvals[])
   {
       double sum = 0;
       for(int x=0; x<testvals.length; x++)
           sum += testvals[x];
       return sum/testvals.length;
   }
  
   //variance calculation
   public static double variance(double testvals[])
   {
       double total = 0;
       double avg = calcaverage( testvals);
       for(int x=0; x<testvals.length; x++)
           total += Math.pow(testvals[x] - avg, 2);
       return total/testvals.length;
   }
   //main function
   public static void main (String[] args) throws java.lang.Exception
   {
       //arrays containing double values
       double testVals[] = new double[15];
       Scanner keyboard = new Scanner(System.in);
       //reading input from user
       System.out.println("Enter the values with space between them");
       for(int i=0; i<15;i++){
           testVals[i] = keyboard.nextDouble();
       }
       //displaying results to screen
       System.out.println("The average is " + calcaverage(testVals));
       System.out.println("The variance is " + variance(testVals));
   }
}

OUTPUT:

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