/** * Using an array as a parameter in a method.
* Call a method to input a date (3 integers: year, month and day * stored in array)
* Note: in terms of design, it would be better to define a Date class.
*/ import uwcse.io.*; public class ArrayAsAParameter { /** * Input and print a date. The input of the date is done * in an auxiliary private method. */ public void inputAndPrintADate() { // date variables int year, month, day; // The above variables have primitives types // We can't modify them from within a method // Use an array instead int[] date = new int[3]; // Read the date with the readDate method readDate(date); // To make it clearer, store the date in year, month and day year = date[0]; month = date[1]; day = date[2]; // Print the date to check it System.out.println("Date is "+((month<10)?"0":"")+month+"/"+((day<10)?"0":"")+day+"/"+year); } /** * Store in the array the input of the date.
* The modifications to the array are visible within * inputAndPrintADate. This is because an array is an object. */ private void readDate(int[] dateArray) { Input input = new Input(); // Get the year and store it in the array (element 0) dateArray[0] = input.readInt("Enter the year: "); // Get the month and store it in the array (element 1) dateArray[1] = input.readInt("Enter the month: "); // Get the day and store it in the array (element 2) dateArray[2] = input.readInt("Enter the day: "); } }