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

Thread: Absolute values

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Absolute values

    Hello Everyone,

    I am having a problem with my homework. They are asking for a method call that takes the large of two absolute values.

    Write a method called largerAbsVal that takes two integers as parameters and returns the larger of the two absolute values. A call of largerAbsVal(11, 2) would return 11, and a call of largerAbsVal(4, -5) would return 5.

    I have tried this code using methods in the Math Class but I am getting an error in Practice-it that says
    Line 4
    Your method's return type is void, which means that it does not return a value. But your code is trying to return a value. This is not allowed.

    cannot return a value from method whose result type is void
    return Math.max(Math.abs(Num1), Math.abs(Num2));

    Here is my code. What I am doing wrong?
    public static void largerAbsVal (int Num1, int Num2)
    {
     
        return Math.max(Math.abs(Num1), Math.abs(Num2));
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Absolute values

    Your method's return type is void, which means that it does not return a value. But your code is trying to return a value. This is not allowed.
    That message says what the problem is. Change the void to a valid datatype that the method is returning.

    See the tutorial: http://docs.oracle.com/javase/tutori...turnvalue.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  2. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  3. Need help with my code! Absolute coordinates
    By MrLemons in forum Java Theory & Questions
    Replies: 10
    Last Post: September 16th, 2013, 04:17 PM
  4. absolute and relative path
    By viper_pranish in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 20th, 2013, 10:34 AM
  5. absolute beginner needs direction
    By action711 in forum JDBC & Databases
    Replies: 2
    Last Post: March 8th, 2013, 08:55 AM