/* Author : Michael Robinson Program : overloading.java Purpose : Using the same method name for multiple methods. By having different amount of variables being accepted, and/or different data types, we can use methods with the same name. This will allows us to use the same method name passing multiple variables of different data types, like when we using the print commands. Updated : October 19, 2099 */ public class overloading { //accepts unknown amount of ints public static int addNumbers( int ... numbers ) { int total = 0; for( int temp : numbers ) { total += temp; } return ( total ); }//end public static int addNumbers( int ... numbers ) //accepts one int, one double and another int public static double addNumbers( int fn, double sn, int tn ) { return ( fn + sn + tn ); }//end public static double addNumbers(int fn, double sn, int tn) //accepts one int, one double and another double public static double addNumbers( int fn, double sn, double tn ) { return ( fn + sn + tn ); }//end public static double addNumbers( // int fn, double sn, double tn ) //calls multiple methods to be overloaded. All methods have //the same name addNumbers( ? ? ? ) accepting different types //and amounts of variables public static void process() { int fn = 5; int sn = 3; int tn = 2; int fon = 1; double done = 3.9; double dtwo = 4.1; System.out.printf( "\nAdding two ints %3d + %3d = %3d\n", fn, sn, addNumbers( fn, fn ) ); System.out.printf( "\nAdding four ints " + "%3d + %3d + %3d + %3d = %3d\n", fn, sn, tn, fon, addNumbers( fn, sn, tn, fon ) ); System.out.printf( "\nAdding three numbers " + "%3d + %3.2f + %3.2f = %3.2f\n", fn, done, dtwo, addNumbers( fn, done, dtwo ) ); System.out.printf( "\nAdding three numbers " + "%3d + %3.2f + %3d = %3.2f\n", fn, done, sn, addNumbers( fn, done, sn ) ); }//end public static void process() public static void main( String arg[] ) { //calls multiple methods to be overloaded process(); }//end public static void main( String arg[] ) }//end public class overloading