Math.max prob???...XD

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2007
Posts: 32
Reputation: upstream is an unknown quantity at this point 
Solved Threads: 1
upstream's Avatar
upstream upstream is offline Offline
Light Poster

Math.max prob???...XD

 
0
  #1
Sep 9th, 2007
Hey, i am practicing in this book sample, it says i can rewrite my code for determining the highest value in my 3 input. the commented part is working perfectly (Line 19, 26), it can return the highest value...

but when i try to rewrite it using the Math.max method, it only return the second highest number, not the highest on the 3..=/

the logic is right? right?
the inner max is evaluated first then the result will be compared to the n1 variable...

return Math.max(n1,Math.max(n2,n3));

  1. import java.util.Scanner;
  2.  
  3. public class MaximumFinder
  4. {
  5. public void determineMaximumValue()
  6. {
  7. Scanner scan = new Scanner(System.in);
  8.  
  9.  
  10. System.out.print("Enter 3 float variable in spaces: ");
  11. double num1 = scan.nextDouble();
  12. double num2 = scan.nextDouble();
  13. double num3 = scan.nextDouble();
  14.  
  15. System.out.println("The maximum value is: " + maximum(num1,num2,num2));
  16. }
  17. public double maximum(double n1, double n2, double n3)
  18. {
  19. /* double maxValue = n1;
  20.  
  21. if(n2 > maxValue)
  22. maxValue = n2;
  23. if(n3 > maxValue)
  24. maxValue = n3;
  25.  
  26. return maxValue; */
  27. return Math.max(n1,Math.max(n2,n3));
  28. }
  29.  
  30. public static void main(String args[])
  31. {
  32. MaximumFinder mf = new MaximumFinder();
  33. mf.determineMaximumValue();
  34.  
  35. }
  36. }

uhm..sorry for the trouble, but can someone tell what's wrong? =/
Programming = new Art
---
I <3 BoA Kwon
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Math.max prob???...XD

 
0
  #2
Sep 9th, 2007
> the commented part is working perfectly (Line 19, 26), it can return the highest value...

Is it? If I remember correctly, finding the maximum of three numbers using only if statements is quite tricky. Or was that sorting using only if statements. wait i think it was sorting, you may be right.
Last edited by iamthwee; Sep 9th, 2007 at 4:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Math.max prob???...XD

 
0
  #3
Sep 9th, 2007
>return Math.max(n1,Math.max(n2,n3));

What happens if you break that down.

int one = 0;
int two = 0;

one = Math.max(n2,n3);

two = Math.max(n1,one);

return two;

Does that make any difference?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 32
Reputation: upstream is an unknown quantity at this point 
Solved Threads: 1
upstream's Avatar
upstream upstream is offline Offline
Light Poster

Re: Math.max prob???...XD

 
0
  #4
Sep 9th, 2007
^
i've tried to break it down before, like that (note that im using a double)
  1. double maxValue;
  2. maxValue = Math.max(n2,n3);
  3. return Math.max(n1,maxValue);

i can't say no, the output is innacurate in some point..like
  1. Enter 3 float variable in spaces: 80.5 80.1 80.3
  2. The maximum value is: 80.5
  3.  
  4. Process completed.

the output is right but this one..
  1. Enter 3 float variable in spaces: 70.5 75.5 80.1
  2. The maximum value is: 75.5
  3.  
  4. Process completed.

i think the compiler doesn't understand me =/ (lol) or it's just i dont understand the prob...

any idea anyone?
Last edited by upstream; Sep 9th, 2007 at 4:43 pm.
Programming = new Art
---
I <3 BoA Kwon
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Math.max prob???...XD

 
1
  #5
Sep 10th, 2007
(num1,num2,num2));
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,421
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 258
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Math.max prob???...XD

 
0
  #6
Sep 10th, 2007
Originally Posted by iamthwee View Post
(num1,num2,num2));
Classic typo. And isn't it grand! ;-)

I hazard to guess, that it is a little embarrassing to have it come this far before being found, though.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 32
Reputation: upstream is an unknown quantity at this point 
Solved Threads: 1
upstream's Avatar
upstream upstream is offline Offline
Light Poster

Re: Math.max prob???...XD

 
0
  #7
Sep 10th, 2007
waa..it's most embarrassing on my part..i always concentrate on the maximum method...i forgot the first part...so sorry for the trouble. =/ it's working now...
Programming = new Art
---
I <3 BoA Kwon
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC