Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private double[] rain;
- public cadimaggProject5(double r[])
- {
- // Create a new array.
- rain = new double[r.length];
- for (int i = 0; i < r.length; i++) {
- rain[i] = r[i];
- }
- }
- public double getTotalRainFall() {
- return Arrays.stream(rain).sum();
- }
- public double getAverageRainFall() {
- return Arrays.stream(rain).average().getAsDouble();
- }
- public int getHighestMonth() {
- int highest = 0;
- // Find the element with the highest value.
- for (int i = 1; i < rain.length; i++) {
- if (rain[i] > rain[highest])
- highest = i;
- }
- // Return the element number.
- return highest;
- }
- public int getLowestMonth()
- {
- int lowest = 0;
- // Find the element with the lowest value.
- for (int i = 1; i < rain.length; i++) {
- if (rain[i] < rain[lowest])
- lowest = i;
- }
- // Return the element number.
- return lowest;
- }
- public double getRainAt(int e)
- {
- return rain[e];
- }
- public static void main(String[] args)
- {
- // Array representing rainfall figures
- // position correlates to the month
- double[] thisYear = { 1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3,
- 2.4, 3.7 };
- RainfallClass r = new RainfallClass(thisYear);
- // Display the statistics.
- System.out.println("The total rainfall for this year is "
- - r.getTotalRainFall());
- System.out.println("The average rainfall for this year is "
- - r.getAverageRainFall());
- int high = r.getHighestMonth();
- System.out.println("The month with the highest amount of rain " + "is "
- - (high + 1) + " with " + r.getRainAt(high) + " inches.");
- int low = r.getLowestMonth();
- System.out.println("The month with the lowest amount of rain " + "is "
- - (low + 1) + " with " + r.getRainAt(low) + " inches.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement