Joseph Kraemer
Joseph Kraemer

Reputation: 111

How do I calculate median from an array from a different class?

I have an array of values in a different class. I am trying to calculate max, min and median from this array. I have my methods created but all values still display "0" when I run the program. Can someone please point out what I am doing wrong?

import java.util.*;
public class SortedListStats
{
    public int maxTemp;
    public int minTemp;
    public int medianTemp;

    public SortedListStats()
    {
        this.maxTemp = maxTemp;
        this.minTemp = minTemp;
        this.medianTemp = medianTemp;
        InputOutput io = new InputOutput(); //This is my other class that has the array.
    }

    public int maxTemp(int[] io)     //method to determine maximum temperature
    {
        int max = io[0];
        for(int i = 0; i < io.length; i++)
        {
            if(max < io[i])
                max = io[i];
        }//end for statement
        return max;
    }//end method maxTemp

    public int minTemp(int[] io)     //method to determine mininum temperature
    {
        int min = io[0];
        for(int i = 0; i < io.length; i++)
        {
            if(min > io[i])
                min = io[i];
        }//end for statement
        return min;
    }//end class minTemp

    public int medianTemp(int[] io)
    {
        Arrays.sort(io);
        int sum = 0;
        int count = 0;
        int median;
        for(int i = 0; i > io.length; i++)
        {
            sum = sum + io[i];
            count++;
        }
        median = sum/count;
        return median;
    }//end method medianTemp
}//end class SortedListStats

Upvotes: 0

Views: 50

Answers (1)

Quicksilver002
Quicksilver002

Reputation: 735

What code are you using to call the methods on this class? The first three lines of the constructor don't do anything. If you're expecting them to call your calculation methods, then they need to be after "new InputOutput()" and they need to be function calls. Like:

this.maxTemp = maxTemp(io.getValues());

You should refrain from naming your functions the same as your member variables to avoid confusion. For instance, the "maxTemp()" function could be "calculateMaxTemp()" instead.

Upvotes: 1

Related Questions