java - two problems with mean med mode method -
so here completed code calculates mean, median, mode, standard deviation, min, max, q1, q2, , 5 number summary supposed returned array. array formatted knowledge, odd reason return array spitting out 5 number summary:[d@689af4
, don't life of know why or how fix it. mode outputting 22 when need out put -1. there 1 can @ , tell me wrong , can fix these issues?
import java.util.arrays; class statistics { public static void main(string[] args) { int[] = 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 : " + max); system.out.println("min number : " + 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; ( 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 odd else { int index = (sorta.length - 1) / 2; median = a[ index ]; } // print values of sorted array (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++){ (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; (int = 0; < a.length; i++) { pupular1 = a[i]; count1 = 1; (int j = + 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]; (int = 1; <= a.length - 1; i++) { if (max < a[i]) { max = a[i]; } } return max; } public static int calcmin(int[] a) { int min = a[0]; (int = 1; <= 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 = 0; double mean =0; sd=0; ( 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 ] ; } (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 ] ; } (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; } }
this line:
system.out.println("5 number summary:"+vals);
simply takes vals
, converts string
, , prints it. default tostring
implementation array types in java produces output noted: overall type ([
'array'), type stored in array (d
double
), @
symbol, , location of array in memory (689af4
, in case).
to perhaps more useful output purposes, can use arrays.tostring
:
system.out.println("5 number summary: " + arrays.tostring(vals));
Comments
Post a Comment