Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Beginner trying to make distance in positive number

1. ## Beginner trying to make distance in positive number

I've been programming for years in a basic programming language, so doing something a bit more advance like this is quite challenging but I love it. Could someone help me where I've gone wrong here? I've been following a tutorial but I've decided to take what I've learned and make my own program but something seems to be wrong.

```class Function{
public double abs(int num)
{
if (num > -1)
{
return num;
}
else
{
return -num;
}
}

public double distance(double aa, double bb)
{
return aa-bb;
}
}

class Test {
public static void main(String[] args)
{
double a = 1, b = 5;
Function func = new Function();
double results;

results = func.distance(a, b);
results = func.abs(results);

System.out.print(results);
}
}```

Basically trying to get the distance between to numbers but in a positive not negative number. Thank you in advance.

- Nicky

2. ## Re: Beginner trying to make distance in positive number

1 - 5 is always going to be negative. You could switch it around and do
5 - 1, or you could also place if statements which determine the larger of the
two numbers before the evaluation, then tests the smaller number against
the larger number so the result is positive.

Just so I am clear, a valid output of say 10 and 6 would be: 4 ?

3. ## Re: Beginner trying to make distance in positive number

Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

This is a great first post, and I wish you the best of luck in your recovery from Basic programming.

When you say, "something seems to be wrong," are you getting errors, unexpected results? If so, you should post those. For example, when I attempt to run your code, I get a compiler error:
```Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method abs(int) in the type Function is not applicable for the arguments (double)

at TestClass.main(TestClass.java:28)```
(I changed the name of the class containing main to "TestClass", otherwise my code is identical to what you posted.)

The error indicates a mismatch in the abs() arguments and abs() signature that requires it be passed int values. Since abs() returns doubles, the most appropriate fix is to change abs() to accept doubles, as in:

public double abs(double num)

Other suggestions:

1. Give your classes, variables, and methods better, more descriptive names. You don't get extra points for using fewer letters of the alphabet or penalized for using more.
2. At a minimum, every class and method should have a comment that describes what it does. (However, a method called "returnAbsoluteValue" may be comment enough. Even so, it's a good habit to have.)
3. Assign the top-level class containing the main() method the "public" access modifier for clarity.

Other than that, you're off to a great start! Keep coding.

4. ## Re: Beginner trying to make distance in positive number

Thank you very much for that.

- Nicky

EDIT:

awkward moment when you find out you can do math.abs(n)

5. ## Re: Beginner trying to make distance in positive number

Originally Posted by Masic1990
awkward moment when you find out you can do math.abs(n)
You mean Math.abs()

Case is significant, and when discussing classes, especially static ones like Math, it's important to get the details right.

6. ## Re: Beginner trying to make distance in positive number

awkward moment . . .
Yes, but writing simple methods like an absoluteValue() method are common programming exercises.

7. ## Re: Beginner trying to make distance in positive number

Originally Posted by jdv
You mean Math.abs()

Case is significant, and when discussing classes, especially static ones like Math, it's important to get the details right.
Yes you're right, I did know that. I will be careful in the future on the forums to save confusion! Thank you.

- Nicky