• Post Reply Bookmark Topic Watch Topic
  • New Topic

write a function with no args who's job is to find the minimum of a series  RSS feed

 
Ernie Velazquez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernie Velazquez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think we've covered the double class yet. When you say Double object are you referring to 'wrapping'? I think I remember the instructor mentioning that would be discussed this week. Thanks for your help!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say Double object are you referring to 'wrapping'?

Yes, the wrapper class Double.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead, you could use a boolean, to indicate whether you have already set the minimum value or not.
 
Ernie Velazquez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nah, I think you're right, the wrapper class is probably the way to go, just read up on it and it's working fine.

Thanks!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!