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 3 of 3

Thread: Function of difference between two numbers

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Function of difference between two numbers

    Good Morning

    Can you please help with some code for a function I need.

    The function takes two numbers as arguments and basically returns the difference between them. However I always need a positive number to be returned so the fucntion needs to take the smaller number from the larger one and this needs to be worked out within the function.

    New to this so any help would be appreciated.

    Thanks

    JS


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Function

    Hello uplink600 and welcome to the Java Programming Forums

    I have written this code for you. It takes 2 integer values from the command line and returns the calculation.

    public class Uplink600 {
     
        /**
         * JavaProgrammingForums.com
         */
     
        public static int arg1, arg2;
     
        public void Calculate(){
     
            int result = 0;
     
            // If statements to determine highest value
            if(arg1 <= arg2){
                result = (arg2 - arg1);
            }
            if(arg1 >= arg2){
                result = (arg1 - arg2);
            }
     
            // Print our result to console
            System.out.println(result);
        }
     
        public static void main(String[] args) {
     
            // Error handling to check for arguments
            if(args.length < 2){
                System.out.println("You must send 2 integer command line arguments!");
                System.exit(0);
            }
     
            // Parse String arguments to Integer
            arg1 = Integer.parseInt(args[0]);
            arg2 = Integer.parseInt(args[1]);
     
            // Run Calculate Method
            Uplink600 uplink = new Uplink600();
            uplink.Calculate();
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    uplink600 (May 14th, 2009)

  4. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Function

    Here's my solution, based on the problem being. "Return the difference between the two characters, where no sign is present"
    public int Difference(int a, int b){
    	return Math.abs(a-b);
    }
    Regards,
    Chris

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    uplink600 (May 14th, 2009)

Similar Threads

  1. Traverse Function for Knights tour
    By budder8818 in forum Java Theory & Questions
    Replies: 0
    Last Post: February 4th, 2009, 02:49 PM