Parameters order

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

Join Date: Apr 2005
Posts: 8
Reputation: oberle1515 is an unknown quantity at this point 
Solved Threads: 0
oberle1515 oberle1515 is offline Offline
Newbie Poster

Parameters order

 
0
  #1
Apr 18th, 2005
Could someone explain parameters a little bit to me. I have to write a program that returns the difference of of two integers.(larger-smaller)

I understand the basis

class FindDifference {
//Main method
public static int difference(int num1, int num2) {

int num1 = 10;
int num2 = 4;
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 158
Reputation: nicentral is an unknown quantity at this point 
Solved Threads: 4
nicentral's Avatar
nicentral nicentral is offline Offline
Junior Poster

Re: Parameters order

 
0
  #2
Apr 18th, 2005
What do you want to know about parameters? They're basically variables set up in the method declaration that allow you to pass values to the method. If your method is to return something, you would need a return statement. I.e. your method should look like this

public static int difference(int num1, int num2){

return num1 - num2;

}

Andy
Nobody believes the official spokesman, but everybody trusts an unidentified source.

-- Please do not PM me with questions about a thread. If you respond to a thread, then everyone can benefit.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Parameters order

 
0
  #3
Apr 18th, 2005
Originally Posted by nicentral
What do you want to know about parameters? They're basically variables set up in the method declaration that allow you to pass values to the method. If your method is to return something, you would need a return statement. I.e. your method should look like this

public static int difference(int num1, int num2){

return num1 - num2;

}

Andy

to call that method you would do this:
int firstNum = 35;
int secondNum = 5;
ClassName.difference(firstNum,secondNum);
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Parameters order

 
0
  #4
Apr 20th, 2005
you always want the largest minus the smallest, here is two ways of doing just that so you don't have to worry or know the order...
  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. int n1 = 10;
  6. int n2 = 4;
  7. System.out.println(difference(n1,n2));
  8. System.out.println(difference(n2,n1));
  9. System.out.println(difference(n1,n2));
  10. System.out.println(difference(n2,n1));
  11. }
  12.  
  13. public static int difference(int num1, int num2)
  14. {
  15. return num1 > num2 ? num1-num2 : num2-num1;
  16. }
  17.  
  18. public static int subtract(int num1, int num2)
  19. {
  20. int returnVal;
  21.  
  22. if (num1 > num2)
  23. {
  24. returnVal = num1 - num2;
  25. }
  26. else
  27. {
  28. returnVal = num2 - num1;
  29. }
  30.  
  31. return returnVal;
  32. }
  33. }

I think that was what you were asking. the difference method is too intuitive so I created a second called subtract with an if statment in there which is hopefully easier to understand
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Parameters order

 
0
  #5
Apr 20th, 2005
What if that's the order it's suppose to be in?

I mean, take for instance this:

-5 - -4 = -1

By your method it would actually turn out to be this:

-4 + 5 = 1
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Parameters order

 
0
  #6
Apr 21st, 2005
if order did matter then he should go with nicentral's post,
I have to write a program that returns the difference of of two integers.(larger-smaller)
but since i saw (larger-smaller) I assumed order didn't and he wanted the big number minus the small, he didn't care about order (...I hope)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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