How do you find a maximum or minimum double from a set?

时间:2016-07-11 23:07:31

标签: java set double max min

In a basic java program, I've defined two methods of a class that are supposed to return the maximum and minimum numbers from a set of four doubles. The value parameter is taken in from a for loop, and then compared to the standing min or max parameter. However, the outputs are not correct and I cannot seem to find out why; I know I have made a logic error somewhere.

The two methods-

//calculate the minimum 
public double calcMin(double value, double min)
{
    if (min < value)
   {
       min = value;
   }
   return value;
}

//calculate the maximum
public double calcMax(double value, double max)
{
    if (max < value)
    {
        max = value;
    }
    return max;
}

The for loop-

    for (int i = 0; i < fillups.length; i ++)
    {
        distance[i] = fillups[i].calcDistance();
        milesPerGallon[i] = fillups[i].calcMPG(distance[i]);
        cost[i] = fillups[i].calcTotalCost();
        minimum = fillups[i].calcMin(distance[i], minimum);
        maximum = fillups[i].calcMax(distance[i], maximum);
        minMPG = fillups[i].calcMin(milesPerGallon[i], minMPG);
        maxMPG = fillups[i].calcMax(milesPerGallon[i], maxMPG);
        minPrice = fillups[i].calcMin(price[i], minPrice);
        maxPrice = fillups[i].calcMax(price[i], maxPrice);
        fillups[i].printResults(i, day[i], distance[i], cost[i], milesPerGallon[i]);  
    } 

The calcMax method seems to work, but the calcMin does not. Perhaps there is a way to make them work using the Double.MAX_VALUE and Double.MIN_VALUE constants.

1 个答案:

答案 0 :(得分:0)

First, I'll repeat your two methods:

//calculate the minimum 
public double calcMin(double value, double min)
{
    if (min < value)
   {
       min = value;
   }
   return value;
}

//calculate the maximum
public double calcMax(double value, double max)
{
    if (max < value)
    {
        max = value;
    }
   return max;
}

Do you see that you use the same logic for calcMin as you do for calcMax? Replace the < in calcMin by > and it should work.

You are searching for the smaller value thus, if the given value is smaller than the current known minimum, then you replace it. You may start with Double.MAX_VALUE for the best known minimum at the beginning (and then you make it better every round). Analogously you may use Double.MIN_VALUE in the first round as best known maximum.

A smaller, much more compact way is to use Math.min(double, double) (and max). Your methods themselves do some useless stuff, there is no need to assign min = value inside the method as min and value get deleted upon leaving the scope of this method. You could simply do this:

public double calcMin(double value, double min) {
    if (min < value) {
        return min;
    } else {
        return value;
    }
}

For finding the smallest value in a Set you may use Collections.min(Set). But be aware that this method needs to search the whole set. It may be better using a good sorting algorithm first. Also note that a Set has no order per definition, a List is sort-able.

Well, I think you get the idea. Cheers.