Ernie Velazquez

Greenhorn

Posts: 6

posted 7 years ago

Hello!

The question asks for a function that returns the minimum of a series of numbers terminated by a zero. I can only seem to get it working if I initialize min to a high number:

public static double min (){

double min=999999;

double input = 1;

System.out.println( "Please enter the numbers you'd like to find the minimum of, and end the sequence by entering zero");

while (input != 0){

input = keyboard.nextDouble();

if (input == 0 && min==999999){System.out.println("program terminating, first number can't be a zero"); System.exit(0);}

else if (input <= min){min = input;}

}

return min;

}

How would I get this to work without limiting the user to entering numbers below 999999? I've seen people use arrays, which we haven't yet covered. Any help would be greatly appreciated?

The question asks for a function that returns the minimum of a series of numbers terminated by a zero. I can only seem to get it working if I initialize min to a high number:

public static double min (){

double min=999999;

double input = 1;

System.out.println( "Please enter the numbers you'd like to find the minimum of, and end the sequence by entering zero");

while (input != 0){

input = keyboard.nextDouble();

if (input == 0 && min==999999){System.out.println("program terminating, first number can't be a zero"); System.exit(0);}

else if (input <= min){min = input;}

}

return min;

}

How would I get this to work without limiting the user to entering numbers below 999999? I've seen people use arrays, which we haven't yet covered. Any help would be greatly appreciated?

posted 7 years ago

You could make use of the Double class, which holds a static value representing the maximum double value : Double.MAX_VALUE. Or you could use a Double object, instead of a double primitive, and initialized it to null.

[My Blog]
*All roads lead to JavaRanch*

Ernie Velazquez

Greenhorn

Posts: 6

posted 7 years ago

Yes, the wrapper class Double.

When you say Double object are you referring to 'wrapping'?

Yes, the wrapper class Double.

[My Blog]
*All roads lead to JavaRanch*

posted 7 years ago

Instead, you could use a boolean, to indicate whether you have already set the minimum value or not.

[My Blog]
*All roads lead to JavaRanch*

Ernie Velazquez

Greenhorn

Posts: 6

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 7 years ago

It may be worth noting that although the class java.lang.Double (follow that link!) is known as a wrapper class, and it's primarily there for wrapping, not all the fields and methods of the class are related to wrapping. Specifically, all the static fields and methods that do not either return a Double instance or require a Double instance as a parameter - those have nothing to do with wrapping. It's just that the Double class is a useful place to collect stuff about doubles (as well as Doubles). Double.MAX_VALUE is just a useful constant.

posted 7 years ago

The definition says that the minimum of an empty list of numbers is +infinity and the maximum of an empty list of numbers is -infinity. This is because these are the identity values for the min / max operators: min(x, +infinity) = x for all possible values of x and max(x, -infinity) = x for all possible values of x. So I'd use Double.POSITIVE_INFINITY instead of Double.MAX_VALUE.

Christophe Verré wrote:You could make use of the Double class, which holds a static value representing the maximum double value : Double.MAX_VALUE. Or you could use a Double object, instead of a double primitive, and initialized it to null.

The definition says that the minimum of an empty list of numbers is +infinity and the maximum of an empty list of numbers is -infinity. This is because these are the identity values for the min / max operators: min(x, +infinity) = x for all possible values of x and max(x, -infinity) = x for all possible values of x. So I'd use Double.POSITIVE_INFINITY instead of Double.MAX_VALUE.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 7 years ago

another possible approach...

Get the first input outside of the loop. Test it for 0, but if it's not, by definition, it will be the minimum (so far). THEN enter your loop to get the rest, comparing against the first.

Get the first input outside of the loop. Test it for 0, but if it's not, by definition, it will be the minimum (so far). THEN enter your loop to get the rest, comparing against the first.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |