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

Java Code Help! Code this: Calculate statistics. Write a class called Statistics

ID: 673848 • Letter: J

Question

Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a number of properties about an array of doubles. There should be separate static methods to calculate the min, the max, the mean, the median, the standard deviation (make sure to make use of your mean method to implement this), and the mode. For the mode, you can assume that the data set is not multimodal. This class should not have a main method. Write a separate tester class called StatisticsTester which only has a main method. When run, it will ask the user for how many numbers they wish to enter and then allow the user to enter that many numbers. Store those numbers in an array and run each of the methods from the Statistics class on this data set. Print out the results.

Explanation / Answer

Statistics.java:


class statistics {
static double myarray[]=new double[50];
static double sortedarr[]=new double[50];
  
public static double min(){
int i;
double min=0;
for (i=0;i<myarray.length;i++){
if(myarray[i]<=myarray[i+1]) //compare every 2 consecutive elements of array and store the min
min=myarray[i];
}
return min;
}
  
public static double max(){
int i;
double max=0;
for (i=0;i<myarray.length;i++){
if(myarray[i]>=myarray[i+1]) //compare every 2 consecutive elements of array and store the max
max=myarray[i];
}
return max;
}
  
public static double mean(){
int i;
double mean=0;
for (i=0;i<myarray.length;i++){
mean=mean+myarray[i];
}
mean=mean/myarray.length;
return mean;
}
  
public static double stdDeviation(){ //apply the formula (sum(i*i)/n)-(sum(i)/n)^2
int i;
double std=0,std1=0,std2=0;
for (i=0;i<myarray.length;i++){
std1=myarray[i]*myarray[i];
}
std1=std1/myarray.length;
std2=mean()*mean();
std=std1-std2;
return std;
}
  
public static double median(){
int n=myarray.length,c,d,med1;
double med=0,swap;

sortedarr=myarray;
//first apply bubble sort to sort the array
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (sortedarr[d] > sortedarr[d+1]) /* For decreasing order use < */
{
swap = sortedarr[d];
sortedarr[d] = sortedarr[d+1];
sortedarr[d+1] = swap;
}
}
}
//take median of sorted array
med1=n/2;
med=sortedarr[med1];
return med;
}
  
public static double mode(){
int i,n=myarray.length,k,j;
double mode=0,max=0;
for (i = 0; i < n - 1; i++)
{
mode = 0;
for (j = i + 1; j < n; j++)
{
if (myarray[i] == myarray[j]) {
mode++;
}
}
if ((mode > max) && (mode != 0)) {
k = 0;
max = mode;
}
  
}
return max;
}
}

StatisticsTester.java:


import java.util.Scanner;


public class StatisticsTester{
  
public static void main(){
statistics tester=new statistics();
Scanner in=new Scanner(System.in);
int number,i;
double min,max,mean,med,std,mode;
System.out.println("How many numbers do you want to enter? ");
number=in.nextInt();
for(i=0;i<number;i++){
System.out.println("Enter the number ");
tester.myarray[i]=in.nextDouble();
min=tester.min();
max=tester.max();
mean=tester.mean();
med=tester.median();
std=tester.stdDeviation();
mode=tester.mode();
System.out.println("In the class statistics, for the entered numbers min is "+min+" max is "+max+"Median is "+med+"Standard deviation is "+std+"Median is "+med+"Mode is "+mode);
}
}
  
}

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