⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 precipitationstatistics.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * PrecipitationStatistics.java   E.L. 2001-08-14
 *
 */
class Month {
  private String monthName;
  private int[] precipitation;

  /*
   * The constructor makes a copy of the precipitation data.
   * The client may therefore use the same data structure to hold data
   * for other months. Every one instance of the Month class holds
   * its own precipitation data, independent of the client's data.
   * (See section 7.3).
   */
  public Month(String initMonthName, int[] initPrecipitation) {
    monthName = initMonthName;
    int noOfDays = initPrecipitation.length;
    precipitation = new int[noOfDays];
    for (int i = 0; i < noOfDays; i++) precipitation[i] = initPrecipitation[i];
  }

  public String getMonthName() {
    return monthName;
  }

  public int getNoOfDays() {
    return precipitation.length;
  }

  /*
   * The method returns the precipitation for one special day.
   * Returns -1 if invalid index.
   */
  public int getPrecipitation(int index) {
    if (index >= 0 && index < precipitation.length) return precipitation[index];
    else return -1;  // invalid index
  }

  /*
   * The method returns the maximal rainfall in one day in the month.
   */
  public int getMaximum() {
    int max = 0;  // Remember: Local variables always have to be given a value.
    if (precipitation.length > 0) {
      max = precipitation[0];
      for (int i = 1; i < precipitation.length; i++) {
        if (precipitation[i] > max) max = precipitation[i];
      }
    }
    return max;
  }

  /*
   * The method returns the average daily precipitation in the month.
   */
  public double getAverage() {
    int sum = 0;
    for (int i = 0; i < precipitation.length; i++) {
      sum += precipitation[i];
    }
    if (precipitation.length > 0) return (double) sum / (double) precipitation.length;
    else return 0.0;
  }

  /*
   * The method returns the number of days without raining.
   */
  public int getNoOfDryDays() {
    int number = 0;
    for (int i = 0; i < precipitation.length; i++) {
      if (precipitation[i] == 0) number++;
    }
    return number;
  }

  /*
   * The method returns an array holding the indices of days
   * with precipitation equal to the maximum precipitation.
   */
  public int[] getDaysMax() {
    int max = getMaximum();

    /* Prepare for the worst;
       we create an array with room for all the days in the month. */
    int [] array = new int[precipitation.length];
    int noOfMax = 0;
    for (int i = 0; i < precipitation.length; i++) {
      if (precipitation[i] == max) {
        array[noOfMax] = i;
        noOfMax++;
      }
    }
    /* Now we create an array with correct size, and copy the data */
    int[] maxDays = new int[noOfMax];
    for (int i = 0; i < noOfMax; i++) maxDays[i] = array[i];
    return maxDays;
  }
}


class PrecipitationStatistics {
  public static void main(String[] args) {
    int[] precipitation = {1, 4, 0, 4, 3};  // a very short month for testing
    /*int[] precipitation = {};  // We have tested with arrays of
     *int[] precipitation = {1}; // length 0 and 1, too. */
    Month oneMonth = new Month("January", precipitation);
    System.out.println("Statistics " + oneMonth.getMonthName());
    System.out.println("Maximum: " + oneMonth.getMaximum());
    System.out.println("Average: " + oneMonth.getAverage());
    System.out.println("No. of dry days: " + oneMonth.getNoOfDryDays());

    for (int i = 0; i < oneMonth.getNoOfDays(); i++) {
      System.out.println("Precipitation day no. " + (i + 1) + ": "
        + oneMonth.getPrecipitation(i));
    }

    int[] maxDays = oneMonth.getDaysMax();
    for (int i = 0; i < maxDays.length; i++) {
      System.out.println("Max. precipitation day no.: " + (maxDays[i] + 1));
    }
  }
}

/* Example Run:
Statistics January
Maximum: 4
Average: 2.4
No. of dry days: 1
Precipitation day no. 1: 1
Precipitation day no. 2: 4
Precipitation day no. 3: 0
Precipitation day no. 4: 4
Precipitation day no. 5: 3
Max. precipitation day no.: 2
Max. precipitation day no.: 4
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -