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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Beginner trying to make distance in positive number

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by Masic1990; August 25th, 2014 at 12:35 PM.


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default 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 ?

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default 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. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)
    Last edited by Masic1990; August 25th, 2014 at 07:34 AM.

  5. #5
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Beginner trying to make distance in positive number

    Quote Originally Posted by Masic1990 View Post
    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. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default 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. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner trying to make distance in positive number

    Quote Originally Posted by jdv View Post
    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

Similar Threads

  1. calculate the factors of a positive integer number
    By ATB in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2014, 07:48 PM
  2. Beginner - how to make .java file run!
    By javahelp2012 in forum JDBC & Databases
    Replies: 3
    Last Post: March 27th, 2012, 01:34 PM
  3. Make a Cluster for given number of node
    By Neeraj Jain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2011, 02:09 PM
  4. The sum and product of a positive number between 1000 and 9999.
    By metaleddie13 in forum Member Introductions
    Replies: 1
    Last Post: September 15th, 2011, 04:39 AM
  5. Best beginners project in Java
    By Fendaril in forum Java Theory & Questions
    Replies: 3
    Last Post: February 10th, 2009, 07:52 AM