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;

Recommended Answers

All 5 Replies

Member Avatar for 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

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);

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...

public class Test
{
	public static void main(String[] args)
	{
		int n1 = 10;
		int n2 = 4;
		System.out.println(difference(n1,n2));
		System.out.println(difference(n2,n1));
		System.out.println(difference(n1,n2));
		System.out.println(difference(n2,n1));
	}

	public static int difference(int num1, int num2)
	{
		return num1 > num2 ? num1-num2 : num2-num1;
	}

	public static int subtract(int num1, int num2)
	{
		int returnVal;

		if (num1 > num2)
		{
			returnVal = num1 - num2;
		}
		else
		{
			returnVal = num2 - num1;
		}

		return returnVal;
	}
}

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

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

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.