/* Author : Michael Robinson Program : varLenArgumentsClass.java Purpose : To call a method passing VARIABLE AMOUNT OF PARAMETERS. Notice that the variable that is accepted by the method is automatically converted to an array, so that to access each value follow the single dimension array rules. To access each value we can use regular for loops, while loops or the enhanced for loop. Updated : April 6th, 2099 */ import java.util.*; public class varLenArgumentsClass { /* Anytime we need to accept variables and we do not know how many are going to be passed we use ( int... variableName ), the three .'s mean that the amount of items we are passing could be any amount of unknown ints */ public static int getIntAverage( int... numbers ) { int total = 0; int counter = 0; //accessing and displaying the items passed for( int x : numbers ) //using the enhanced for loop { System.out.print( x ); if( counter < numbers.length - 1 ) { System.out.print( " + " ); counter++; } total += x; } System.out.printf( " = %d ", total ); return ( total / numbers.length ); //return the average value }//end public static int getIntAverage( int... numbers ) //accepts unknown amount of floats public static float getFloatAverage( float... numbers ) { float total = 0; int counter = 0; //accessing and displaying the items passed for( int x = 0; x < numbers.length; x++ ) { System.out.printf( "%.2f" , numbers[x] ); if( counter < numbers.length - 1 ) { System.out.print( " + " ); counter++; } total+=x; } System.out.printf( " = %.2f ", total ); return ( total / numbers.length ); //return the average value }//end public static float getFloatAverage( float... numbers ) //accepts unknown amount of Strings public static void printNames( String... theStrings ) { int x = 0; //accessing the items passed while( x < theStrings.length ) //using a while loop { System.out.print( theStrings[x] + " " ); x++; } System.out.println( "" ); }//end public static void printNames( String... theStrings ) //************* special data type **************** //The Object data type (notice Object with capital O), accepts //data of all data types such as int, float, double, String, etc //This method accepts unknown amount of Objects public static void passingObjects( Object ... args ) { System.out.println( "\n You have passed in " + args.length + " arguments " ); for( Object o : args ) { System.out.println( " " + o ); } System.out.println(); }//end public static void passingObjects(Object ... args) public static ArrayList ArrayListOfObjects() { //create ArrayList of Objects named ArrayLobjects ArrayList ArrayLobjects = new ArrayList(); //create four wrappers of type Integer Integer integer1 = new Integer( 3 ); Integer integer2 = new Integer( 7 ); Integer integer3 = new Integer( 11 ); Integer integer4 = new Integer( 23 ); //create a variable of data type String String food1 = "Tuna Salad"; //call method passing ArrayList of Objects named ArrayLobjects ArrayListOfObjectsResults( ArrayLobjects ); //add two Integer wrapper and one String to the ArrayList //ArrayLobjects ArrayLobjects.add( integer1 ); ArrayLobjects.add( integer2 ); ArrayLobjects.add( food1 ); //Again call method passing ArrayList of Objects named ArrayLobjects ArrayListOfObjectsResults( ArrayLobjects ); //add four Integer wrappers to the ArrayList ArrayLobjects.add( integer1 ); ArrayLobjects.add( integer2 ); ArrayLobjects.add( integer3 ); ArrayLobjects.add( integer4 ); //create a new wrapper called integer5 Integer integer5 = new Integer( 99 ); //add integer5 wrappers to the ArrayList ArrayLobjects ArrayLobjects.add( integer5 ); //Again call method passing ArrayList of Objects named ArrayLobjects ArrayListOfObjectsResults( ArrayLobjects ); //remove index 3 from the ArrayLobjects System.out.printf( "Removing value %s", ArrayLobjects.get( 2 ) + "\n" ); ArrayLobjects.remove( 2 ); //copying contents of ArrayLobjects to ArrayLobjects_clone System.out.println( "Creating ArrayLobjects_clone from ArrayLobjects" ); Object ArrayLobjects_clone = ArrayLobjects.clone(); System.out.println( "The clone contains: " + ArrayLobjects_clone ); //Again call method passing ArrayList of Objects named ArrayLobjects ArrayListOfObjectsResults( ArrayLobjects ); return( ArrayLobjects ); //returns ArrayLobjects to calling method }//end of public static ArrayList ArrayListOfObjects() public static void ArrayListOfObjectsResults( ArrayList ArrayLobjects ) { System.out.println( "ArrayLobjects contains: " + ArrayLobjects); System.out.println( "ArrayLobjects size: " + ArrayLobjects.size() + "\n" ); }//end of public static void ArrayListOfObjectsResults( ArrayList // ArrayLobjects) public static void main( String arg[] ) { System.out.println( "\nProcessing ints" ); //calling getIntAverage() method passing different parameters System.out.println( "[ The Average is : " + getIntAverage( 2, 74, 6, 8, 5, 60 ) + " ]" ); System.out.println( "[ The Average is : " + getIntAverage( 2, 74, 6, 8, 5 ) + " ]" ); System.out.println( "[ The Average is : " + getIntAverage( 2, 74, 6, 8 ) + " ]" ); System.out.println( "[ The Average is : " + getIntAverage( 2, 74, 6 ) + " ]" ); System.out.println( "[ The Average is : " + getIntAverage( 2, 74 ) + " ]" ); System.out.println( "\nProcessing Strings" ); //calling printNames() method passing different parameters printNames( "Pilar", "Mariana", "Mark", "Daniel", "Ivan", "Ana Milena", "Sebastian", "Blanca", "Michael"); printNames( "Pilar", "Ana Milena", "Mariana", "Mark","Ivan", "Sebastian", "Daniel", "Blanca" ); printNames( "Pilar", "Ana Milena", "Mariana", "Mark", "Daniel", "Sebastian", "Ivan" ); printNames( "Pilar", "Ana Milena", "Mariana", "Mark", "Daniel","Sebastian" ); printNames( "Pilar", "Ana Milena", "Mariana","Mark", "Daniel" ); System.out.printf( "\n\nProcessing floats\n" ); //calling getFloatAverage() method passing different parameters System.out.printf( "[The Average is : %.2f ]\n", getFloatAverage( 2, 74, 6, 8, 5, 60 ) ); System.out.printf( "\t [The Average is : %.2f ]\n", getFloatAverage( 2, 74, 6, 8, 5 ) ); System.out.printf( "\t\t [The Average is : %.2f ]\n", getFloatAverage( 2, 74, 6, 8 ) ); System.out.printf( "\t\t\t [The Average is : %.2f ]\n", getFloatAverage( 2, 74, 6 ) ); System.out.printf( "\t\t\t\t [The Average is : %.2f ]\n", getFloatAverage( 2, 74 ) ); System.out.println( "\n\nProcessing Objects" ); //calling passingObjects() method passing different parameters passingObjects( "This is very interesting..." ); passingObjects( "very", "flexible", "topic" ); passingObjects( 5, 3, 7 ); passingObjects( 5.1, 3, 7, "I can't believe this" ); //create an ArrayList of Objects ArrayList result = ArrayListOfObjects(); System.out.println( "The ArrayList of Object = " + result ); }//end of public static void main( String arg[] ) }//end of public class varLenArgumentsClass