Write a program that includes two methods named calcAverage()and variance(). The
ID: 3615539 • Letter: W
Question
Write a program that includes two methods named calcAverage()and variance(). The calcAverage() method should calculate andreturn the average of the values stored in an array named testvals.The array should be declared in main() and include the values 89,95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73. The variance()method should calculate and return the variance of the data.
The variance is obtained by subtracting the average from eachvalue in testvals, squaring the difference obtained, adding theirsquares, and dividing by the number of elements in testvals.
The values returned fromcalcaverage() andvariance() should be displayed fromwithin main().
Explanation / Answer
import javax.swing.*; import java.io.*;public class stat { public static void main(String args[]) { double []testvals={89,95,72,83,99,54,86,75,92,73,79,75,82,73}; double result=calAvg(testvals); double var=variance(testvals,result); System.out.println("Average of Data:"+result); System.out.println("Variance of Data:"+var); }
public static double calAvg(double myarr[]) { double sum=0; for(int v=0;v<14;v++) { sum+=myarr[v]; } return sum/14; }
public static double variance(double myarr[], doubleave) { double sqr=0; double sqrav=0; double num=0;
for(int v=0;v<14;v++) { num=(myarr[v]-ave); sqr=num*num; sqrav=sqr/14; myarr[v]=sqrav; }
double sum=0; for(int v=0;v<14;v++) { sum+=myarr[v]; } return sum;
} } Hope it is helpful to you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.