0
public class Test {

    public static int f1( int m1 ) 
    {
        return ( m1 + 1 );
    }

    public static double f1( double m1 ) 
    {
        return ( m1 * m1 );
    }

    public static void main( String[] arg ) {
        double x1, x3;
        x1 = 2.0;
        x2 = 5.0;
        x3 = f1( x1 );
        System.out.println("x3 = “ + x3 );
    }
}

I know it prints out 4.0 but how does it get this answer. Im cunfused as to where you get # for m1?

Edited by Catarino

2
Contributors
2
Replies
19
Views
2 Years
Discussion Span
Last Post by rubberman
0

Ok i figured it out incase anyone needed to know as well. Essentially since x1 is a double you ise the second method and you substitute x1 for m1 making it 2.0 * 2.0 = 4.0.

0

Glad you figured it out. This is very basic Java/C++/C# coding. The type of variable that is passed to an overloaded function (which f1() is) will determine which instance of the function is invoked. Since you passed a double (x1), it invoked public static double f1( double m1 ) and not the integer version.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.