I am making a porgram that calculates the Mean(return as double), Median(return as double), Mode(return as int), and Standard deviation of an array that I chose myself. The most helpful things I can find are codes where the array is an input by the user.
I have been using this and some other ones similar as kind of this guide as well as my book and notes from class. Some things I just keep tinkering with them until they somehow work.
But like I said, in my code I would just like to put the array in myself and not gather input from the user. I am stuck on the median. I have it all typed up but the compiler is throwing back 1 error that says:
1 error found: File: C:\Users\Cori\Desktop\Statistics.java [line: 41] Error: The method bubbleSort(int[]) is undefined for the type Statistics
I did the bubbleSort exactly like the link says and I have been trying all kinds of crazy stuff. I think maybe It has something to do with the variable not being defined, but I really don't know because this is all very foreign to me. Here is my entire code so far. I feel like if I can just figure this out the rest of my project will be very easy.
public class Statistics {
public static void main(String[] args) {
int[] a = { 22, 44, 66, 55, 33 };
double mean;
double median;
median = calcMed(a);
mean = calcMean(a);
System.out.println("Median:" + median);
System.out.println("Mean:" + mean);
}
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) {
int i;
int sum = 0;
int[] sortedArr = bubbleSort(a);
double median = 0;
{
int index = (sortedArr.length - 1) / 2;
median = sortedArr[index];
}
for (int v : sortedArr) {
System.out.println(v);
}
return median;
}
}
Please don't dog my formatting(just some tips would be nice). I just need to know how to fix the bubbleSort so I can calculate the median. Also I know some things are unnecessary, so if you could also give me some pointers on what is ok to delete and things that might be easier.
I figured it out.
You are missing the bubbleSort method (copied from the link in question):
/**
* This program returns a sorted version of the input array.
*
* @param arr
* @return
*/
public static int[] bubbleSort(int[] arr)
{
// We must sort the array. We will use an algorithm called Bubble Sort.
boolean performedSwap = true;
int tempValue = 0;
// If we performed a swap at some point in an iteration, this means that array
// wasn't sorted and we need to perform another iteration
while(performedSwap)
{
performedSwap = false;
// Iterate through the array, swapping pairs that are out of order.
// If we performed a swap, we set the "performedSwap" flag to true
for (int i=0; i < arr.length; i++)
{
if (arr[i] > arr[i+1])
{
tempValue = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tempValue;
performedSwap = true;
}
}
}
return arr;
}
Without this method you can't sort the array (there are more better solutions then bubblesort, but for this case ok).
The error:
1 error found: File: C:\Users\Cori\Desktop\Statistics.java [line: 41] Error: The method bubbleSort(int[]) is undefined for the type Statistics
Tells you there is missing method bubbleSort() with parameter int[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With