Search code examples
javaarraysmode

two problems with mean med mode method


So here is my completed code that calculates Mean, Median, Mode, Standard deviation, min, max, q1, q2, and 5 number summary which is supposed to be returned as an array. The array is formatted properly to my knowledge, but for some odd reason the return array is spitting out this 5 number Summary:[D@689af4 and I don't for the life of my know why or how to fix it. Also the mode is outputting 22 when I need it to out put -1. Is there any one who can look at this and tell me what is wrong and what I can to do fix these issues?

import java.util.Arrays;

class Statistics {
  public static void main(String[] args)
  {
     int[] a = new int[]{22,44,66,55,33};

           bubbleSort(a);
           double mean; 
           double median; 
           int mode;
           int max;
           int min;
           double sd;
           int q1; 
           int q3; 
           double[] vals; 


           mode = calcMoe (a);
           median = calcMed (a);
           mean = calcMean (a);
           max =calcMax (a);
           min =calcMin (a);
           sd =calcSd (a);
           q1=calcQuart1 (a); 
           q3=calcQuart3 (a); 
           vals=calcNumsum (a);



           System.out.println("Median:"+median); 
           System.out.println("Mean:"+mean); 
           System.out.println("Mode:"+mode);
           System.out.println("max number is : " + max);
           System.out.println("min number is : " + min);
           System.out.println("Standard Deviation:"+sd);
           System.out.println("1st Quartile:"+q1); 
           System.out.println("3rd Quartile:"+q3);
           System.out.println("5 number Summary:"+vals);

  }



  public static double calcMean(int[] a)
  {

  // int[]array = {22,44,66,55,33}; 
    int i;//=0;
    int sum=0;
    double mean =0;
    for ( i=0;i<a.length;i++)
    { 
      //System.out.println(a[i]);
      sum=sum+a[i];
    }
    {  mean = ((double) sum/ ((double) a.length));
      //System.out.println(); } {

        return mean;}
      } 
   //Calulate median

   public static double calcMed(int[] a)
 {// Sort array 
  int[] sorta = bubbleSort(a);

  double median = 0;
  if (a.length % 2 == 0)

   {
    int indexA = (sorta.length - 1) / 2;
   int indexB = sorta.length / 2;

   median = ((double) (sorta[indexA] + sorta[indexB])) / 2;
  }
 // Else if our array's length is odd
  else  
  {
   int index = (sorta.length - 1) / 2;
   median = a[ index ];
  }

  // Print the values of the sorted array
  for (int v : sorta) 
  {
    System.out.println(v);
  }

  return median; 

   }

   public static int[] bubbleSort(int[] a)
    {
     //outer loop 
    for(int luck=0; luck<a.length -1; luck++){
     for (int juck=1; juck<a.length - luck; juck++){
       if (a[juck-1]>a[juck]){
         int temp= a[juck];
         a[juck]=a[juck-1];
         a[juck-1]=temp;
          //System.out.printf("unsorted array after %d pass %s: %n", luck+1,     Arrays.toString(a));
       }
      }
     }
    return a;
    }





 public static int calcMoe(int[] a)
{
    Arrays.sort(a);

     int count2 = 0;
     int count1 = 1;
     int pupular1 =0;
     int mode =0;


    for (int i = 0; i < a.length; i++)
    {
            pupular1 = a[i];
            count1 = 1;    

        for (int j = i + 1; j < a.length; j++)
        {
            if (pupular1 == a[j]) count1++;
        }

        if (count1 > count2)
        {
                mode = pupular1;
                count2 = count1;
        }

        if (count1 == count2)
        {
            mode = Math.min(mode, pupular1);

         }

         }


     return mode;
  }



public static int calcMax(int[] a) {
 //int min = a[0];
 int max = a[0];
  for (int i = 1; i <= a.length - 1; i++) {
  if (max < a[i]) {
   max = a[i];
   }
  }
   return max; 
}
public static int calcMin(int[] a) {
 int min = a[0];

  for (int i = 1; i <= a.length - 1; i++) {
  if (min > a[i]) {
   min = a[i];
  }
  }
   return min; 
}
public static double calcSd(int[] a) {
  //int sum = 0;
  //int max = 0;
  //int min = a[0];
  double sd = 0;
  int i = 0;
  double mean =0;
  sd=0;
    for ( i=0;i<a.length;i++)
        {
       sd += ((a[i] - mean)*(a[i] - mean)) / (a.length - 1);
    }  


 double standarddeviation = Math.sqrt(sd);
 {
 }
 return standarddeviation; 
 }
public static int calcQuart1(int[] a) {
  int[] sorta = bubbleSort(a);

  int q1 = 0;
  {
   int index = (sorta.length - 1) / 4;
   q1 = a[ index ] ;
  }


  for (int v : sorta) 
  {
    System.out.println(v);
  }

  return q1; 
 }
public static int calcQuart3(int[] a) {
  int[] sorta = bubbleSort(a);

  int q3 = 0;
  {
   int index = 3*(sorta.length - 1) / 4;
   q3 = a[ index ] ;
  }


  for (int v : sorta) 
   {
    System.out.println(v);
  }

  return q3; 
}
   public static double[] calcNumsum(int[] a) { 


  double median = calcMed (a);
  double max =calcMax (a);
  double min =calcMin (a);
  double q1=calcQuart1 (a); 
  double q3=calcQuart3 (a); 
  double[] vals = new double[5];
  vals[0] = min; 
  vals [1] = q1; 
  vals [2] = median; 
  vals [3] = q3; 
  vals [4] = max; 
  return vals;
}
   }

Solution

  • This line:

    System.out.println("5 number Summary:"+vals);
    

    simply takes vals, converts it to a String, and prints it. The default toString implementation for array types in Java produces the output you noted: the overall type ([ for 'array'), the type stored in the array (D for double), an @ symbol, and the location of the array in memory (689af4, in this case).

    To get perhaps a more useful output for your purposes, you can use Arrays.toString:

    System.out.println("5 number summary: " + Arrays.toString(vals));