943,982 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2913
  • Java RSS
Sep 9th, 2007
0

Math.max prob???...XD

Expand Post »
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));

Java Syntax (Toggle Plain Text)
  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? =/
Reputation Points: 10
Solved Threads: 1
Light Poster
upstream is offline Offline
43 posts
since Sep 2007
Sep 9th, 2007
0

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

> 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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 9th, 2007
0

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

>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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 9th, 2007
0

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

^
i've tried to break it down before, like that (note that im using a double)
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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..
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Light Poster
upstream is offline Offline
43 posts
since Sep 2007
Sep 10th, 2007
1

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

(num1,num2,num2));
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 10th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by iamthwee ...
(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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Sep 10th, 2007
0

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

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...
Reputation Points: 10
Solved Threads: 1
Light Poster
upstream is offline Offline
43 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to separate a string ?
Next Thread in Java Forum Timeline: Java Beans





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC