import java.text.DecimalFormat; // for formatting import java.awt.Color; import uwcse.graphics.*; /** * A class to illustrate the use of a 2D array.
* The methods of the class initialize a 2D array, namely * an array to store the daily rainfall amount over 3 months. The * monthly rainfall average is computed. */ public class TwoDimensionalArray{ /** * Initialize a 2D array to store the daily rainfall over * a 3 months period (namely April, May and June). Do initialization * in a private method (assign to each element a random double between * 0 and 10).
* Then, compute and print the monthly rainfall average. */ public void processRainfall() { // Store in an array the daily rainfall // over a period of 3 months (April, May, June) // rainfall should be an array of 3 1D arrays (each 1D array // is of size 30, 31 and 30). double[][] rainfall = new double[3][]; rainfall[0] = new double[30]; // April rainfall[1] = new double[31]; // May rainfall[2] = new double[30]; // June // Initialize the array // (call fillRandomly) this.fillRandomly(rainfall); // Compute and print the average rainfall for each month // (call computeAndPrintMonthlyAverage) this.computeAndPrintMonthlyAverage(rainfall); // display the array this.display(rainfall); } /** * Initialize the elements of a 2D array with a random double between * 0 and 10. * @param rainfall the array to initialize */ private void fillRandomly(double[][] rainfall) { // Fill randomly the array with a random value between 0 and 10 for (int r = 0; r < rainfall.length; r ++) { for (int c = 0; c < rainfall[r].length; c ++) { rainfall[r][c] = Math.random() * 10; } } } /** * Computes and prints the daily average per month * @param rainfall the array to average */ private void computeAndPrintMonthlyAverage(double[][] rainfall) { String[] month = {"April", "May", "June"}; DecimalFormat df = new DecimalFormat("0.00"); for (int m = 0; m < rainfall.length; m ++) { double sum = 0; for (int d = 0; d < rainfall[m].length; d ++) { sum += rainfall[m][d]; } System.out.println("In " + month[m] + ", the average daily rainfall is " + df.format(sum / rainfall[m].length)); } } /* * Displays the array graphically: draws the table, * and fills the squares of the table with a color * corresponding to the rainfall amount (red = 0, * green = 10) * @param rainfall the array that contains the rainfall amounts */ private void display(double[][] rainfall) { // A 2D array of rectangles: each rectangle // corresponds to a rainfall amount. Rectangle[][] rects = new Rectangle[rainfall.length][]; for (int m = 0; m < rainfall.length; m ++) { rects[m] = new Rectangle[rainfall[m].length]; } // Create squares of side 15 // upper left corner of the first rectangle int x = 20; int y = 20; GWindow window = new GWindow(); for (int m = 0; m < rects.length; m ++) { for (int d = 0; d < rects[m].length; d ++) { // color of the square int blue = 0; int red = (int) ((1 - rainfall[m][d] / 10) * 255); int green = (int) (( rainfall[m][d] / 10 ) * 255); Color c = new Color(red, green, blue); rects[m][d] = new Rectangle(x, y, 15, 15, c, true); window.add(rects[m][d]); // next rectangle x += 15; } // next line of rectangles y += 20; x = 20; } } }