public class MultidimensionalArraysExercises{ /* Two-dimensional Arrays 2. Write a method that takes a two-dimensional array as a parameter (and no other parameters). Have the method populate each value in the array. Start with 0.0 in the first index, then 0.001 in the second, etc. What should the return value be? Write some code to test your method. */ /** * Simulates data with 0.000 in the first element and 0.001 in the second, etc. * @param array A rectangular or ragged array */ public static void populate(double[][] array ){ double step = 0.001; double value = 0.0; int numRows = array.length; for( int rowI = 0; rowI < numRows; ++rowI){ int numCols = array[rowI].length; for( int colI = 0; colI < numCols; ++colI){ array[rowI][colI] = value; value += step; } } /* for( int rowI = 0; rowI < numRows; ++rowI){ double[] rowArray = array[rowI]; int numCols = rowArray.length; for( int colI = 0; colI < numCols; ++colI){ rowArray[colI] = value; value += step; } } */ } /* Two-dimensional Arrays 3a. Write a method that takes a two-dimensional array as a parameter (and no other parameters). Have the method return the total of all of the values in the array. Write some code to test your method. (If you use the method described above to populate each value in the rainfall array, then you should get a total of approximately 66.43. Note, if you're getting a total of 69.006, then you're probably using a rectangular array instead of a ragged or jagged array.) */ /** * Calculates the total of all of the values * @param array A rectangular or ragged array * @return The sum of all of the values in array */ public static double total(double[][] array ){ double total = 0.0; int numRows = array.length; for( int rowI = 0; rowI < numRows; ++rowI){ int numCols = array[rowI].length; for( int colI = 0; colI < numCols; ++colI){ total = total + array[rowI][colI]; } } return total; } public static void main( String[] args ){ /* Two-dimensional Arrays 1. Declare a two-dimensional array that can hold the amount of rainfall for everyday in a year. The first index should be the month and the second index the day of the month. */ double[][] rainfallRect = new double[12][31]; // rectangular array double[][] rainfall = new double[12][]; // ragged array int monthI = 0; rainfall[monthI++] = new double[31]; // January //rainfall[0][2] = 0.25; rainfall[monthI++] = new double[28]; // February rainfall[monthI++] = new double[31]; // March rainfall[monthI++] = new double[30]; // April rainfall[monthI++] = new double[31]; // May rainfall[monthI++] = new double[30]; // June rainfall[monthI++] = new double[31]; // July rainfall[monthI++] = new double[31]; // Aug rainfall[monthI++] = new double[30]; // Sep rainfall[monthI++] = new double[31]; // Oct rainfall[monthI++] = new double[30]; // Nov rainfall[monthI++] = new double[31]; // Dec System.out.println( "Last rainfall amount: " + rainfall[11][30] ); // Dec 31st /* Two-dimensional Arrays 3b. Write some code to test your method. (If you use the method described above to populate each value in the rainfall array, then you should get a total of approximately 66.43. Note, if you're getting a total of 69.006, then you're probably using a rectangular array instead of a ragged or jagged array.) */ System.out.println( "Total rainfall: " + total(rainfall) ); /* Multidimensional Arrays 1. Declare a multi-dimensional array that can hold the amount of rainfall for everyday in a year for 100 years. The first index should be the year, the second index the month and the third index the day of the month. Will the methods described above work in this three- dimensional array? Discuss with someone else why or why not. */ } }